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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.