Skip headers when parsing CSV file

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;
							$counter=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') ");	
									 $no_of_clients++;																									
								}									
								$counter++;																	
							}

Ooo, this very recent thread maybe of some interest.

How déjà vu. :wink:

Have you looked into MySQL’s LOAD DATA or other import utilities? Cutting out the middle-man, or making him do hardly any work, can oftentimes make life much easier.