以HTML/PHP显示数据库中的项目
我正试图从数据库中显示网页上的一些项目。我有一个函数,可以循环遍历数据库中存储的数据。当前,它显示数据库中出现的最后一项,而不是第一项或其他任何内容。我试着按id订购这些物品,但结果是一样的。我还尝试显示数组中的各个项目,但它只显示数据库中最后一个项目的第一个字符 显示项目功能:以HTML/PHP显示数据库中的项目,php,html,mysqli,Php,Html,Mysqli,我正试图从数据库中显示网页上的一些项目。我有一个函数,可以循环遍历数据库中存储的数据。当前,它显示数据库中出现的最后一项,而不是第一项或其他任何内容。我试着按id订购这些物品,但结果是一样的。我还尝试显示数组中的各个项目,但它只显示数据库中最后一个项目的第一个字符 显示项目功能: 用于显示项目的区域: 另外,不要担心用close标签来结束章节和文章,因为它已经完成了 看来,HTML生成应该在循环内部,而不是外部: <?php $products = get_produc
用于显示项目的区域:
另外,不要担心用close标签来结束章节和文章,因为它已经完成了 看来,HTML生成应该在循环内部,而不是外部:
<?php
$products = get_product_details();
foreach($products as $ap) {
$name = $ap['pro_name'];
$description = $ap['descr'];
$price = $ap['rrp'];
?>
<div class = "section_4">
<h1 class = "food_h2"><?php echo $name; ?></h2>
<br><img src = "img/products/hassellback_pot.jpg" class = "img_dis">
<br><h1 class = "food_h2"><?php echo $description; ?></h1>
<br><h1 class = "food_h2">£ <?php echo $price; ?></h1>
<button class = "basket_btn" type = "button"> Buy </button>
</div>
<?php
} ?>
英镑
购买
您正在数组上循环,但没有对值执行任何操作:
foreach($products as $ap)
{
$name = $ap['pro_name'];
$description = $ap['descr'];
$price = $ap['rrp'];
}
然后在循环之后,当值是数组的最后一个值时,您最终使用它们一次:
英镑
购买
相反,请使用循环中的值,以便可以使用每个值:
foreach($ap形式的产品)
{
$name=$ap['pro_name'];
$description=$ap['descr'];
$price=$ap['rrp'];
?>
英镑
购买
菜单
£
购买
在函数中选择查询并返回数组,然后使用foreach循环显示函数中的数据,在本例中为数组。@Mario这是什么意思?请提供示例。你的foreach
循环除了重复重写变量外,什么都不做。因此在该循环之后,这些变量将始终具有循环中的最后一个值,guaranteed.这些变量的使用是否也应该在循环中?@David抱歉,我有点困惑-我对PHP很不熟悉,所以我不确定我在做什么,所以我需要一些示例。将mysqli\u fetch\u assoc($res)
转换为$ret=mysqli\u fetch\u数组($res)
在函数中循环时获取所有不需要的项的列表。这个答案看起来像是一个准确的最终结果。如果您描述询问者的代码不起作用的原因以及此代码的不同之处,就会更清楚。
<?php
$products = get_product_details();
foreach($products as $ap) {
$name = $ap['pro_name'];
$description = $ap['descr'];
$price = $ap['rrp'];
?>
<div class = "section_4">
<h1 class = "food_h2"><?php echo $name; ?></h2>
<br><img src = "img/products/hassellback_pot.jpg" class = "img_dis">
<br><h1 class = "food_h2"><?php echo $description; ?></h1>
<br><h1 class = "food_h2">£ <?php echo $price; ?></h1>
<button class = "basket_btn" type = "button"> Buy </button>
</div>
<?php
} ?>
foreach($products as $ap)
{
$name = $ap['pro_name'];
$description = $ap['descr'];
$price = $ap['rrp'];
}
<div class = "section_4">
<h1 class = "food_h2"><?php echo $name; ?></h2>
<br><img src = "img/products/hassellback_pot.jpg" class = "img_dis">
<br><h1 class = "food_h2"><?php echo $description; ?></h1>
<br><h1 class = "food_h2">£ <?php echo $price; ?></h1>
<button class = "basket_btn" type = "button"> Buy </button>
</div>
foreach($products as $ap)
{
$name = $ap['pro_name'];
$description = $ap['descr'];
$price = $ap['rrp'];
?>
<div class = "section_4">
<h1 class = "food_h2"><?php echo $name; ?></h2>
<br><img src = "img/products/hassellback_pot.jpg" class = "img_dis">
<br><h1 class = "food_h2"><?php echo $description; ?></h1>
<br><h1 class = "food_h2">£ <?php echo $price; ?></h1>
<button class = "basket_btn" type = "button"> Buy </button>
</div>
<?php
} // end the loop after using the values
<article class = "main_area2">
<h1 class = "food_h1">Menu</h1>
<br>
<section class = "menu_section">
<?php foreach(get_product_details() as $ap): ?>
<div class = "section_4">
<h1 class = "food_h2"><?php echo $ap['pro_name']; ?></h1>
<br>
<img src = "img/products/hassellback_pot.jpg" class = "img_dis">
<br>
<h1 class = "food_h2"><?php echo $ap['descr'] ?></h1>
<br>
<h1 class = "food_h2">£ <?php $ap['rrp']; ?></h1>
<button class = "basket_btn" type = "button"> Buy </button>
</div>
<?php endforeach; ?>
</section>
</article>