Jump to content

horizontal list from a recordset/query


tommy2shoes

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/232048-horizontal-list-from-a-recordsetquery/
Share on other sites

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.";
    }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.