Milani Posted January 10, 2008 Share Posted January 10, 2008 Hi. The routine below creates a MySQL query for each line that it finds in a csv file. The first line of my csv is the headers row, so I would like to tweak the following code to skip the first line. But of course my skills are not strong enough to do it myself. Some sort function that skips the output when $lines = 1 (I think). Please help if you can. $lines = 0; $queries = ""; $linearray = array(); foreach(split($lineseparator,$csvcontent) as $line) { $lines++; $linearray = explode($fieldseparator,$line); $linemysql = implode("','",$linearray); $query = "insert into $databasetable values('$linemysql');"; $queries .= $query . "\n"; @mysql_query($query); } Quote Link to comment https://forums.phpfreaks.com/topic/85452-solved-removing-first-line-of-output/ Share on other sites More sharing options...
teng84 Posted January 10, 2008 Share Posted January 10, 2008 $lines = 0; $queries = ""; $linearray = array(); foreach(split($lineseparator,$csvcontent) as $line) { $lines++; if ($lines == 1){ continue; } $linearray = explode($fieldseparator,$line); $linemysql = implode("','",$linearray); $query = "insert into $databasetable values('$linemysql');"; $queries .= $query . "\n"; @mysql_query($query); } maybe Quote Link to comment https://forums.phpfreaks.com/topic/85452-solved-removing-first-line-of-output/#findComment-436040 Share on other sites More sharing options...
Ken2k7 Posted January 10, 2008 Share Posted January 10, 2008 Find: $linearray = explode($fieldseparator,$line); Replace: $linearray = array_splice(explode($fieldseparator,$line),1); Quote Link to comment https://forums.phpfreaks.com/topic/85452-solved-removing-first-line-of-output/#findComment-436042 Share on other sites More sharing options...
Milani Posted January 11, 2008 Author Share Posted January 11, 2008 Thanks to both of you! Unfortunately $linearray = array_splice(explode($fieldseparator,$line),1); did not work. But teng84's solution did. So that I can better understand what is happening here. I see you identify the line we want to skip with if ($lines == 1). How does the script know not to proccess line 1, and then how does it know to resume counting up from 1? In any case - I'm happy I have a solution. Thanks again!! Quote Link to comment https://forums.phpfreaks.com/topic/85452-solved-removing-first-line-of-output/#findComment-436092 Share on other sites More sharing options...
Ken2k7 Posted January 11, 2008 Share Posted January 11, 2008 In teng's code, continue; skips the current iteration of the loop and continues at the beginning of the next iteration. Also, if this is solved, please check this as topic solved with the button at the bottom. Edit: I'm questioning why your code inserts into the database everytime the for loop runs. You would end up with multiple entries. Quote Link to comment https://forums.phpfreaks.com/topic/85452-solved-removing-first-line-of-output/#findComment-436094 Share on other sites More sharing options...
Milani Posted January 11, 2008 Author Share Posted January 11, 2008 I see. Thanks! It doesn't loop to create multiple entries of the came csv line (record). It creates 1 "insert into... " for each line from the csv. Then the loop stops. Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/85452-solved-removing-first-line-of-output/#findComment-436114 Share on other sites More sharing options...
Ken2k7 Posted January 11, 2008 Share Posted January 11, 2008 But isn't the mysql_query a part of the foreach loop? Quote Link to comment https://forums.phpfreaks.com/topic/85452-solved-removing-first-line-of-output/#findComment-436116 Share on other sites More sharing options...
teng84 Posted January 11, 2008 Share Posted January 11, 2008 yes see your point it will insert duplicate entry but if he defined his field as unique it wont and it will not trow an error because of @ any ways move that after the loop Quote Link to comment https://forums.phpfreaks.com/topic/85452-solved-removing-first-line-of-output/#findComment-436123 Share on other sites More sharing options...
Ken2k7 Posted January 11, 2008 Share Posted January 11, 2008 I just raised that up because I wondered why array_splice didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/85452-solved-removing-first-line-of-output/#findComment-436126 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.