V
V
Viktor Dotsenko2015-01-23 17:23:44
PHP
Viktor Dotsenko, 2015-01-23 17:23:44

How to make a closing tag after displaying four elements?

Good afternoon.
There was a small problem. For the test I deduce records from a DB.

<? foreach ($data as $row): ?>

 <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3">
<div class="game-element">
   
          <img src="<?=$row['POSTER']?>"><br>
               <a href="?id=<?=$row['ID']?>" class="game-title"><?=$row['TITLE']?></a>


                 </div>
            </div><!--game-element -->
        <? endforeach ?>

e76e167865a64fe5abfb5eb5f0b1372f.jpg
How to make it output without this empty space?
It seems to me that 4 elements should be wrapped with a tag - ... the output of elements, but how to do it only after the fourth?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vyacheslav, 2015-01-23
@hedint

Find out when the 4th element is:

$counter = 0;
 foreach ($data as $row) {
$counter++;
if ($counter % 4 === 0)
{
//4-й элемент
}
}

But, it seems to me that the problem is more likely in the layout, and no extra elements will be needed with normal layout / styles.

I
IceJOKER, 2015-01-23
@IceJOKER

stackoverflow.com/questions/13270870/wrapping-a-di...
I think you can write a four yourself instead of a three)

D
Dinar Garipov, 2015-01-23
@gaaarfild

<div class="container">
  <?php $i = 0; ?>
  <?php foreach ($data as $row): ?>
    <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3">
      <div class="game-element">
          <img src="<?=$row['POSTER']?>"><br>
              <a href="?id=<?=$row['ID']?>" class="game-title"><?=$row['TITLE']?></a>


        </div>
    </div><!--game-element -->
    <?php 
      $i++
      if ($i == 4) {
        $i = 0;
        echo '</div><div class="container">';
      }
     ?>
  <?php endforeach ?>
<div>

just fix the code better. I hastily wrote govnokod.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question