steveangelis Posted August 9, 2009 Share Posted August 9, 2009 I have created a simple PHP list and after each item I have a comma. I am unsure though how to have it so that after the query posts the last item it does not have a comma at the end. Here is my code: while($rowd = mysql_fetch_array($qrd)) { $qrd2= mysql_query("select * from dpt where id=".$rowd['dpt_id']); $rrd2 = mysql_fetch_array($qrd2); echo $rrd2['name'].", "; } Those code is nothing fancy but I am unsure to have the last item not have a trailing comma. Any ideas? Link to comment https://forums.phpfreaks.com/topic/169510-solved-lists-and-commas/ Share on other sites More sharing options...
smerny Posted August 9, 2009 Share Posted August 9, 2009 $i = 0; while($rowd = mysql_fetch_array($qrd)) { $qrd2= mysql_query("select * from dpt where id=".$rowd['dpt_id']); $rrd2 = mysql_fetch_array($qrd2); if($i > 0) echo ", " echo $rrd2['name']; $i += 1; } try this Link to comment https://forums.phpfreaks.com/topic/169510-solved-lists-and-commas/#findComment-894363 Share on other sites More sharing options...
Garethp Posted August 9, 2009 Share Posted August 9, 2009 $Content = ""; while($rowd = mysql_fetch_array($qrd)) { $qrd2= mysql_query("select * from dpt where id=".$rowd['dpt_id']); $rrd2 = mysql_fetch_array($qrd2); $Content .= $rrd2['name'].", "; } $Content = substr_replace($string ,"",-1); echo $Content; Link to comment https://forums.phpfreaks.com/topic/169510-solved-lists-and-commas/#findComment-894371 Share on other sites More sharing options...
steveangelis Posted August 9, 2009 Author Share Posted August 9, 2009 That works thank you. Link to comment https://forums.phpfreaks.com/topic/169510-solved-lists-and-commas/#findComment-894372 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.