[英]Display all posts from database in row
我试图回显数据库中我的职位表上的所有内容,但似乎只显示一个,如果我尝试复制代码,它将仅重复职位表中1个id之一
<div class="row">
<?php
$query = "SELECT * FROM products";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
$product_id = $row['product_id'];
$product_category_id = $row['product_category_id'];
$product_name = $row['product_name'];
$product_price = $row['product_price'];
$product_details = $row['product_details'];
$product_image = $row['product_image'];
?>
<div class="col-sm-6 col-lg-4">
<div class="single_special_cource">
<img width="360px" height="313px" src="img/<?php echo $product_image; ?>" class="special_img" alt="">
<div class="special_cource_text">
<a href="course-details.php?p_id=<?php echo $product_id; ?>" class="btn_4">
<?php
$query = "SELECT * FROM categories WHERE cat_id = {$product_category_id}";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
echo $cat_title;
}
?>
</a>
<h4><?php echo $product_price; ?></h4>
<a href="course-details.php?p_id=<?php echo $product_id; ?>">
<h3><?php echo $product_name; ?> </h3>
</a>
<p><?php echo $product_details; ?> </p>
</div>
</div>
</div>
<?php
}
?>
</div>
尝试更改而循环到
$row = mysqli_fetch_assoc($result);
while($row) {
// code here
$row = mysqli_fetch_assoc($result);
}
主while循环中使用的变量遍历了您的行和查询。 更改内部循环变量名称
<?php
$categoryQ = "SELECT * FROM categories WHERE cat_id = {$product_category_id}";
$categoryRes = mysqli_query($connection, $categoryQ);
while ($row2 = mysqli_fetch_assoc($categoryRes)) {
$cat_id = $row2['cat_id'];
$cat_title = $row2['cat_title'];
echo $cat_title;
}
?>
例如
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.