0

我正在尝试从我的数据库中提取客户订单,但是我目前拥有的代码仅显示来自一个订单的数据,但客户可能会下多个订单。因此,我知道我需要添加一个 foreach 循环,但是我对它们很陌生,并且不确定确切的位置以及如何在我的代码中使用它。我的 $order_id 变量同时显示了 $order_id,所以我认为它需要高于 $order_detail,因此对于每个 order_id,显示详细信息

function viewOrders($session_mem_id){
//This block grabs the orders
$order_list = "";
//Selecting all the orders in the table from that member
$sql = mysql_query("SELECT `order_id` FROM `transactions` WHERE `mem_id` = $session_mem_id") or die(mysql_error());

$orderCount = mysql_num_rows($sql);
if ($orderCount > 0) {
    while($row = mysql_fetch_array($sql)){
        //creating variables from the information
        $order_id = $row["order_id"];

    }

    $order_details = mysql_query("SELECT * FROM `transactionDetails` WHERE `order_id` = $order_id") or die(mysql_error());
    $orderDetailsCount = mysql_num_rows($order_details);
    while($row = mysql_fetch_array($order_details)){
        //creating variables from the information
        $order_product_id = $row["Product_ID"];
        $order_product_price = $row["Price"];
        $order_product_quantity = $row["Quantity"];
        $order_List .='<tr>';
        $order_List .='<td>' . $order_id .'</td>';
        //$order_List .='<td><a href="product.php?id=' . $order_product_id . '">' . $product_name . '</a></td>';
        $order_List .='<td>£' . $order_product_price .'</td>';
        $order_List .='<td>' .$order_product_quantity .'</td>';
        $order_List .='</tr>';
        }
    } else {
    //displaying a message if no products were found
    $order_list = "You have no orders to display";
}
print_r($order_List);
}
4

1 回答 1

1

尝试这个:

<?php
function viewOrders($session_mem_id)
{
    //This block grabs the orders
    $order_list = "";
    //Selecting all the orders in the table from that member
    $sql = mysql_query("SELECT `order_id` FROM `transactions` WHERE `mem_id` = $session_mem_id") or die(mysql_error());

    while ($transactions = mysql_fetch_array($sql)) {
        //creating variables from the information
        $order_id = $transactions["order_id"];

        $order_details = mysql_query("SELECT * FROM `transactionDetails` WHERE `order_id` = $order_id") or die(mysql_error());
        $orderDetailsCount = mysql_num_rows($order_details);
        while ($row = mysql_fetch_array($order_details)) {
            //creating variables from the information
            $order_product_id = $row["Product_ID"];
            $order_product_price = $row["Price"];
            $order_product_quantity = $row["Quantity"];
            $order_list .= '<tr>';
            $order_list .= '<td>' . $order_id . '</td>';
            //$order_list .='<td><a href="product.php?id=' . $order_product_id . '">' . $product_name . '</a></td>';
            $order_list .= '<td>£' . $order_product_price . '</td>';
            $order_list .= '<td>' . $order_product_quantity . '</td>';
            $order_list .= '</tr>';
        }
    }

    if (count($order_list)==0) {
        $order_list = "You have no orders to display";
    }

    print_r($order_list);
}
于 2013-03-15T15:06:51.593 回答