php - 无组织的表

标签 php mysql sql

我需要 table 方面的帮助。它们没有组织/整齐并且非常困惑 -> 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. 首先从数据库获取客户
  2. 对于每个客户,按 customerID 与 ordermenu(在 ordermenu.OrderID = order.OrderID)连接并与 menu(在 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
        }
    }
    

关于php - 无组织的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28311728/

相关文章:

mysql - mysql中2列按月和年排序

mysql - 根据 OR 子句条件查找 SQL 中的重复项

php - 错误类未捕获引发的错误

php - 将 01、02、03、04 .. 09 转换为 1,2,3,4 ... 9

mysql - mysql 和 php 中的未读聊天消息

php - 我正在使用 PHP ODBC 写入 Access 数据库,但它不会写入数据库

c# - 在数据库中存储和解析 bool 表达式

php - 检查 $_SERVER ['REQUEST_METHOD' ] == 'POST' 的原因?

php - 在 PHP 中将字符串转换为定义的常量

mySQL 在本地主机上出现错误,但在远程主机上却没有