2011-09-14 62 views
0

我已经为管理员用户在php中制定了一个基本的订单列表,用于检查登录用户放置的订单内容。订单列表/ while循环php问题

该脚本的目的是检索订单明细(商品,数量,价格)以及用户的名字和姓氏(其中'Order for:'是)。

下面的脚本确实可以检索订单(如果有多个订单,那么订单)以及它的/他们的物品,数量和价格。

但是,它不显示用户的姓名。

我知道问题是,我试图显示名称是在while循环之外,但我有点卡在它应该坐的位置。有什么建议么?代码如下:

<?php 
    $page_title = 'View Individual Order'; 
    include ('includes/header.html'); 

    // Check for a valid user ID, through GET or POST. 
    if ((isset($_GET['id'])) && (is_numeric($_GET['id']))) 
    { // Accessed through view_users.php 
    $id = $_GET['id']; 

    } elseif ((isset($_POST['id'])) && (is_numeric($_POST['id']))) 
    { // Form has been submitted. 
    $id = $_POST['id']; 
} else { // No valid ID, kill the script. 
    echo '<h1 id="mainhead">Page Error</h1> 
    <p class="error">This page has been accessed in error.</p><p><br /><br /></p>'; 
    include ('./includes/header.html'); 
    exit(); 
} 
?> 

<h1>Order Details</h1> 

<?php 
require_once ('database.php'); // Connect to the db. 

// Retrieve the user's, order and product information. 
$query = "SELECT us.users_id, us.users_sales_id, us.users_first_name, us.users_surname, us.users_dealer_name, 
      ord.order_id, ord.users_id, ord.total, ord.order_date, 
      oc.oc_id, oc.order_id, oc.products_id, oc.quantity, oc.price, 
      prd.products_id, prd.products_name, prd.price  
     FROM users AS us, orders AS ord, order_contents AS oc, products AS prd 
     WHERE ord.order_id=$id 
     AND us.users_id = ord.users_id 
     AND ord.order_id = oc.order_id 
     AND oc.products_id = prd.products_id  
     "; 

$result = mysql_query ($query) or die(mysql_error()); 

if (mysql_num_rows($result)) { // Valid user ID, show the form. 

    echo '<p>Order for:<strong>' . $row[2] . ' ' . $row[3] . ' </strong> </p> 
      <table border="0" style="font-size:11px;" cellspacing="1" cellpadding="5"> 
       <tr class="top"> 
       <td align="left"><b>Product</b></td> 
       <td align="center"><b>Price</b></td> 
       <td align="center"><b>Qty</b></td> 
       </tr>'; 

    $bg = '#dddddd'; // Set the background color. 

    while($row = mysql_fetch_array($result, MYSQL_NUM)) { // WHILE loop start 

    $bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced'); 

    echo  '<tr bgcolor="' . $bg . '">'; 

    echo  '<td align="left">' . $row[15] . '</td> 
      <td align="center">' . $row[13] . ' pts</td> 
      <td align="center">' . $row[12] . '</td> 
      </tr>'; 

    echo  '';   

       }// end of WHILE loop 

     echo '</table> 
      <p> Here:</p> 
      <br><br> 
      <p><a href="view-all-orders.php"> << Back to Orders</a></p> 
      <p>&nbsp;</p> 
      <p>&nbsp;</p> 
      <p>&nbsp;</p> 
      '; 

} else { // Not a valid user ID. 
    echo '<h1 id="mainhead">Page Error</h1> 
    <p class="error">This page has been accessed in error.</p><p><br /><br /></p>'; 
} 

mysql_close(); // Close the database connection. 
?> 

<p>Footer here</p> 

<?php 
include ('./includes/footer_admin_user.html'); // Include the HTML footer. 
?> 
+0

你真的应该学会正确缩进。以这种方式遍布各处阅读代码是非常困难的。 –

+0

我的歉意,下次会做:) – AdamMc

回答

0

你可以这样做的一种方法是首先获取行,然后使用do/while循环而不是仅仅基本的while循环。就像这样:

if (mysql_num_rows($result)) { // Valid user ID, show the form. 

    /*********** I added this line ***********/ 
    $row = mysql_fetch_array($result, MYSQL_NUM); 

    echo '<p>Order for:<strong>' . $row[2] . ' ' . $row[3] . ' </strong> </p> 
      <table border="0" style="font-size:11px;" cellspacing="1" cellpadding="5"> 
       <tr class="top"> 
       <td align="left"><b>Product</b></td> 
       <td align="center"><b>Price</b></td> 
       <td align="center"><b>Qty</b></td> 
       </tr>'; 

    $bg = '#dddddd'; // Set the background color. 

    /*********** I changed this from a while loop to a do-while loop ***********/ 
    do { // WHILE loop start 

    $bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced'); 

    echo  '<tr bgcolor="' . $bg . '">'; 

    echo  '<td align="left">' . $row[15] . '</td> 
      <td align="center">' . $row[13] . ' pts</td> 
      <td align="center">' . $row[12] . '</td> 
      </tr>'; 

    echo  '';   

    } while($row = mysql_fetch_array($result, MYSQL_NUM)); // end of WHILE loop 
+0

谢谢本,这工作太 – AdamMc

0

它看起来像问题是,当你想显示用户名:

echo '<p>Order for:<strong>'.$row[2].' '.$row[3] 

$row变量尚不存在。没有看到你的数据库或数据库查询的结果,我的猜测是用户的名字在它们的顺序中的每一项旁边重复,所以它可能就像启动WHILE循环一样简单,并检查你是否'已经打印出他们的名字了:

$lastUser = NULL; 
while($row = mysql_fetch_array($result, MYSQL_NUM)) { 
    if ($row[0] !== $lastUser) { 
     if (isset($lastUser)) { 
      // finish up the report for the previous user 
     } 
     // echo the stuff for the current user's name 
     $lastUser = $row[0]; 
    } 
    // go on echo-ing their order information 
} 
// after the while loop is over, 
// close up the last user's report 

但就像我说的,这只是一个猜测,可能完全关闭。

+0

感谢您的帮助@sdleihssirhc – AdamMc

0

的问题是你的努力mysql_fetch_array()之前访问$row[2]$row[3]。既然你已经echo“荷兰国际集团HTML标签,你为什么不‘缓冲’你的输出第一喜欢这个?:

while($row = mysql_fetch_array($result, MYSQL_NUM)) { 
    $bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced'); 

    $order = '<tr bgcolor="' . $bg . '"> 
       <td align="left">' . $row[15] . '</td> 
       <td align="center">' . $row[13] . ' pts</td> 
       <td align="center">' . $row[12] . '</td> 
      </tr>'; 
    $orders[$row[2] . " " . $row[3]][] .= $order; 
} 

又做了第二foreach循环的$orders

foreach($orders as $name => $orderList) 
{ 
    echo "Order for: $name"; 
    echo "<table ...>"; 
    foreach($orderList as $order) 
    { 
    echo $order; 
    } 
    echo "</table>"; 
} 
+0

感谢罗兰多,这工作 – AdamMc