2013-03-15 122 views
0

我試圖從我的數據庫中拉取客戶的訂單,但是我目前只能顯示來自一個訂單的數據的代碼,但客戶可能做出多個訂單。正因爲如此,我知道我需要添加一個foreach循環,但是我對他們來說是新的,我不確定確切的位置以及如何將它用於我的代碼。我的$ ORDER_ID變量同時顯示$ ORDER_ID的,所以我覺得它需要去上面$ order_detail,所以對於每個ORDER_ID,顯示細節foreach循環的位置函數

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); 
} 
+0

寫下簡單的英語什麼功能你想從foreach循環中獲得。確定可能存在的任何先決條件(比如在循環遍歷之前獲得結果列表)。識別foreach循環的前提條件(如從函數返回)。然後查看您的代碼並確定一個滿足foreach循環的先決條件和後置條件的位置。 – atk 2013-03-15 15:02:45

+0

與你的代碼的安全性相關,你使用的是'mysql'而不是'mysqli'(或'PDO'),這是非常危險的。見[這個問題](http://stackoverflow.com/questions/8891443/when-should-i-use-mysqli-instead-of-mysql),以及[這個文檔](http://www.php .net/manual/en/mysqlinfo.api.choosing.php)和[本FAQ](http://www.php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated)。 – ajp15243 2013-03-15 15:04:17

回答

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