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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.