user2078728
user2078728

Reputation: 11

Combine Data in Tables for Same ID's.

I need help combining data from MySQL with PHP and showing it in the same tables.

Now I'm getting this:

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 28      | Salad   |    4    | 10.99|
--------------------------------------

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 28      | Pizza   |    1    | 15   |
--------------------------------------

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 26      | Fish   |    3     | 12   |
--------------------------------------

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 22      | Pizza   |    1     | 15  |
--------------------------------------

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 22      | Salad   |    1    | 10.99|
--------------------------------------

And I want to show it like this:

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 28      | Salad   |    4    | 10.99|
| 28      | Pizza   |    1    | 15   |
--------------------------------------

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 26      | Fish   |    3     | 12   |
--------------------------------------

--------------------------------------
|Order ID | Product | Quantity| Cost |
--------------------------------------
| 22      | Pizza   |    1     | 15  |
| 22      | Salad   |    1    | 10.99|
--------------------------------------

My PHP code:

$username2= $_SESSION['Username'];
$result = mysql_query("SELECT * FROM order_detail, products WHERE order_detail.user =   '$username2' AND order_detail.productid = products.serial ORDER BY orderid DESC");


while($row = mysql_fetch_array($result))
{
echo "<table class='curvedEdges'>
<tr>
<th>Order ID</th>
<th>Product</th>
<th>Quantity</th>
<th>Cost</th>
</tr>";

echo "<tr>";
echo "<td>" . $row['orderid'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['price'] . " LT</td>";
echo "</tr>";
}
echo "</table>";
}

What is the easiest way to do this in PHP? Maybe someone can show me the code? :) Thanks!

Upvotes: 1

Views: 156

Answers (2)

pratik garg
pratik garg

Reputation: 3342

I think you need to go for two loop to achieve this -

    $username2= $_SESSION['Username'];
    $result2 = mysql_query("SELECT distinct orderid FROM order_detail, products WHERE order_detail.user =   '$username2' AND order_detail.productid = products.serial ORDER BY orderid DESC"); 

    while($row2 = mysql_fetch_array($result2))
    {
    $row2orderid = $row2['orderid'];

    $result = mysql_query("SELECT * FROM order_detail, products WHERE order_detail.user =   '$username2' AND order_detail.productid = products.serial and orderid = '$row2orderid' ORDER BY orderid DESC");

echo "<table class='curvedEdges'>
    <tr>
    <th>Order ID</th>
    <th>Product</th>
    <th>Quantity</th>
    <th>Cost</th>
    </tr>";

    while($row = mysql_fetch_array($result))
    {    
    echo "<tr>";
    echo "<td>" . $row['orderid'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['quantity'] . "</td>";
    echo "<td>" . $row['price'] . " LT</td>";
    echo "</tr>";
    }
    echo "</table>";
    }

Upvotes: 0

Adam Plocher
Adam Plocher

Reputation: 14233

Something like this should work:

$orderID = null;
$tableShown = false;

while($row = mysql_fetch_array($result)) {
    if ($orderID != $row['orderid']) {
        if ($tableShown)
            echo "</table>";

        echo "<table class='curvedEdges'>
        <tr>
        <th>Order ID</th>
        <th>Product</th>
        <th>Quantity</th>
        <th>Cost</th>
        </tr>";
        $tableShown = true;
    }

    echo "<tr>";
    echo "<td>" . $row['orderid'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['quantity'] . "</td>";
    echo "<td>" . $row['price'] . " LT</td>";
    echo "</tr>";

    $orderID = $row['orderid'];
}

if ($tableShown)
    echo "</table>";

This should cause the HTML table to get recreated with headers for each change in the Order ID.

Upvotes: 2

Related Questions