rubing Posted July 25, 2008 Share Posted July 25, 2008 hey friends, I am using php to display all results from my table as a comma seperated string. However I don't want the comma after the last result. Should I just use string substition to eliminate the last character? while ((($row = $result->fetch_assoc()) !== NULL) && ($i < $limit)) { $clip=$row['mp3id']; $clip ="http://www.example.com/musicserve.php?id=".$clip; echo $clip . ","; ++$i; } Link to comment https://forums.phpfreaks.com/topic/116605-echoing-associative-array-as-csv-how-to-omit-last-comma/ Share on other sites More sharing options...
JonnoTheDev Posted July 25, 2008 Share Posted July 25, 2008 No. Store the values into an array and then implode as a string: $values = array(); // your while loop while($row = $result->fetch_assoc()) { $values[] = $row['mp3id']; } print implode(",", $values); Link to comment https://forums.phpfreaks.com/topic/116605-echoing-associative-array-as-csv-how-to-omit-last-comma/#findComment-599572 Share on other sites More sharing options...
rubing Posted July 26, 2008 Author Share Posted July 26, 2008 Awesome! I knew string substition was an ass backward approach! Link to comment https://forums.phpfreaks.com/topic/116605-echoing-associative-array-as-csv-how-to-omit-last-comma/#findComment-599849 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.