how to select statement in another statement in while loop

youssef :

i want to display all orders from my database in desc order according to order_id

im using a while loop to display order_id, order_date, and client info.


while using this loop, its is repeating the order_id several times and putting each product in another order display.

for example order # 13 contains 3 products, so order # 13 is repeated 3 times

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

how this should work? where is my mistake and how should i fix it?

Omar Abbas :

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.

<?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 } ?>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=20485&siteId=1