While not stopping when you should

0

I have the following code:

<div class="row">
 <?
 $tag = mysqli_real_escape_string($connection,$_GET['categoria']);
 $result = $connection -> query("select * from produtos where tags like '%$tag%' and online='0' order by id limit 4");
 while($row = $result -> fetch_array(MYSQLI_ASSOC)){
     $id = $row['id'];
     $titulo = $row['titulo'];
     $resumo = $row['resumo'];
     $imagem = $row['imagem'];
 ?>
    <div class="grid_3">
        <div class="box2 wrap1 wow fadeInLeft" data-wow-delay="0.1s">
            <a href="verproduto.php?id=<?=$id?>"><img class="first" src="<?=$imagem?>" alt=""/></a>
            <div class="caption bggreen equal">
                <h6 class="text_3 colorblue">
                    <a href="verproduto.php?id=<?=$id?>""><?=$titulo?></a>
                </h6>
                <br>
                <p class="colorwhite">
                <?=$resumo?>
                </p>
            </div>
        </div>
    </div>
    <?
     }
     $result -> free();
    ?>

    </div>

<div class="row"> only accepts 4 boxes, how do I not have to repeat this div whenever the products exceed that number? Do I put the div within the while or do I have to do otherwise?

    
asked by anonymous 19.01.2015 / 23:49

2 answers

2

If I understood what you want you were going to the right way, you just needed to add an accountant to know when to stop:

<?
$tag = mysqli_real_escape_string($connection,$_GET['categoria']);
$result = $connection -> query("select * from produtos where tags like '%$tag%' and online='0' order by id limit 4");
$contador = 0;
while ($contador < 4 && ($row = $result -> fetch_array(MYSQLI_ASSOC)){
    $id = $row['id'];
    $titulo = $row['titulo'];
    $resumo = $row['resumo'];
    $imagem = $row['imagem'];
?>
    <div class="row">
        <div class="grid_3">
            <div class="box2 wrap1 wow fadeInLeft" data-wow-delay="0.1s">
                <a href="verproduto.php?id=<?=$id?>"><img class="first" src="<?=$imagem?>" alt=""/></a>
                <div class="caption bggreen equal">
                    <h6 class="text_3 colorblue">
                        <a href="verproduto.php?id=<?=$id?>""><?=$titulo?></a>
                    </h6>
                    <br>
                    <p class="colorwhite">
                         <?=$resumo?>
                    </p>
                </div>
            </div>
        </div>
    </div>
<?
    $contador++;
}
$result -> free();
?>
    
20.01.2015 / 00:04
0

I have solved the problem. Just remove the limit 4 in the query. Since I am using grids and they are with float: left then the boxes automatically change line.

    
20.01.2015 / 11:53