2011-09-14 13 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

ための第二foreachループを行う?: 『バッファ』はありません正しくインデントする方法を学ぶ。このような場所にスペースを置いてコードを読むのは非常に難しいです。 –

+0

私の謝罪は、次回に行うでしょう:) – AdamMc

答えて

0

あなたができることの1つは、最初に行をつかんでから、ちょうど基本whileループの代わりにdo/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]にアクセスしようとしたことです。すでにHTMLタグをINGのecho」しているので、なぜあなたは最初にこのようなあなたの出力はあなたが本当にすべき

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

が続い$orders

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

ありがとうRolando、これは働いた – AdamMc