我想以表格形式显示来自循环的数据

时间:2016-03-12 16:06:21

标签: php

我的PHP代码如下所示。

 <?php  
     for($i=0;$i<$max;$i++){
        $pid=$_SESSION['cart'][$i]['productid'];
        $sid=$_SESSION['cart'][$i]['STOREID'];
        $q=$_SESSION['cart'][$i]['qty']; 
        $Product=get_product_name($pid,$sid);
        $price=get_price($pid,$sid);
        echo $Product;
 ?>
      <table border='1'>
         <tr>
           <th>Ordered Product</th
           <th>Price</th>
           <th>Quantity</th>
        </tr>   
     </table>

我想在订购产品的表格中显示产品。

1 个答案:

答案 0 :(得分:0)

这取决于您愿意显示的格式。让我举个例子:

&#13;
&#13;
<table border='1'>
  <thead>
    <tr>
       <th>Ordered Product</th>
       <th>Price</th>
       <th>Quantity</th>
    </tr>
   </thead>
   <tbody>
     <?php 

        for($i=0;$i<$max;$i++){
          $pid=$_SESSION['cart'][$i]['productid'];
          $sid=$_SESSION['cart'][$i]['STOREID'];
          $q=$_SESSION['cart'][$i]['qty']; 
          $Product=get_product_name($pid,$sid);
          $price=get_price($pid,$sid);

      ?>

     <tr>
       <th><?php echo $product ?></th>
       <th><?php echo $price ?></th>
       <th><?php echo $q ?></th>
     </tr> 
 
     <?php } ?>

   </tbody>
</table> 
  
&#13;
&#13;
&#13;

我想提供的一个建议是避免在echo中编写HTML代码。如果您保留PHP和HTML代码,这是最好的。

希望这有帮助!

和平!的xD