0

I am trying to pull customers orders from my database, however the code I currently have only shows data from one order, but the customer may make more than one order. Because of this I know I need to add a foreach loop, however I am new to them and am unsure of the exact location and how to use it with my code. My $order_id variable shows both $order_id's so I think it needs to go above $order_detail, so for each order_id, display the details

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);
}
jhetheringt7
  • 191
  • 1
  • 1
  • 7
  • Write down in plain english what functionality you want to get from the foreach loop. Identify any prerequisites that might exist (like having the list of results before looping through them). Identify anything that the foreach loop is a prerequisite of (like returning from the function). Then look at your code and identify a location that meets both the prerequisites and post-conditions of the foreach loop. – atk Mar 15 '13 at 15:02
  • Related to the security of your code, you are using the `mysql` API instead of `mysqli` (or `PDO`), which is rather dangerous. See [this question](http://stackoverflow.com/questions/8891443/when-should-i-use-mysqli-instead-of-mysql), as well as [this doc](http://www.php.net/manual/en/mysqlinfo.api.choosing.php) and [this FAQ](http://www.php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated). – ajp15243 Mar 15 '13 at 15:04

1 Answers1

1

Try this:

<?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);
}
BetaRide
  • 16,207
  • 29
  • 99
  • 177