Solution for while in HTML structure

-3

Does anyone have a simple solution to while looping in PHP for the following HTML structure?

I basically want every 3 divs to have a <li> separating them so that the slide can work properly, since every <li> works as a "new page". A template page , if you analyze the source code, it is quite complete and easy to understand.

PHP code

<?php 
$fotosEmpresa = $conn->prepare("SELECT * FROM imgs_global WHERE TipoImg = ? ORDER BY rand()");                      
$fotosEmpresa->execute(array("QuemSomos")); 
if($fotosEmpresa->rowCount() > 0): ?>
<ul class="bxslider">
    <li>
        <div class="row">
        <?php while($rowFotoEmpresa = $fotosEmpresa->fetch(PDO::FETCH_OBJ)): ?>
            <div class="col-md-4">
                <div class="team-post">
                    <div class="team-gal">
                        <a href="administracao/imagens/quemsomos/imgG/<?php echo $rowFotoEmpresa->NomeImg; ?>" class="zoom">
                            <img alt="" src="administracao/imagens/quemsomos/<?php echo $rowFotoEmpresa->NomeImg; ?>">                          
                        </a>
                    </div>
                </div>
            </div>
        <?php endwhile; ?>  
        </div>
    </li>
</ul>
<?php endif; ?>

HTML Structure

<ul class="bxslider">
    <li>
        <div class="row">

            <div class="col-md-4">
                <div class="team-post">
                    <div class="team-gal">
                        <img alt="" src="http://placehold.it/300x378"></div><h2>JohnSmith</h2><span>Webdesigner</span></div></div><divclass="col-md-4">
                <div class="team-post">
                    <div class="team-gal">
                        <img alt="" src="http://placehold.it/300x378"></div><h2>MikeSmith</h2><span>FounderofaCompany</span></div></div><divclass="col-md-4">
                <div class="team-post">
                    <div class="team-gal">
                        <img alt="" src="http://placehold.it/300x378"></div><h2>DonaldSmith</h2><span>Development</span></div></div></div></li><li><divclass="row">

            <div class="col-md-4">
                <div class="team-post">
                    <div class="team-gal">
                        <img alt="" src="http://placehold.it/300x378"></div><h2>JohnSmith</h2><span>Webdesigner</span></div></div><divclass="col-md-4">
                <div class="team-post">
                    <div class="team-gal">
                        <img alt="" src="http://placehold.it/300x378"></div><h2>MikeSmith</h2><span>FounderofaCompany</span></div></div><divclass="col-md-4">
                <div class="team-post">
                    <div class="team-gal">
                        <img alt="" src="http://placehold.it/300x378"></div><h2>DonaldSmith</h2><span>Development</span></div></div></div></li></ul>

Wehaveseenthatevery%canddthatrepeatsanew"page" is generated for the slider, that is, in the structure it needs to treat that to each number of records it generates a <li> , something like .

    
asked by anonymous 24.07.2014 / 21:00

1 answer

2

I'm keeping the same code structure you used, where php and html are mixed, to solve this question you can use the% operator in this situation it will check if 3 records have already been processed, so it will split the variable $ i by 3 and check if the rest is 0, when the rest is 0 means that the number is multiple, because it was able to execute an exact division, in its specific case. This will happen every three records or how much the counter is 0 (0 divided by any number is 0, so exact division).

<?php

    $i=0; // Essa variável será usada como contador, vai permitir saber quantos registros foram processados
?>

<ul class="bxslider">
    <li>
        <div class="row">
            <?php 
                while($rowFotoEmpresa = $fotosEmpresa->fetch(PDO::FETCH_OBJ)){

                // Essa é a parte responsável por fazer com que a cada 3 registros gere um novo li, o zero também é um múltiplo, mas temos que remove-lo do contrário ele irá criar um li sem nenhum registro 
                if($i % 3 == 0 && $i != 0){
            ?>
            </li>
            <li>
            <?php
                } // Essa parte é responsável por encerrar a verificação do contador
                $i++; // Aqui é incrementado o contador, para informar que um novo registro foi processado
            ?>
            <div class="col-md-4">
                <div class="team-post">
                    <div class="team-gal">
                        <a href="administracao/imagens/quemsomos/imgG/<?php echo $rowFotoEmpresa->NomeImg; ?>" class="zoom">
                            <img alt="" src="administracao/imagens/quemsomos/<?php echo $rowFotoEmpresa->NomeImg; ?>">                          
                        </a>
                    </div>
                </div>
            </div>
        <?php } ?>  
        </div>
    </li>
</ul>

Every 3 records processed will include a tag to close the current li and will open a new li tag. Any questions leave a comment.

  

A tip that does not have to do directly with the post, try to start   separate the layers, separate php from HTML, use MVC . exist   Template frameworks such as twig and classes with php pure   for those who want to use only native code , this makes the code more   clean, easier to read, and maintainable than being a form   more standardized way of working. If you want to see more about   frameworks see Symfony , Zend and

    
25.07.2014 / 15:52