Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Table is not organized

So I have a table where customer can see his order history but the problem is it is not organized/neat. (see: http://i.imgur.com/4DyAr3N.png) the data is redundant. Someone help me please


<div class="tabcontents">
        <table style = "width:100%">
        <?php
            $result = $mysqli->query("SELECT * FROM `order` WHERE CustomerID='".$customerID."'");
            while($obj = mysqli_fetch_assoc($result)) {
                $orderDate = $obj['OrderDate'];
                $orderId = $obj['OrderID'];
                $totalAmount = $obj['OrderTotal'];
                $paymentStatus = $obj['PaymentStatus'];
                $preparationStatus = $obj['PreparationStatus'];
            ?>

                <tr>
                    <th>Order ID</th>
                    <th>Date of Order</th>
                    <th>Menu Name</th>
                    <th>Price</th>
                    <th>Quantity</th>
                    <th>Total Amount</th>
                    <th>Preparation Status</th>
                    <th>Payment Status</th>
                </tr>
                <?php
                                $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 $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><?php echo $preparationStatus;?></td>
                    <td><?php echo $paymentStatus;?></td>
                </tr>
                <?php } } ?>
        </table>
    </div>

1 Answer

Andrew Shook
Andrew Shook
31,709 Points

Samin, have you thought about making it a two step process. You could create one page "Orders" that shows all the orders, cost, and their statuses. Then you could have an "Order" page that shows all the details for just one order ( meaning the products that make up that one order). This would remove some of the redundancy have right now.

Hi. Yes, I have thought about that but I thought it would be inconvenient for the customer. I just did some modifications to the query and this is (http://i.imgur.com/gy1szCy.png) what it looks like now.


<div class="tabcontents">
        <table style = "width:100%">
        <tr>
                <th>Order ID</th>
                <th>Date of Order</th>
                <th>Menu Name</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Total Amount</th>
                <th>Preparation Status</th>
                <th>Payment Status</th>
            </tr>
                <?php
                    $result = $mysqli->query("SELECT * FROM `order` WHERE CustomerID='".$customerID."'");
                    while($obj = mysqli_fetch_assoc($result)) {
                        $orderDate = $obj['OrderDate'];
                        $orderId = $obj['OrderID'];
                        $totalAmount = $obj['OrderTotal'];
                        $paymentStatus = $obj['PaymentStatus'];
                        $preparationStatus = $obj['PreparationStatus'];
                    ?>
                <?php
                    $result1 = $mysqli->query("SELECT * FROM ordermenu WHERE OrderID = $orderId");
                    $i = 1;
                    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>
                <?php
                    if ($i >1) {
                        $ordID = "";
                        $ordDate="";
                        $totalAmt="";
                        $preparatnStatus="";
                        $paymntStatus="";
                    } else {
                        $ordID = $orderId;
                        $ordDate = $orderDate;
                        $totalAmt = $totalAmount;
                        $paymntStatus = $paymentStatus;
                        $preparatnStatus = $preparationStatus;
                    }
                    $i++;
                ?>
                    <td><?php echo $ordID;?></td>
                    <td><?php echo $ordDate;?></td>
                    <td><?php echo $name;?></td>
                    <td>$<?php echo $price;?></td>
                    <td><?php echo $menuQty;?></td>
                    <td>$<?php echo $totalAmt;?></td>
                    <td><?php echo $preparatnStatus;?></td>
                    <td><?php echo $paymntStatus;?></td>
                </tr>
                <?php } } ?>
        </table>
    </div>

Help me