函数中foreach循环的位置

时间:2013-03-15 14:58:30

标签: php mysql arrays

我正在尝试从我的数据库中提取客户订单,但是我目前的代码只显示来自一个订单的数据,但客户可能会生成多个订单。因此,我知道我需要添加一个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);
}

1 个答案:

答案 0 :(得分: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);
}