因此,在下面的查詢中,我透過 order_id 連接兩個表並顯示 user_orders 表中的所有值。
根據下圖,我嘗試僅顯示與 order_manager 表相符的 order_Id 行。
public function getUserOrder(){ $sql = "SELECT user_orders.order_id, user_orders.title, user_orders.price, user_orders.quantity FROM order_manager JOIN user_orders ON order_manager.order_id = user_orders.order_id;"; $stmt = $this->connect()->prepare($sql); $stmt->execute(); while ($result = $stmt->fetchAll()){ return $result; } }
我嘗試使用一個 if 語句,該語句似乎可以執行某些操作,但它給出的值與反向訂單 ID 不符。
<div class="container mt-5"> <?php $artworks = new Artworks(); ?> <div class="row"> <div class="col-lg-12"> <table class="table table-dark"> <thead> <tr> <th scope="col">Order ID</th> <th scope="col">Full Name</th> <th scope="col">Phone</th> <th scope="col">Address</th> <th scope="col">Orders</th> </tr> </thead> <?php $artworks->getOrder(); foreach ($artworks->getOrder() as $art) { echo "<tbody> <tr> <td>$art[order_id]</td> <td> $art[full_name]</td> <td> $art[phone] </td> <td>$art[address]</td> <td> <table class= 'tale text-center table-dark'> <thead> <tr> <th scope='col'>Order ID</th> <th scope='col'>title</th> <th scope='col'>price</th> <th scope='col'>Quantity</th> </tr> <thead> <tbody> <tr>"; $artworks->getUserOrder(); foreach ($artworks->getUserOrder() as $order) { if ($order['order_id'] == $art['order_id']) { echo "<td>$order[order_id]</td>"; } echo " <td>$order[title]</td> <td>$order[price]</td> <td>$order[quantity]</td> </tr>"; } echo " </tbody> </table> </td> </tr> "; } ?> </tbody> </table> </div> </div> </div>
這是一張圖像來幫助解釋所需的輸出
解決了將 td 項目移到 if 語句中的問題。我猜這基本上是正確的。