Limit a loop

Asked

Viewed 217 times

0

I’m recently practicing PHP and I know how to loop with While, but I wanted to limit this loop so as not to break a column of the site.

<?php

        include_once 'conexao.php';

        $sql = "select * from perfil";

        $result = mysqli_query($con, $sql);

        while ($row = mysqli_fetch_array($result)){

    ?>
                <article class="novos-titulos">
<h3><a href="perfil.php?idperfil=<?php echo $row["idperfil"];?>"><?php echo mb_strimwidth($row["titulo"], 0, 20, "..." ); ?></a></h3>

                    <a href="perfil.php?idperfil=<?php echo $row["idperfil"];?>"><img src="img/<?php echo $row["capa"]; ?>" alt=""></a>
                    <span>Total: <?php echo $row["episodios"]; ?></span>

                </article>
  <?php } mysqli_close($con);?>
            </div>
  • What you want is to put a stop point on the correct loop? You can create a Count variable to stop the loop or use a for (https://www.w3schools.com/php/php_looping_for.asp)

  • if($Count == 10) break;

  • So. I want it to stop adding item to column. Limit to 5 items.

  • How so columns? The code does not generate several <Articles> one below the other?

1 answer

0


A solution, even efficient, to limit the number of iterations in the loop is to limit the return of the SQL query (this avoids loading data that will not be used):

select * from perfil limit 5;

Browser other questions tagged

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