Foreach php变量值创建while循环

时间:2010-12-13 09:35:30

标签: php mysql

我有一张这样的订单,每个客户都有多个订单:

Customer   ponumber quantity ..

customer1   234345     56
customer2   343454     34
customer1   w34234     54
customer5   332423     54

我正在尝试遍历每个DISTINCT客户并使用以下代码打印相应的订单。

<?php

include("php/database_connect.php");

  $query = mysql_query("SELECT DISTINCT customer FROM orders");
  $customer = mysql_fetch_array($query);

foreach($customer as $customers)
{

echo ' <li><h1>'. $customers . '</h1></li>';

$result = mysql_query("SELECT * FROM orders WHERE misc='new' AND customer='$customers'       ORDER BY columnpos ASC ");

while($row = mysql_fetch_array($result))

         {
         echo'  

         <li id="id_' . $row['id'] . '">
         <div class="card">

         <table>
         <tr>
         <td>' . $row['customer'] . '</td>
         <tr>
         <td>P/O: ' . $row['ponumber'] . '</td>
         </tr>
         <tr>
         <td>' . $row['partnumber'] . '</td>
         </tr>
         <tr>
         <td><b>' . $row['quantity'] . '</b> x ' . $row['foil'] . '</td>
         </tr>
         <tr>
         <td>' . $row['daterequired'] . '</td>
         </tr>
         <tr>
         <td>' . $row['prep'] . '</td>
         </tr>
         </table>
        <input class="hiddenid" type="hidden" value="' . $row['id'] . '" />
         <input class="hiddenquantity" type="hidden" value="' .     $row['quantity'] . '" />
         <input class="hiddenpartnumber" type="hidden" value="' .   $row['partnumber'] . '" />
         <input class="hiddenfoil" type="hidden" value="' . $row['foil'] . '" />
         </div>
         </li>


         ';
         }
}
?>

但是上面的代码目前正在打印:

customer1 234345
customer1 w34234

customer1 234345
customer1 w34234

这似乎很奇怪......它没有通过每个客户。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:3)

只进行一次数据库查询(性能更佳),但在循环之前处理结果:

$result = mysql_query("SELECT * FROM orders WHERE misc='new' ORDER BY customer ASC ");

$ordersByCustomer = array(); // nested array. 1. level: customers, 2. level: orders

while(($row = mysql_fetch_assoc($result))) {
    if(!array_key_exists($row['customer'], $ordersByCustomer)) {
        $ordersByCustomer[$row['customer']] = array();
    }
    $ordersByCustomer[$row['customer']][] = $row;
}

foreach($ordersByCustomer as $customer=>$orders) {
    echo ' <li><h1>'. $customer . '</h1></li>';
    foreach($orders as $order) {
        // rest of HTML code
    }
}

我还鼓励您使用alternative syntax for control structures来更好地分离HTML和PHP:

<?php foreach($ordersByCustomer as $customer=>$orders): ?>
    <li><h1><?php echo $customer; ?></h1></li>
    <?php foreach($orders as $order): ?>
        <li id="id_<?php echo $row['id'];?>">
            <div class="card">
                <table>
                    <tr><td><?php echo $order['customer']; ?></td></tr>
                    <!-- and so forth -->
    <?php endforeach; ?>
<?php endforeach; ?>

顺便说一句,您没有生成有效的HTML。 li元素必须包含在ulol元素中。

答案 1 :(得分:2)

使用

while($customer = mysql_fetch_array($query){

而不是

$customer = mysql_fetch_array($query);

foreach($customer as $customers)
{