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

时间:2016-03-29 07:03:51

标签: php

我需要使用for循环将以下数据打印到表中。我知道会有两个爆炸。第一个用于" |"第二,","之后应该打印。

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>"
    }

预期产出:

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

我试过但它没有用。我很困惑。请给出一些建议。谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

<?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>

希望这有帮助。