Php 函数中foreach循环的位置

Php 函数中foreach循环的位置,php,mysql,arrays,Php,Mysql,Arrays,我正在尝试从我的数据库中提取客户订单,但是我当前的代码只显示一个订单的数据,但是客户可能会下不止一个订单。正因为如此,我知道我需要添加一个foreach循环,但是我对它们是新手,不确定确切的位置以及如何在代码中使用它。我的$order\u id变量同时显示$order\u id,因此我认为它需要高于$order\u detail,因此对于每个order\u id,显示详细信息 function viewOrders($session_mem_id){ //This block grabs the

我正在尝试从我的数据库中提取客户订单,但是我当前的代码只显示一个订单的数据,但是客户可能会下不止一个订单。正因为如此,我知道我需要添加一个foreach循环,但是我对它们是新手,不确定确切的位置以及如何在代码中使用它。我的$order\u id变量同时显示$order\u id,因此我认为它需要高于$order\u detail,因此对于每个order\u 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);
}
函数视图顺序($session\u mem\u id){
//这一块抓住了订单
$order_list=“”;
//从该成员选择表中的所有订单
$sql=mysql\u query(“从`transactions`中选择`order\u id`,其中`mem\u id`=$session\u mem\u id')或die(mysql\u error());
$orderCount=mysql\u num\u行($sql);
如果($orderCount>0){
while($row=mysql\u fetch\u数组($sql)){
//从信息中创建变量
$order_id=$row[“order_id”];
}
$order\u details=mysql\u查询(“从`transactionDetails`中选择*,其中`order\u id`=$order\u id')或die(mysql\u error());
$orderDetailsCount=mysql\u num\u行($order\u details);
while($row=mysql\u fetch\u数组($order\u details)){
//从信息中创建变量
$order_product_id=$row[“product_id”];
$order_product_price=$row[“price”];
$order\u product\u quantity=$row[“quantity”];
$order_List.='';
$order_List.=''.$order_id';
//$order_List.='';
$order_List.='5英镑'。$order_product_价格';
$order\U List.=''.$order\U product\U QUOTE';
$order_List.='';
}
}否则{
//如果未找到任何产品,则显示消息
$order\u 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);
}

用简单的英语写下您希望从foreach循环中获得的功能。确定可能存在的任何先决条件(例如,在循环遍历之前拥有结果列表)。识别foreach循环的先决条件(如从函数返回)。然后查看您的代码并确定一个满足foreach循环的先决条件和后决条件的位置。与代码的安全性相关,您使用的是
mysql
API,而不是
mysqli
(或
PDO
),这相当危险。看,以及和。