无组织的表

时间:2015-02-04 01:03:52

标签: php mysql sql

我需要帮助。它们没有组织/整洁而且非常混乱 - > http://i.imgur.com/1LLpQ2G.png

桌子必须看起来整洁,易懂,不要像这样混淆 - > http://i.imgur.com/R3iqFzz.png(这就是表格的样子)

这是我迄今取得的成就(参见:OrderID 2 ) - > http://i.imgur.com/fj06EGB.png

以下是代码

<table>
            <tr>
                <th>Customer Name</th>
                <th>Customer Contact</th>
                <th>Customer Email</th>
                <th>Order ID</th>
                <th>Order Date</th>
                <th>Menu Name</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Total Amount</th>
                <th>Action</th>
            </tr>
        <?php
            $result = $mysqli->query("SELECT * FROM `order`");
            while($obj = mysqli_fetch_assoc($result)) {
                $orderDate = $obj['OrderDate'];
                $orderId = $obj['OrderID'];
                $totalAmount = $obj['OrderTotal'];
                $paymentStatus = $obj['PaymentStatus'];
                $customerId = $obj['CustomerID'];
                $res = $mysqli->query("SELECT CustomerName, CustomerContactNo, CustomerEmail FROM customer WHERE CustomerID=$customerId");
                if($row = mysqli_fetch_assoc($res)) {
                    $customerName = $row['CustomerName'];
                    $customerContactNo = $row['CustomerContactNo'];
                    $email = $row['CustomerEmail'];
                }

                $result1 = $mysqli->query("SELECT * FROM ordermenu WHERE OrderID = $orderId");
                while($obj1 = mysqli_fetch_assoc($result1)) {
                    $menuId = $obj1['MenuID'];
                    $menuQty = $obj1['menuQty'];
                    $result2 = $mysqli->query("SELECT * FROM menu WHERE MenuID = $menuId");
                    $obj2 = mysqli_fetch_assoc($result2);
                    $name = $obj2['MenuName'];
                    $price = $obj2['MenuPrice'];
                ?>


            <tr>
                <td><?php echo $customerName;?></td>
                <td><?php echo $customerContactNo;?></td>
                <td><?php echo $email;?></td>
                <td><?php echo $orderId;?></td>
                <td><?php echo $orderDate;?></td>
                <td><?php echo $name;?></td>
                <td>$<?php echo $price;?></td>
                <td><?php echo $menuQty;?></td>
                <td>$<?php echo $totalAmount;?></td>
                <td><a href="update_order.php?id=<?php echo $orderId;?>" color="green">Update</a></td>
            </tr>

                <?php } ?>
            <?php } ?>
        </table>

任何人都可以帮忙。

1 个答案:

答案 0 :(得分:1)

  1. 首先从db
  2. 获取客户
  3. Foreach客户通过customerID与ordermenu(订购菜单上的订单)订购订单并在菜单上加入(在ordermenu.MenuID = menu.MenuID上)

    <?php
    $customerQuery = $mysqli->query("SELECT CustomerID, CustomerName, CustomerContactNo, CustomerEmail FROM customer;");
    while($customer = mysqli_fetch_assoc($customerQuery)) {
        $customerId = $customer['CustomerID'];
        $orderQuery = $mysqli->query("SELECT * FROM `order` o LEFT JOIN ordermenu om ON o.OrderID = om.OrderID LEFT JOIN menu m ON m.MenuID = om.MenuID WHERE o.CustomerID = $customerId");
        while($order = mysqli_fetch_assoc($orderQuery)) {
            // do something with your customer and order record
            // show in table for example
        }
    }