While isn’t stopping when he should

Asked

Viewed 170 times

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>

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

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

2

If I understood what you want you were going the right way, 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();
?>

I put in the Github for future reference.

  • I’ll try it and see if it works, thank you

  • I already tried this change but now it only prints me 1 product per line, IE, is printing a product for each <div class="Row">. I need it to print 4 products per line and when reaching 4 creates a new line

  • There is CSS problem and not PHP, are different things.

  • I’ve already solved the mustache problem, thanks for the help anyway.

0

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.