Break UL tag after 'n' times in while-loop

php

Solution

First, I provide simple PHP script that can be addapted to your needs. In this script open and end tags are handled properly.

I added also `$break_after` variable - you can set here any positive value you want - in your case it's 3 because you want to do the action after each 3rd element.

First method (it assumes you can get number of data elements before loop)

<?php
$data = array(1,2,3,4,5);

$break_after = 3;

$counter = 0;   
$totalNumber = count($data);                    
foreach ($data as $item) {
    if ($counter % $break_after == 0) {
        echo '<ul>';
    }
    echo '<li>'.$item.'</li>';    

    if ($counter % $break_after == ($break_after-1) || $counter == $totalNumber-1) {
        echo '</ul>';
    }
    ++$counter;

}

Second method (it assumes you cannot get number of data elements before loop)

<?php
$data = array(1,2,3,4,5);

$break_after = 3;

$counter = 0;               
foreach ($data as $item) {
    if ($counter % $break_after == 0) {
        echo '<ul>';
    }
    echo '<li>'.$item.'</li>';    

    if ($counter % $break_after == ($break_after-1)) {
        echo '</ul>';
    }
    ++$counter;

}

if ((($counter-1) % $break_after) != ($break_after-1)) {
    echo '</ul>';

}

Regarding your question, you also need to remember to start your `<ul>` each 3rd record (not just closing it) but also make sure to close it after your loop. In your case you can use second method because you don't know number of elements (in fact you can get them using `mysqli_num_rows` function and then you could also use method 1). For your case your code should probably look like this:

<?php
  $selektKat = "SELECT * FROM `proizvodi` WHERE `kategorija` = '$kat'"; //don't worry about SQLi; I will fix it
  $result = mysqli_query($con, $selektKat) or die(mysqli_error());

// Line where the loop starts
<?php
$counter = 0;   

$break_after = 3;

while ($row = mysqli_fetch_array($result)) { 
  if ($counter % $break_after == 0) {
   ?>
  <ul class="products-grid clearfix" style="margin-right: 5px; margin-left: 20px;">
  <?php } ?>
  <li class="item" style="min-height: 339px">
    <a id="product-image-42321" href="proizvod.php" title="naziv" class="product-image">
      <img src="http://static.511tactical.com/mag/media/catalog/product/cache/1/small_image/220x/9df78eab33525d08d6e5fb8d27136e95/5/3/53212_716_Alternate1.jpg" width="220" alt="<?php echo $row['naziv'] ?>" />
    </a>
      <ul class="swatches clearfix">
      <li id="swatch-228" class="swatch product-42321">
        <a href="proizvod.php" title="<?php echo $row['naziv']; ?>">
        <img src="<?php echo __DIR__ . '/images/' . $row['slika'] . '.jpg'; ?>" />
        </a>
      </li>
      </ul>

      <div class="price-box">
        <span class="label" id="configurable-price-from-42321">
          <span class="configurable-price-from-label">
          </span>
        </span>


      <div class="product-name"><a id="product-name-140981" href="proizvod.php"><?php echo $row['naziv']; ?></a></div>

      <span class="regular-price" id="product-price-42321">
        <span class="price"><?php echo $row['cijena']; ?><sup>KM</sup>
        </span>
      </span>
      </div>

      <div class="actions">
      </div>

  </li>

<?php
    if ($counter % $break_after == ($break_after - 1)) {
        echo '</ul>';
    }
    ++$counter;

}

if (($counter-1) % $break_after != ($break_after - 1)) { // make sure there will be closing </ul>
    echo '</ul>';

}
?>
</div>

Problem

I'm currently in developing module shop for the company I work for and there is a little problem. While extracting records from the table, I want for every third data record to close HTML "UL" tag. This is what I currently have: ``` <?php $selektKat = "SELECT * FROM `proizvodi` WHERE `kategorija` = '$kat'"; //don't worry about SQLi; I will fix it $result = mysqli_query($con, $selektKat) or die(mysqli_error()); // Line where the loop starts <?php while ($row = mysqli_fetch_array($result)) { ?> <ul class="products-grid clearfix" style="margin-right: 5px; margin-left: 20px;"> <li class="item" style="min-height: 339px"> <a id="product-image-42321" href="proizvod.php" title="naziv" class="product-image"> <img src="http://static.511tactical.com/mag/media/catalog/product/cache/1/small_image/220x/9df78eab33525d08d6e5fb8d27136e95/5/3/53212_716_Alternate1.jpg" width="220" alt="<?php echo $row['naziv'] ?>" /> </a> <ul class="swatches clearfix"> <li id="swatch-228" class="swatch product-42321"> <a href="proizvod.php" title="<?php echo $row['naziv']; ?>"> <img src="<?php echo __DIR__ . '/images/' . $row['slika'] . '.jpg'; ?>" /> </a> </li> </ul> <div class="price-box"> <span class="label" id="configurable-price-from-42321"> <span class="configurable-price-from-label"> </span> </span> <div class="product-name"><a id="product-name-140981" href="proizvod.php"><?php echo $row['naziv']; ?></a></div> <span class="regular-price" id="product-price-42321"> <span class="price"><?php echo $row['cijena']; ?><sup>KM</sup> </span> </span> </div> <div class="actions"> </div> </li> <?php } ?> </ul> // This has to be closed in loop after every 3 records </div> ``` Picture: Cheers.

Original source