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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?