简体   繁体   中英

Foreach php looping in html tables

Declare single dimensional array for the following: Code, Description, Quantity, & Unit Price using the following values: Code: A001 to A005 Description: Mouse, Keyboard, Monitor, Flash Disk, Hard Disk Unit Price: 100, 2500, 200, 300, 1500 Quantity: 5,5,5,5,5

Ive tried using the following codes but this returns an error

<html>
<body>
<?php

    $product = array ("Code"=> array("A001", "A002", "A003", "A004","A005"),
    "Description"=>array("Mouse", "Monitor", "Keyboard", "FlashDisk", "HardDisk"), 
    "Quantity"=>array("5", "5", "5", "5", "5"),
    "UnitPrice"=>array("100", "2500","200","300","1500"));
?>
<table>
    <tr>
        <th align= center style="font-size: 20px;" colspan="5">COMPUTER STORE</th>
    </tr>
    <tr>
         <td align= center colspan="5">Full Name</td>
    </tr>
    <tr>
        <td height ="20" colspan="5"> </td>
    </tr>
    <tr>
        <td colspan="5"> </td>
    </tr>
    <tr>
        <th align= center style="font-size: 20px;" colspan="5">PRODUCT INFORMATION</th>
    </tr>
    <tr>
        <td height ="20" colspan="5"> </td>
    </tr>
    <tr>
         <th>Code</th>
         <th>Description</th>
         <th>Unit Price</th>enter code here
         <th>Quantity</th>
    </tr>

<?php

foreach ($product as $i_names => $i_values) {

    echo "<tr>"
            echo "<td>$i_values[0]</td>"
            ."<td>$i_values[1]</td>"
            ."<td>$i_values[2]</td>"
            ."<td>$i_values[3]</td>"
            ."<td>$i_values[4]</td>"
        ."</tr>";
}

?>

</table>
</body>
</html>

I expect a table that displays array details in each columns but it displays the details by rows.

You've have some unnecessary string concatenation with double quotes . Just do this way in single line-

foreach ($product as $i_names => $i_values) {
       echo "<tr><td>$i_values[0]</td><td>$i_values[1]</td><td>$i_values[2]</td><td>$i_values[3]</td><td>$i_values[4]</td></tr>";
}

WORKING DEMO: http://main.xfiddle.com/eb798d87/foreach.php

echo "<tr>"后面缺少分号,应该是echo "<tr>";

@Ryan first of all I think you should rethink your data structure, because it doesn't make much sense...

This has nothing to do you making the echo in one line as @Always Sunny suggested! Actually the Working Demo he provided is not working!

But moving forward, this may not be an elegant solution but will handle it:

echo "<tr>";
 foreach ($product as $i_names => $i_values) {
           echo "<td>$i_values[0]</td>";  
}
 echo "</tr>";

 echo "<tr>";
 foreach ($product as $i_names => $value) {
           echo "<td>$i_values[1]</td>";          
}
 echo "</tr>";


 echo "<tr>";
 foreach ($product as $i_names => $value) {
           echo "<td>$i_values[2]</td>";          
}
 echo "</tr>";


 echo "<tr>";
 foreach ($product as $i_names => $value) {
           echo "<td>$i_values[3]</td>";          
}
 echo "</tr>";


 echo "<tr>";
 foreach ($product as $i_names => $value) {
           echo "<td>$i_values[4]</td>";          
}
 echo "</tr>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM