在SELECT中显示所有选定数据条目时出现问题。现在唯一显示的是recipe_direct数据。每个表数据应在一行中列出名称,个人资料,直接,身份验证名称和身份验证电子邮件。
<div class="starter-template">
<h1>Recipes</h1>
</div>
<?php
try
{
$sql = 'SELECT recipe_id, recipe_name, recipe_ingred, recipe_direct, author_name, author_email FROM recipes';
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching recipes: ' . $e->getMessage();
}
while ($row = $result->fetch())
{
$recipes[$row['recipe_id']] = $row;
}
?>
<p><a href="addrecipe.php">Add a Recipe</a></p>
<?php foreach ($recipes as $id => $recipe): ?>
<blockquote>
<table class="table table-striped">
<tr>
<td><?php echo $recipe['recipe_name']; ?></td>
<td><?php echo $recipe['recipe_ingred']; ?></td>
<td><?php echo $recipe['recipe_direct']; ?></td>
<td><?php echo $recipe['author_name']; ?></td>
<td><?php echo $recipe['author_email']; ?></td>
</tr>
<?php htmlout($recipe_html); ?> |
<a href="updaterecipe.php?id=<?php echo $id; ?>">edit</a> |
<a href="deleterecipe.php?id=<?php echo $id; ?>">delete</a>
</table>
</blockquote>
<?php endforeach; ?>
答案 0 :(得分:1)
每次为其分配值时都会覆盖$recipe_html
为什么不回显表行:
<table>
<?php foreach ($recipes as $id => $recipe): ?>
<tr>
<td><?= $recipe['recipe_name']; ?></td>
<td><?= $recipe['recipe_ingred']; ?></td>
<td><?= $recipe['recipe_direct']; ?></td>
<td><?= $recipe['author_name']; ?></td>
<td><?= $recipe['author_email']; ?></td>
</tr>
<?php endforeach; ?>
</table>
EdIt 作为@Fred -ii-状态,表标签应该在循环之外
答案 1 :(得分:0)
在foreach
之前,所有内容都是正确的。
<blockquote>
<table class="table table-striped">
<?php foreach ($recipes as $id => $recipe): ?>
<tr>
<td><?php $recipe_html = $recipe['recipe_name']; ?></td>
<td><?php $recipe_html = $recipe['recipe_ingred']; ?></td>
<td><?php $recipe_html = $recipe['recipe_direct']; ?></td>
<td><?php $recipe['author_name']; ?></td>
<td><?php $recipe['author_email']; ?></td>
</tr>
<p> //seems you were missing opening <p> tag//
<?php htmlout($recipe_html); ?> |
<a href="updaterecipe.php?id=<?=$id?>">edit</a> | //can use shorthand here//
<a href="deleterecipe.php?id=<?=$id?>">delete</a> //can use shorthand here//
</p>
<?php endforeach; ?>
</table>
</blockquote>