php循环解决方案

时间:2012-07-11 11:33:28

标签: php mysql loops while-loop

我有以下代码:

<?php

$manifest_query = tep_db_query("select o.franchise_id, o.orders_id, o.customers_id,  o.delivery_name, o.delivery_street_address, o.delivery_city, o.delivery_postcode, o.delivery_state, o.customers_telephone, o.date_purchased from " . TABLE_ORDERS . " o, " . TABLE_ORDERS_TOTAL . " ot where o.franchise_id = '" . (int)$franchise_id . "' and o.orders_id = ot.orders_id and ot.class = 'ot_total' and o.orders_status = '5'");

while ($manifest = tep_db_fetch_array($manifest_query))
{
    $products_query = tep_db_query("select orders_products_id, orders_id, products_id, products_model, products_name, products_quantity from " . TABLE_ORDERS_PRODUCTS . " where orders_id = '" . (int)$manifest['orders_id'] . "'");
    $products = tep_db_fetch_array($products_query);
    ?>
    <tr>
    <td height="50" align="center"><?php echo $manifest['orders_id'] ;?></td>
    <td cellpadding="2"><?php echo $manifest['delivery_name'] .'<br> '. $manifest['delivery_street_address'] .'<br> '. $manifest['delivery_city'].'<br> '. $manifest['delivery_postcode'].'<br> '. $manifest['delivery_state'].'<br> '. $manifest['customers_telephone'] ;?></td>
    <td><?php echo $products['products_quantity'] . '&nbsp;x&nbsp;' . $products['products_name'] . '<br> ' . '&nbsp;&nbsp;'.$products['products_model'];?></td>
    <?php   
}
?>

$ products打印分配给第3列中订单ID的产品。但它只打印分配给订单的第一个产品,即使订单中分配了多个产品。任何人都知道如何打印order_id的所有产品?

2 个答案:

答案 0 :(得分:1)

你需要嵌套一个while循环。您只调用第一行,因为您只有一个循环。

<?php

 $manifest_query = tep_db_query("select o.franchise_id, o.orders_id, o.customers_id,  o.delivery_name, o.delivery_street_address, o.delivery_city, o.delivery_postcode, o.delivery_state, o.customers_telephone, o.date_purchased from " . TABLE_ORDERS . " o, " . TABLE_ORDERS_TOTAL . " ot where o.franchise_id = '" . (int)$franchise_id . "' and o.orders_id = ot.orders_id and ot.class = 'ot_total' and o.orders_status = '5'");

while ($manifest = tep_db_fetch_array($manifest_query)){

$products_query = tep_db_query("select orders_products_id, orders_id, products_id, products_model, products_name, products_quantity from " . TABLE_ORDERS_PRODUCTS . " where orders_id = '" . (int)$manifest['orders_id'] . "'");
    while($products = tep_db_fetch_array($products_query))


     ?>

      <tr>
      <td height="50" align="center"><?php echo $manifest['orders_id'] ;?></td>
      <td cellpadding="2"><?php echo $manifest['delivery_name'] .'<br> '. $manifest['delivery_street_address'] .'<br> '. $manifest['delivery_city'].'<br> '. $manifest['delivery_postcode'].'<br> '. $manifest['delivery_state'].'<br> '. $manifest['customers_telephone'] ;?></td>


      <td><?php echo $products['products_quantity'] . '&nbsp;x&nbsp;' . $products['products_name'] . '<br> ' . '&nbsp;&nbsp;'.$products['products_model'];?></td>



    <?php
    }
} 
?>

答案 1 :(得分:0)

在每个while循环中,您都会创建一个新的数据库查询,因此旧的数据库查询将被丢弃,这意味着$ product将只有第一个条目。在$ product的原始while循环中进行另一个while循环。