php - foreach 循环在函数中的位置

标签 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);
}

最佳答案

试试这个:

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

关于php - foreach 循环在函数中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15435555/

相关文章:

php - 如何在Codeigniter连接中使用Mysql查询?

arrays - 二维数组上的 numpy Logical.xor

php - 按 RAND() 排序

javascript - AJAX 上传仅显示多个文件上传中最后一个元素的进度

php - 如何从包含unix时间戳中日期字段的表中获取记录?

javascript - jquery/javascript : arrays

php - 以 PHP 形式发送数组值

php - 如何从一对多关系的mysql表中获取数据

php - 服务器聊天应用程序

MySQL 在 POINT 列上插入/更新