944,156 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 16147
  • PHP RSS
Jan 7th, 2005
0

parsing csv file

Expand Post »
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

PHP Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AllenGarner is offline Offline
1 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: limit the number of downloads
Next Thread in PHP Forum Timeline: http://www.ultracoder.com





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC