hoopplaya4 Posted July 31, 2009 Share Posted July 31, 2009 Hello, I currently have a SELECT statement (note, this is a simplified version): SELECT group_concat(DISTINCT receiver.usrFirst, ' ', receiver.usrLast) as receiver_name, COUNT(DISTINCT receiver.usrFirst) as receiver_count //etc.... That outputs the following: <?php echo $row['receiver_name']; //Which outputs: John Smith,John Doe,Jane Doe echo $row['receiver_count']; //Which outputs: 3 I'd like to see if I can get some help writing the PHP to display it like: John Smith, John Doe, and Jane Doe 1) Notice the <space> after the comma 2) Notice the <and> before the last person. Any direction on this one would be very helpful. Link to comment https://forums.phpfreaks.com/topic/168305-solved-for-loop-commas-and-select-statement/ Share on other sites More sharing options...
lonewolf217 Posted July 31, 2009 Share Posted July 31, 2009 untested., there may be a fancier way to do this, but i think this would work <?php $a = explode(",",$row['receiver_name']); $count = $row['receiver_count']; $i ==0; foreach($a as $name) { if($i == 0) { echo $name; //this is the first name } if($i == $count) { echo " and " . $name ; //this is the last name } else { echo ", " . $name; } $i++; } Link to comment https://forums.phpfreaks.com/topic/168305-solved-for-loop-commas-and-select-statement/#findComment-887746 Share on other sites More sharing options...
hoopplaya4 Posted July 31, 2009 Author Share Posted July 31, 2009 Thanks for the reply lonewolf. So, I think that was close, but it now displays something like this: John Smith, John Smith, John Doe, Jane Doe Note: The <and> does not seem to appear and John Smith shows up twice. Link to comment https://forums.phpfreaks.com/topic/168305-solved-for-loop-commas-and-select-statement/#findComment-887750 Share on other sites More sharing options...
wildteen88 Posted July 31, 2009 Share Posted July 31, 2009 Use the following as the query SELECT group_concat(DISTINCT receiver.usrFirst, ' ', receiver.usrLast SEPARATOR ', ' ) ) as receiver_name, COUNT(DISTINCT receiver.usrFirst) as receiver_count //etc.... This should generate a list like John Smith, John Doe, Jane Doe Now to add the and before the last name you'd use $receiver_name = $row['receiver_name']; $receiver_name = substr_replace($receiver_name, ' and', strrpos($receiver_name, ',')+1, 0); Link to comment https://forums.phpfreaks.com/topic/168305-solved-for-loop-commas-and-select-statement/#findComment-887758 Share on other sites More sharing options...
hoopplaya4 Posted July 31, 2009 Author Share Posted July 31, 2009 That works great. Thanks! Link to comment https://forums.phpfreaks.com/topic/168305-solved-for-loop-commas-and-select-statement/#findComment-887765 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.