tags:

views:

78

answers:

2

I have 2 table.

Table1 "Order"
orderid - customer_id
1001    - 1234

Table2 "Items"
no - orderid - items_code
1  - 1001 - 100
2  - 1001 - 200
3  - 1001 - 300

how to get results as below (in php):

Order #    Items             Customer ID
_________________________________________
1001       100, 200, 300     1234
_________________________________________
1002       400, 500, 600     1210
_________________________________________
1003       321, 654, 987     1256
_________________________________________

This is my previous coding:

    <?

include("cat-config.php");

$resultdata=mysql_query("
(SELECT * FROM Order LIMIT 10)
");
echo "<table width=\"100%\" border=\"0\">
  <tr>
    <td>Order #</td>
    <td>Items</td>
    <td>Customer ID</td>
  </tr>";
while($row=mysql_fetch_assoc($resultdata)){
  echo "
  <tr>
    <td>$row[OrderID]</td>
<td>
**(I want loops items data on table "items": 100, 200,300 here)
</td>
    <td>$row[CustomerID</td>
  </tr>
";
}
mysql_close();
?>
A: 

Easiest is to run it as 2 seperate queries...

SELECT * FROM Order

while loop through results

X = OrderID

SELECT * FROM Items WHERE OrderId = X

fire
can you implement your solutions to my previous coding, please see my previos coding above.
key
A: 
$orders = array( 1001, 1002, 1003 );

for ( $i = 0; $i < count( $orders ); $i++ )
{
    $items = getItemsForOrderId( $orders[$i] ); // SQL query or something

    for ( $j = 0; $j < count( $items ); $j++ )
    {
        echo 'Order #' . $orders[$i] . ', Item ' . $items[$j];
    }
 }

Format to wanted output, and adjust to get the data correctly.

Update based on the changed question:

This could be one way to do it:

<?php
include 'cat-config.php';

$resultdata = mysql_query( 'SELECT * FROM Order LIMIT 10' );

echo "<table width=\"100%\" border=\"0\">
  <tr>
    <td>Order #</td>
    <td>Items</td>
    <td>Customer ID</td>
  </tr>";

while ( $orderRow = mysql_fetch_assoc( $resultData ) )
{
    echo "  <tr>\n    <td>" . $orderRow['orderid'] . "</td>\n";
    echo "    <td>";

    $itemData = mysql_query( 'SELECT * FROM Items WHERE orderid = ' . $orderRow['orderid'] );

    while ( $itemRow = mysql_fetch_assoc( $itemData ) )
    {
        echo $itemRow['items_code'] . ', ';
    }

    echo "</td>\n    <td>" . $orderRow['customer_id'] . "</td>\n  </tr>\n";
}
poke
how if i have more, data on table order?
key
Updated my answer in regards to your posted code.
poke
thank you,, thank you,,, arigato gozaimashhhhh,,,
key
Hehe, どう致しまして! :)
poke