Jump to content

Displaying one and many values


thatsme2

Recommended Posts

Hello,

 

  I have a table (members_table) and another table (purchased_table).  Members contains fields (id, fname, lname, address).  purchased_table has fields (purchased_id, member_id, purchased_item_id).  To display i am using a join statement,

SELECT * FROM members_table m INNER JOIN purchased_table p ON m.member_id=p.member_id WHERE m.member_id=10;

 

The above query returns member_details as many items the member has brought items. I want to display the member details only once and purchased_items as many items as the member has brought.

 

to display iam currently using,

 

while($r = mysql_fetch_assoc($q))

{

  $fname = $r[fname];

  $lname = $r[lname];

  $r[purchased_item_id] = $r[purchased_item_id];

}

 

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/98434-displaying-one-and-many-values/
Share on other sites

I want to display the member details only once and purchased_items as many items as the member has brought.

 

Erm.. So if you have 2 members each have 6 items you want.. how many rows ?

12 but 2 ???

your asking to have 1 table with different records set!

 

Method A

<?php 
$sql = "SELECT m.id, m.fname, m.lname, p.purchased_item_id
        FROM members m
            INNER JOIN purchased_table p ON m.id = p.member_id
        ORDER BY m.id";
$res = mysql_query($sql) or die (mysql_error()."<pre>$sql</pre>");
$previd = '';
while (list($id, $fn, $ln, $itm) = mysql_fetch_row($res))                                       // edit: % corrected
{
    if ($previd != $id)                           // is a new member id
    {
        echo "<h3>$fn $ln</h3>";                  // then output it
        $previd = $id;
    }
    echo "$itm<br/>";
}
?>

 

Method B

<?php 
$sql = "SELECT m.id, m.fname, m.lname, GROUP_CONCAT(p.purchased_item_id SEPARATOR '<br/>')
        FROM members m
            INNER JOIN purchased_table p ON m.id = p.member_id
        GROUP BY m.id";
$res = mysql_query($sql) or die (mysql_error()."<pre>$sql</pre>");

while (list($id, $fn, $ln, $items) = mysql_fetch_row($res))                                       // edit: % corrected
{
        echo "<h3>$fn $ln</h3>";

        echo "$items";
}

?>

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.