parsing csv file

Reply

Join Date: Jan 2005
Posts: 1
Reputation: AllenGarner is an unknown quantity at this point 
Solved Threads: 0
AllenGarner AllenGarner is offline Offline
Newbie Poster

parsing csv file

 
0
  #1
Jan 7th, 2005
I am trying to Upload a csv file then split it up into an an array and build a query to insert it into a mysql table I have succeeded in getting this to work with one exception when I come across a field seperated by commas and enclosed by double quotes if that field also contains double quotes inside of it it throws off my column count I need to be able to consider something like - ,"sometext"some more text"additional text", as one field or value here is what i have so far

  1. $row = 1;
  2. $handle = fopen($uploadFile, "r");
  3.  
  4. while ( ($data = fgetcsv($handle, 10000, ",")) != FALSE ) {
  5. $num = count($data);
  6. if($row == 1){
  7. $fields = " `id` , ";
  8. for($i=0; $i<$num; $i++){
  9. $fields .= "`" . addslashes(trim($data[$i])) . "` , ";
  10. }
  11. $fields .= "`group` , `user` , ";
  12. $f = substr($fields, 0, -1);
  13. $f = substr($f, 0, -1);
  14. $row++;
  15. }else{
  16. $values = " '' , ";
  17. for($i=0; $i<$num; $i++){
  18. $values .= "'" . addslashes(trim($data[$i])) . "' , ";
  19. }
  20. $values .= "'personal' , '" . $_SESSION['user'] . "' , ";
  21.  
  22. $v = substr($values, 0, -1);
  23. $v = substr($v, 0, -1);
  24. $sql = "INSERT INTO `addressbook` ( $f ) VALUES ( $v )";
  25. mysql_query($sql) or die(print "<center><font size='2' face='verdana' color='#5E6B79'><b>Upload complete some entries may not have been loaded successfully!</b><br><br><a href='addressbook.php' class='light'>Back to Address Book</a></font></center></td></tr></table>");
  26. $values = '';
  27. $row++;
  28. }
  29. }
  30. fclose ($handle);

can anyone help? thanx
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC