Ratin
Ratin

Reputation: 127

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:

desired output:

Upvotes: 1

Views: 1563

Answers (1)

Barmar
Barmar

Reputation: 780798

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

Upvotes: 2

Related Questions