开发者

category text file to php array with explode

I need to import a large category .txt file to my mysql database. This text file has categories with 3 levels.

My text file look likes the example below (+/- 362 entries):

Business
Business = Business Innovation & Ideation = Innovation
Business = Economics
Business = Economics = Global Economics

My database table structure:

| id | cat | sub_cat | sub_sub_cat | 

What I want to do, is insert the categories in my database by php. The separator between the cat, sub and sub_sub cat is '='. What I want to reach is a something like a loop to insert this data to my database.

开发者_StackOverflow

Thanks in advice.

With kind regards,

Nicky


$lines = file('category.txt'); // slurp file and split into an array by lines

foreach($lines as $line) {
    $parts = explode('=', $line); // decompose a line into individual sections
    $cat = mysql_real_escape_string(trim($parts[0])); // prepare sections for SQL
    $subcat = mysql_real_escape_string(trim($parts[1]));
    $subsubcat = mysql_real_escape_string(trim($parts[2]));

    $sql = "INSERT INTO yourtable (cat, sub-cat, sub_sub_cat) VALUES ('$cat', '$subcat', '$subsubcat');";
    mysql_query($sql) or die(mysql_error());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜