tommy2shoes Posted March 29, 2011 Share Posted March 29, 2011 I have a table called parties with 3 fields - partyid, partyname, linkid. For any linkid there will be a number of parties somewhere between 2 and 10. I can get a vertical list but have a couple of issues which I can't fix: I want to have a page that displays the list of partynames (alphabetically) horizontally rather than vertically within a piece of text. For example, "The parties linked to you are Party1, Party2, Party3" Also, ideally, I would like the word 'and' before the last Party name so, using the above example, I would get "The parties linked to you are Party1, Party2 and Party3" I have no idea how to do this or if it can be done. Any ideas would be VERY gratefully received. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/232048-horizontal-list-from-a-recordsetquery/ Share on other sites More sharing options...
Pikachu2000 Posted March 29, 2011 Share Posted March 29, 2011 In the FAQ/Code Snippet Repository forum, I believe the first thread under the stickies, 'Multi-column Results' is what you're after. Quote Link to comment https://forums.phpfreaks.com/topic/232048-horizontal-list-from-a-recordsetquery/#findComment-1193655 Share on other sites More sharing options...
litebearer Posted March 29, 2011 Share Posted March 29, 2011 Show us what code you have tried thus far Quote Link to comment https://forums.phpfreaks.com/topic/232048-horizontal-list-from-a-recordsetquery/#findComment-1193658 Share on other sites More sharing options...
sasa Posted March 29, 2011 Share Posted March 29, 2011 look <?php $sql = 'SELECT partyname, linkid FROM parties ORDER BY partyname'; $res = mysql_query($sql); $out = array(); while ($row = mysql_fetch_array($res)) { $out[$row['linkid']][] = $row['partyname']; } foreach ($out as $link => $parties){ if(count($parties) == 1 ){ echo "The party linked to $link is ", $parties[0], '.'; } else { $last = array_pop($parties); echo "The parties linked to $link are ", implode(', ', $parties), " and $last."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232048-horizontal-list-from-a-recordsetquery/#findComment-1193748 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.