SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    May 2011
    Location
    Idaho
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fgetcsv skip first line

    I have searched the archives and could not find what I need.

    I have this code, which works great for uploading the entire csv file into my database :

    $objCSV = fopen("data/".$_FILES["fileCSV"]["name"], "r");

    while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
    $strSQL = "INSERT INTO cvs ( id, data_a, data_b, data_c, data_d ) VALUES ( '$objArr[0]', '$objArr[1]', '$objArr[2]', '$objArr[3]', '$objArr[4]') ";
    $objQuery = mysql_query($strSQL);
    }


    I need to skip line (1) so as NOT to upload the field headers.
    Any help would be appreciated

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    $flag = true;
    while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
    if($flag) { $flag = false; continue; }
    $strSQL = "INSERT INTO cvs ( id, data_a, data_b, data_c, data_d ) VALUES ( '$objArr[0]', '$objArr[1]', '$objArr[2]', '$objArr[3]', '$objArr[4]') ";
    $objQuery = mysql_query($strSQL);
    }

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Welcome to Sitepoint CowboyTed.

    You could also look to use SplFileObject, as it's iterable, you can use the LimitIterator on it to skip the first line.

    PHP Code:
    <?php
    $csv 
    = new SplFileObject('path/to/file.csv''r');
    $csv->setFlags(SplFileObject::READ_CSV);

    foreach(new 
    LimitIterator($csv1) as $line){
      
    var_dump($line); #it's a numerically indexed array
    }
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    This might be of interest too: https://gist.github.com/485810
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Member
    Join Date
    May 2011
    Location
    Idaho
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks StarLion,

    How exactly does that code cause the first line to be skipped. (So I have this clear in my mind for future use)

  6. #6
    SitePoint Member
    Join Date
    May 2011
    Location
    Idaho
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Anthony,

    Reading some of your posts from this site (from google) is what convinced me that this may be a site worth joining.

    I am not familiar with SplFileObject - could you expalin a little more,.. I just saw you posted a link as well and I will read it too.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Gee, thanks.

    I've added some comments to help, hopefully.

    PHP Code:
    <?php
    /*
      Open the CSV file for reading (r)
    */
    $csv = new SplFileObject('path/to/file.csv''r');

    /*
      Tell the object that this file should
      be treated as a CSV
    */
    $csv->setFlags(SplFileObject::READ_CSV);

    /*
      Wrap the CSV file in a filter which
      skips the first line
    */
    $csv = new LimitIterator($csv1);

    /*
      Loops over the CSV file, the SplFileObject
      automatically creates an array for you
    */
    foreach($csv as $line){
      
    var_dump($line); #it's a numerically indexed array
    }
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CowboyTed View Post
    Thanks StarLion,

    How exactly does that code cause the first line to be skipped. (So I have this clear in my mind for future use)
    The flag is set to true outside the loop.
    The first line gets read.
    The IF statement finds that the flag is true; sets the flag to false, and goes back to the beginning of the loop (Continue)
    The second line gets read. Now the flag is false, so the if doesnt execute, and the code procedes as normal.

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Anthony, nice - thanks for mentioning that method.

    Can you name any another benefits of using the SPL library to do that?

  10. #10
    SitePoint Member
    Join Date
    May 2011
    Location
    Idaho
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    I used StarLion's modification although I am not quite sure how it works. It works though!! But if I don't understand why it works then I have only hacked it.

    I will try your idea out as well,.. I will have to look at it a bit and see how to work it into my code.

    As I am writing this I see StarLion's reply,... oh my I cannot keep up with you guys

    Thanks so much,
    I really think I found a good forum at last!!!!!!!!!!!!

  11. #11
    SitePoint Member
    Join Date
    May 2011
    Location
    Idaho
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks StarLion,

    That is what I was thinking but I wasn't sure. It sure was an "easy" way to fix this issue. I do want to get into Anthoy's idea too.

    I am by no means a guru but I am well along in this crazy language and am hooked!
    I keep getting deeper and deeper and have found there is no end to learning,...

    Thanks again


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •