我是PHP新手,我正在尝试使用两个foreach创建一个表,我没有得到我想要的输出。
<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>
开始一行,因此您需要以</tr>
结束该行:
<tr>
<?php foreach($name as $item): ?>
<td><?=$item?></td>
<?php foreach($price as $item2): ?>
<td><?=$item2?></td>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
<?php
for($i = 0; $i< count($name); $i++) {
$item = $name[$i];
$item2 = $price[$i];
?>
<tr>
<td><?=$item?></td>
<td><?=$item2?></td>
</tr>
<?php
}
?>
尝试将您的php输出符号<?=
更改为完整的<?php echo
<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>
你可能没有得到任何输出,因为你的php.ini设置了不允许简写php '
哦,是的,就像Barmer和Revent提到的,你也有一些HTML标签嵌套问题。欢迎来到美妙的PHP世界。祝你好运!