PHP import CSV and skip headers
Results 1 to 5 of 5

Thread: PHP import CSV and skip headers

  1. #1
    Junior Member
    Join Date
    Apr 2007

    PHP import CSV and skip headers

    Hi I am writing a file import script for CSV files. I have an option where the user selects whether the file contains headers or not. In case the file does contain headers ($headers == 1), I want the script to skip the first line of the CSV file.

    I was wondering what's the best way to do this, I came up with the following code which works, but I suspect there is a better way to do it:

    							$no_of_clients = 0;
    							while ( ($data = fgetcsv($handle, '', ',') ) !== FALSE ) {
    								if  ( ($counter == 0) && ($headers == 1) ) { } 
    								else {
    									$business_name = trim($data[0]);
    									$name = trim($data[1]);
    									$surname = trim($data[2]);			
    									$comments = trim($data[3]);
    									$email = trim($data[4]);
    									$telephone = trim($data[5]);				
    									$address = trim($data[6]);
    									$vatno = trim($data[7]);			
    									$date_joined = trim($data[8]);			  
    									$active = trim($data[9]);									
    									$db->query("INSERT INTO clients (client_id, business_name, name, 
    												surname, comments, email, address, vatno, date_joined, active)".
    												"VALUES (NULL, '$business_name', '$name', '$surname', '$comments', '$email', '$address',
    												 '$vatno',"."'$date_joined', '$active') ");	

  2. #2
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Accelerating Windows at 9.81 m/s....
    Goodness gracious ... what are your tabs set at? 85?
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  3. #3
    Senior Member
    Join Date
    May 2006
    That's certainly one way of doing it, hard to say if there is a "better" way. Since you are reading line by line and not the entire file into a variable, you pretty much have to have the counter like you have. The only improvement I can see, other then the tabs , is to build your if statement in such a way that you don't have an empty code block.

    PHP Code:
    if($headers == && $counter != 0){
    //process logic

    other methods I've seen are to read the complete file into a string, then using unset to get rid of the first entry. Something like...
    PHP Code:
    $data file_get_contents($file_path);
    $lines explode("\n"$data);


    $lines as $line){

  4. #4
    Senior Member
    Join Date
    Sep 2002
    Use the fileImporter class here:

    It took the headaches out of my CSV imports.

  5. #5
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    You don't really need a counter:
    PHP Code:
    if($headers == 1) {
    fgets($handle); // read and ignore the first line
    while( (
    $data fgetcsv($handle''',') ) !== FALSE ) {
    // etc.

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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