Ratin Ratin - 27 days ago 22
MySQL Question

how to display table data horizontally

my problem is that the table data displaying vertically. How to display it horizontally?

php code:

public function showFoodOrder($menu) {
$keys = array_keys($menu);
for ($i = 0; $i < count($menu); $i++) {

$x = $i;
$n = ++ $x;
echo "row",$n, ".<br>";

foreach ($menu[$keys[$i]] as $key=>$value) {
echo "<table border=5>\n";
echo "<tr>\n";
echo "\t<td>".$value."</td>";
echo "</tr>\n";
echo "</table>\n";
}//end of foreach loop

}//end of for loop

}//end of showFoodOrder fnc


$menu variable information:

Array
(
[0] => Array
(
[food_name] => aaa
[food_desc] => bbb
[food_price] => ccc
)

[1] => Array
(
[food_name] => xxx
[food_desc] => yyy
[food_price] => zzz
)

)


current output:


  • row1

  • aaa

  • bbb

  • ccc

  • row2

  • xxx

  • yyy

  • zzz



desired output:


  • row1 aaa bbb ccc

  • row2 xxx yyy zzz


Answer

You're creating a new table for each item. You should start the table before the first loop. You should start a new row with <tr> in the outer loop, and then <td> in the inner loop.

public function showFoodOrder($menu) {
    echo "<table border=5>\n";
    foreach ($menu as $i => $item) {
        echo "<tr>\n";
        foreach ($item as $key=>$value) {
            echo "\t<td>".$value."</td>";
        }
        echo "</tr>\n";
    }
    echo "</table>\n";
}
Comments