【问题标题】:php looping solutionsphp循环解决方案
【发布时间】:2012-07-11 11:33:28
【问题描述】:

我有以下代码:

<?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 的所有产品吗?

【问题讨论】:

  • 试试 print_r($products);在拳头 之前并检查你的 while 循环。它没有关闭
  • 乍一看,我发现你的 while 循环没有关闭。
  • 对不起,忘记关闭我的帖子
  • 你需要把右大括号放在 php 标签中
  • 验证并计算您的$manifest_query中的行数

标签: php mysql loops while-loop


【解决方案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
    }
} 
?>

【讨论】:

  • 这会打印两次订单,一个产品在一行中,然后在下一个产品中重复该行。无论如何将所有产品打印到一行中的订单?
  • 好吧,我已经向你展示了如何解决你的问题,如果我为你做所有事情,你就会变得懒惰!
【解决方案2】:

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

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签