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

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

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.