Jump to content

[SOLVED] Removing first line of output


Milani

Recommended Posts

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);
}

Link to comment
https://forums.phpfreaks.com/topic/85452-solved-removing-first-line-of-output/
Share on other sites

$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

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!!

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.