简体   繁体   中英

How to give line break in div using while loop in php?

I want to give br tag or hr tag after div, and my div is inside the while loop. Please check the code below what am I doing wrong.

<?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>

[Here I want to give br tag after 4 products show in a div, right now my 5th product showing just after details button. I want to give space between details button and title on every 5th product or when my div will show on another row.]

<?php endwhile; ?>

在详细信息按钮和产品标题之间,在这里我要为每4个产品赋予br标签的耐克运动鞋。

Your suggestions are welcome.

Put the conditional after the product div, and your $count after the conditional, like this:

<?php

$count = 1; 

while($count <= 20) {
    echo "Product ID: $count <br>";
    if($count%4 == 0) echo "<hr>";
    $count++;
}

You have so many unnecessary openings and closings of your PHP tag. Take a look at this example that uses all of the results from your MySQL query:

// 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++;
}

Fiddle: Live Demo

UPDATE After reading your comment, you need to be using the .card-columns class to layout your product gallery. Take a look at this example that does not require that you keep a counter:

<?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 -->';
?>

Fiddle: Live Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM