dongshen9686 2016-03-29 07:03
浏览 35
已采纳

如何使用php使用for循环将数据打印到表中

I need to print the following data into table using for loop.I know there will be two explode.First for "|" and second for "," and after that it should be print.

PHP:

  $data=750ML XYZ,750ML ABC|280,30|60,20|16800,600|12.25,25.25|205800,15150
   for($i=0;$i<count($d);$i++)
    {  
    $d2[]=explode(",",$d[$i]);
echo "<tr>";
    //Suggest here
echo "</tr>"
    }

Expected Output:

Goods         Pkg      Avg     Qty     Rate    Total
750ML XYZ     280      60     16800  12.25    205800
750ML ABC      30      20       600  25.25    15150

I tried but it didnt work.I am confused.Please give some suggestion.Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doubeng3412 2016-03-29 07:25
    关注

    Try this:

    <?php
    
    $data= "750ML XYZ,750ML ABC|280,30|60,20|16800,600|12.25,25.25|205800,15150";
    
    $array = explode("|", $data);
    
    $final = array();
    
    foreach($array as $a) {
        $row = explode(",", $a);
        $final["first"][]  =  $row[0];
        $final["second"][] =  $row[1];
    } 
    ?>
    <table>
        <thead>
            <th>Goods</th>
            <th>Pkg</th>
            <th>Avg</th>
            <th>Qty</th>
            <th>Rate</th>
            <th>Total</th>
        </thead>
        <tbody>
            <?php foreach($final as $f) { ?>
                <tr>
                    <?php foreach($f as $v){ ?>
                        <td><?php echo $v; ?></td>
                    <?php } ?>
                </tr>
            <?php } ?>
        </tbody>
    </table>
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?