[英]How to give line break in div using while loop in php?
我想在div之后給出br標簽或hr標簽,而我的div在while循環內。 請檢查下面的代碼我在做什么錯。
<?php $count =0;?>
<?php while($product = mysqli_fetch_assoc($featured)) : ?>
<?php $count++;
if($count%4 == 0) {
?><br><hr><?php
}
?>
<!-- Number 1 -->
<div class="col-md-3">
<h6><?= $product['title']; ?></h6>
<div class="container-fluid">
<div class="row" style="width:200px;height:200px">
<img src="<?= $product['image']; ?>" alt="<?= $product['title']; ?>" class="img-thumb" />
</div>
</div>
<hr>
<p class="list-price text-danger">List Price: <s>$<?= $product['list_price']; ?></s></p>
<p class="price">Our Price: $<?= $product['price']; ?></p>
<button type="button" class="btn btn-sm btn-success" onclick = "detailsmodal(<?= $product['id']; ?>)">Deatils</button>
</div>
[在這里,我要在div中顯示4個產品后給br標簽,現在我的第5個產品在詳細信息按鈕后顯示。 我想在每第5個產品上,或者當我的div在另一行顯示時,在詳細信息按鈕和標題之間留空格。
<?php endwhile; ?>
歡迎您提出建議。
將條件放在產品div之后,並將$count
放在條件之后,如下所示:
<?php
$count = 1;
while($count <= 20) {
echo "Product ID: $count <br>";
if($count%4 == 0) echo "<hr>";
$count++;
}
您的PHP標記有很多不必要的開頭和結尾。 看一下使用MySQL查詢的所有結果的示例:
// Create a counter to store where we're at
$counter = 1;
// Iterate through each product
foreach($products as $product) {
// Print the HTML markup
echo '
<div class="col-md-3">
<h6>', $product['title'], '</h6>
<div class="container-fluid">
<div class="row" style="width: 200px; height: 200px;">
<img src="', $product['image'], '" alt="', $product['title'], '" class="img-thumb" />
</div>
</div>
<hr />
<p class="list-price text-danger">List Price: <s>', $product['list_price'], '</s></p>
<p class="price">Our Price: $', $product['price'], '</p>
<button type="button" class="btn btn-sm btn-success" onclick = "detailsmodal(', $product['id'], ')">Deatils</button>
</div>';
// Print the line/horizontal rulers
if($counter % 4 === 0) {
echo '<br /><hr />';
}
// Increment the counter
$counter++;
}
小提琴: 現場演示
更新閱讀您的評論后,您需要使用.card-columns類來布局產品庫 。 請看以下示例,該示例不需要保留計數器:
<?php
// Print the containing card-columns
echo '<!-- beginning of card gallery -->
<div class="card-columns mt-3">';
// Iterate through each product
foreach($products as $product) {
// Print the HTML markup
echo '
<div class="card">
<img src="', $product['image'], '" alt="', $product['title'], '" class="card-img-top" />
<div class="card-body">
<h5 class="card-title">', $product['title'], '</h5>
<p class="card-text text-danger">List Price: <s>', $product['list_price'], '</s></p>
<p class="card-text">Our Price: <s>', $product['price'], '</s></p>
<button type="button" class="btn btn-sm btn-success", onclick="detailsmodal(', $product['id'], ')">Deatils</button>
</div>
</div> <!-- end of single product -->';
}
echo '
</div> <!-- end of product gallery -->';
?>
小提琴: 現場演示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.