-1

I'm trying to display the content from 3 different mysql tables, clients, orders. products with the html structure:

                 Product 1        Product 2      Product 3 .....
client 1       quantityOrder     quantityOrder        -
client 2            -            quantityOrder    quantityOrder
client 3        quantityOrder        -                 -

I'm using a multidimensional array, but somehow it does not work.

Tried to follow the instructions from this post but also no luck: PHP two dimensional array into HTML

Any idea?

Thanks Alex

Community
  • 1
  • 1
  • Welcome to StackOverflow. I don't see any problems in your code _because you haven't shown it..._ Please click "edit" and add your code (or the relevant part of it) to your question. What does "somehow it does not work" mean in this case? Nothing happens? The wrong thing happens? It _almost_ works but doesn't quite do what you want? – nnnnnn Sep 04 '12 at 22:48

1 Answers1

0

Do not use arrays at all. You have a bidimensional array, you can build a table directly:

SELECT client.id, client.name, product.name as productname, SUM(order.quantity) AS qty
    FROM client CROSS JOIN product
         LEFT JOIN order ON ( order.product_id = product.id AND order.client_id = client.id)
    GROUP BY client.id, product.id ORDER BY client.name, product.code;

This will output a cell for each client and product, possibly NULL if there is no JOIN between that (client, product) and an order.

Then you can just iterate the table in PHP (of course you might save it in a multidimensional array, using client and product IDs for keys):

print "<table>";
$customer_id = -1;
while($tuple = SQLFetchTuple($exec))
{
    if ($tuple['id'] != $customer_id)
    {
        if (-1 != $customer_id)
            print "</tr>"; // Close previous row
        print "<tr><td>{$tuple['name']</td>"; // Output row header
        $customer_id = $tuple['id'];
    }
    if ($tuple['qty'])
        print "<td>{$tuple['qty']</td>";
    else
        print "<td>-</td>";
}
print "</table>";
LSerni
  • 55,617
  • 10
  • 65
  • 107