t_machine Posted August 18, 2007 Share Posted August 18, 2007 hi, I am wondering if anyone could help. I have a loop that displays my results and places a comma after each. The problem is that when the last result is displayed, it shows the comma. Example: for loop{ echo $row['test'].', '; } displays... test1, test2, test3, Notice after test3, there is still a comma? How do I remove it and have it display... test1, test2, test3 Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/65610-solved-how-to-check-if-its-the-last-result-and-remove-comma/ Share on other sites More sharing options...
marcus Posted August 18, 2007 Share Posted August 18, 2007 $sql = "SELECT * FROM `tbl` WHERE `this`='that'"; $res = mysql_query($sql) or die(mysql_error()); $num = mysql_num_rows($res); $x=1; while($row = mysql_fetch-assoc($res)){ if($x == $num){ $comma = ""; }else { $comma = ", "; } echo $row['text'] . $comma; $x++; } Quote Link to comment https://forums.phpfreaks.com/topic/65610-solved-how-to-check-if-its-the-last-result-and-remove-comma/#findComment-327643 Share on other sites More sharing options...
t_machine Posted August 18, 2007 Author Share Posted August 18, 2007 Thank you very much. It works great Quote Link to comment https://forums.phpfreaks.com/topic/65610-solved-how-to-check-if-its-the-last-result-and-remove-comma/#findComment-327653 Share on other sites More sharing options...
Barand Posted August 19, 2007 Share Posted August 19, 2007 The problem with adding commas after the item is that you are never sure that you have reached the last item However, you always know when you output the first. So an alternative technique is to output the comma then the item unless it is the first item. Quote Link to comment https://forums.phpfreaks.com/topic/65610-solved-how-to-check-if-its-the-last-result-and-remove-comma/#findComment-328361 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.