[英]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.