doulun1666 2011-09-14 01:51
浏览 91
已采纳

订单列表/ while循环php问题

I have put together a basic order list for admin users in php for checking order contents placed by logged in users.

The aim of this script is to retrieve the order details (item, quantity, price) as well as the user’s first name and surname (where ‘Order for:’ is).

The script below does everything ok in that it retrieves the order (and orders if there are more than one) and it’s/their item, quantity and price.

However, it doesn’t display the user’s name and surname.

I know the problem is that where I am trying to display the name is outside the while loop but Im a little stuck in where it should sit. Any suggestions? Code is below:

    <?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.
?>
  • 写回答

3条回答 默认 最新

  • dongtaijue1578 2011-09-14 01:59
    关注

    One way you could do it is grab the row first, and then use a do/while loop instead of just a basic while loop. Like this:

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法