0

我有以下代码:

<?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 在第三列打印分配给订单 ID 的产品。但它只打印分配给订单的第一个产品,即使有多个产品分配给订单。有人知道如何打印 order_id 的所有产品吗?

4

2 回答 2

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
    }
} 
?>
于 2012-07-11T11:46:57.267 回答
0

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

于 2012-07-11T11:47:05.467 回答