dongshen9686
2016-03-29 07:03 阅读 24
已采纳

如何使用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 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.

    点赞 评论 复制链接分享

相关推荐