简体   繁体   English

如何在while循环中的另一个语句中选择语句

[英]how to select statement in another statement in while loop

i want to display all orders from my database in desc order according to order_id我想根据 order_id 以 desc 顺序显示我数据库中的所有订单

im using a while loop to display order_id, order_date, and client info.我使用 while 循环来显示 order_id、order_date 和客户端信息。


while using this loop, its is repeating the order_id several times and putting each product in another order display.使用此循环时,它会多次重复 order_id 并将每个产品放在另一个订单显示中。

for example order # 13 contains 3 products, so order # 13 is repeated 3 times例如订单 #13 包含 3 个产品,因此订单 #13 重复 3 次

this is my code这是我的代码

<?php       
include '../php_action/db_connect.php';      
$sql="SELECT * FROM orders left join order_item on orders.order_id=order_item.order_id left join products on products.product_id=order_item.product_id left join category on products.category_id=category.category_id order by orders.order_id desc";
$result=$connect->query($sql); 
while($row=$result->fetch_array())
{  
?>            
<form action="" class="checkout-form" method="POST">
    <div class="row">
        <div class="col-lg-12">
            <div class="place-order">
                <h4>Order number # <?php echo $row['order_id']?> Order Date : <?php echo $row['order_date']?></h4>
                <div class="order-total">
                    <ul class="order-table">
                        <li>Product <span>Total</span></li>
                        <li class="fw-normal"><?php echo $row["product_name"].'  - '.$row['category_name'].'<br>'. $row['product_weight'].'KG ' .' X  ' .number_format($row["product_price"],0).' LBP'?> <span><?php echo 'LBP '.number_format($row['total'],0)?></span></li>
                        <li class="fw-normal">Subtotal <span><?php echo 'LBP '.number_format($row['subtotal'],0)?></span></li>
                        <li class="total-price" style="font-size:1.3em">Total <span><?php echo 'LBP '.number_format($row['grandtotal'],0)?> </span></li>
                    </ul>
                    <div class="payment-check">
                        <div class="pc-item">
                            <label for="pc-check">
                            <?php 
                            echo 'ORDER DATE: '.$row['order_date'];
                            echo '<br>';
                            echo 'FIRST NAME: '.$row['client_firstname'];
                            echo '<br>';
                            echo 'LAST NAME: '.$row['client_lastname'];
                            echo '<br>';
                            echo 'PHONE: '.$row['client_phone'];
                            echo '<br>';
                            echo 'CITY: '.$row['client_city'];
                            echo '<br>';
                            echo 'ADDRESS: '.$row['client_address'];
                            echo '<br>';
                            echo 'BLDG: '.$row['client_bldg'];
                            echo '<br>';
                            echo 'FLOOR NO: '.$row['client_floor'];
                            echo '<br>';
                            echo '<br>';
                            echo 'PAYMENT METHOD: Cash On Delivery';
                            ?>    
                            </label>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<?php } ?>

i want to display the order # and order date, and inside the order table i want to display all products from table order_item where order_item.order_id = order.order_id我想显示订单号和订单日期,在订单表中我想显示表 order_item 中的所有产品,其中 order_item.order_id = order.order_id

how this should work?这应该如何工作? where is my mistake and how should i fix it?我的错误在哪里,我该如何解决?

You can achieve this by many different approaches, here is one very simple approach which you can use to Display the required results.您可以通过许多不同的方法来实现这一点,这里是一种非常简单的方法,您可以使用它来显示所需的结果。

You can also set the desired format for the values such as product_price , and order_date etc.您还可以为product_priceorder_date等值设置所需的格式。

<?php       
include '../php_action/db_connect.php';      
$sql="SELECT * FROM orders left join order_item on orders.order_id=order_item.order_id left join products on products.product_id=order_item.product_id left join category on products.category_id=category.category_id order by orders.order_id desc";
$result=$connect->query($sql);

$orders = [];

while($row=$result->fetch_array())
{
    $order_id = $row['order_id'];
    if(!in_array($order_id, array_keys($orders))){
        //Save Order details once  
        $orders[$order_id]['order_id'] = $row['order_id'];
        $orders[$order_id]['total'] = $row['total'];
        $orders[$order_id]['subtotal'] = $row['subtotal'];
        $orders[$order_id]['grandtotal'] = $row['grandtotal'];
        $orders[$order_id]['client_firstname'] = $row['client_firstname'];
        $orders[$order_id]['client_lastname'] = $row['client_lastname'];
        $orders[$order_id]['client_phone'] = $row['client_phone'];
        $orders[$order_id]['client_city'] = $row['client_city'];
        $orders[$order_id]['client_address'] = $row['client_address'];
        $orders[$order_id]['client_bldg'] = $row['client_bldg'];
        $orders[$order_id]['client_floor'] = $row['client_floor'];
    }
    //Push Products into the Order
    $orders[$order_id]['products'][] = [
        'product_name'=> $row['product_name'],
        'category_name'=> $row['category_name'],
        'product_weight' => $row['product_weight'],
        'product_price' => $row['product_price'],
        'total' => $row['total'],
        'qty' => 2,
    ];
}

foreach($orders as $row){
?>            
<form action="" class="checkout-form" method="POST">
    <div class="row">
        <div class="col-lg-12">
            <div class="place-order">
                <h4>Order number # <?php echo $row['order_id']?> Order Date : <?php echo $row['order_date']?></h4>
                <div class="order-total">
                    <ul class="order-table">
                        <!-- Loop through all the products for the current order -->
                        <?php foreach($row['products'] as $product){ ?>
                            <li>Product <span>Total</span></li>
                            <li class="fw-normal"><?php echo $product["product_name"].'  - '.$product['category_name'].'<br>'. $product['product_weight'].'KG ' .' X  ' .number_format($product["product_price"],0).' LBP'?> <span><?php echo 'LBP '.number_format($product['total'],0)?></span></li>
                        <?php } ?>                        
                            <li class="fw-normal">Subtotal <span><?php echo 'LBP '.number_format($row['subtotal'],0)?></span></li>
                            <li class="total-price" style="font-size:1.3em">Total <span><?php echo 'LBP '.number_format($row['grandtotal'],0)?> </span></li>
                    </ul>
                    <div class="payment-check">
                        <div class="pc-item">
                            <label for="pc-check">
                            <?php 
                            echo 'ORDER DATE: '.$row['order_date'];
                            echo '<br>';
                            echo 'FIRST NAME: '.$row['client_firstname'];
                            echo '<br>';
                            echo 'LAST NAME: '.$row['client_lastname'];
                            echo '<br>';
                            echo 'PHONE: '.$row['client_phone'];
                            echo '<br>';
                            echo 'CITY: '.$row['client_city'];
                            echo '<br>';
                            echo 'ADDRESS: '.$row['client_address'];
                            echo '<br>';
                            echo 'BLDG: '.$row['client_bldg'];
                            echo '<br>';
                            echo 'FLOOR NO: '.$row['client_floor'];
                            echo '<br>';
                            echo '<br>';
                            echo 'PAYMENT METHOD: Cash On Delivery';
                            ?>    
                            </label>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<?php } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM