PHP表中的嵌套循环
我是PHP新手,我正在尝试使用两个foreach创建一个表,但我没有得到我想要的输出PHP表中的嵌套循环,php,html,foreach,html-table,Php,Html,Foreach,Html Table,我是PHP新手,我正在尝试使用两个foreach创建一个表,但我没有得到我想要的输出 <html> <head> <title>Didier Test</title> </head> <body> <h1>Yesso!</h1> </body> <table border="1"> &l
<html>
<head>
<title>Didier Test</title>
</head>
<body>
<h1>Yesso!</h1>
</body>
<table border="1">
<tr>
<th>Books</th>
<th>Price</th>
</tr>
<tr>
<?php foreach($name as $item): ?>
<td><?=$item?></td>
<?php foreach($price as $item2): ?>
<td><?=$item2?></td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
</body>
</html>
迪迪埃试验
是的!
书
价格
我知道我的内心有问题,但我不知道如何纠正它
请让我知道。首先,在您输出表格之前,您正在关闭第7行的body标签
<body>
<h1>Yesso!</h1>
</body>
是的!
我也不知道为什么要对看似无关的数据进行嵌套循环。除此之外,我们还需要查看您的查询。由于您在第一个循环之前以
开始行,因此您需要在第一个循环之后以
结束行:
<tr>
<?php foreach($name as $item): ?>
<td><?=$item?></td>
<?php foreach($price as $item2): ?>
<td><?=$item2?></td>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
尝试更改速记php输出符号
您可能不会得到任何输出,因为您的php.ini已设置为不允许使用速记php'
哦,是的,就像Barmer和Revent提到的,您也有一些HTML标记嵌套问题。欢迎来到PHP的奇妙世界&祝你好运:)哪期?出什么问题了?您的循环可能无法工作,因为变量中没有数据。请使用
var\u dump($name,$price)
并向我们显示结果。这些必须是循环工作的数组。可能所有项目都有相同的价格,无论列是什么维度。但我怀疑扎克的回答是对的。
<?php
for($i = 0; $i< count($name); $i++) {
$item = $name[$i];
$item2 = $price[$i];
?>
<tr>
<td><?=$item?></td>
<td><?=$item2?></td>
</tr>
<?php
}
?>
<html>
<head>
<title>Didier Test</title>
</head>
<body>
<h1>Yesso!</h1>
</body>
<table border="1">
<tr>
<th>Books</th>
<th>Price</th>
</tr>
<tr>
<?php foreach($name as $item): ?>
<td><?php echo $item?></td>
<?php foreach($price as $item2): ?>
<td><?php echo $item2?></td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
</body>
</html>