我的问题是表格数据垂直显示。如何水平显示?
php代码:
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变量信息:
Array
(
[0] => Array
(
[food_name] => aaa
[food_desc] => bbb
[food_price] => ccc
)
[1] => Array
(
[food_name] => xxx
[food_desc] => yyy
[food_price] => zzz
)
)
当前输出:
期望的输出:
答案 0 :(得分:3)
您正在为每个项目创建一个新表格。你应该在第一个循环之前启动表。你应该在外部循环中用<tr>
开始一个新行,然后在内部循环中开始<td>
。
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";
}