Solution for while in HTML structure

Asked

Viewed 1,783 times

-3

Someone has a simple solution to loop while in PHP to the following HTML structure?

Basically I want every 3 Ivs to have one <li> separating them so that the slide can function properly, since each <li> works as a "new page". A template page, If you look at the source code, it’s 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>John Smith</h2>
                    <span>Webdesigner</span>
                </div>
            </div>

            <div class="col-md-4">
                <div class="team-post">
                    <div class="team-gal">
                        <img alt="" src="http://placehold.it/300x378">
                    </div>
                    <h2>Mike Smith</h2>
                    <span>Founder of a Company</span>
                </div>
            </div>

            <div class="col-md-4">
                <div class="team-post">
                    <div class="team-gal">
                        <img alt="" src="http://placehold.it/300x378">
                    </div>
                    <h2>Donald Smith</h2>
                    <span>Development</span>
                </div>
            </div>

        </div>
    </li>
    <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>John Smith</h2>
                    <span>Webdesigner</span>
                </div>
            </div>

            <div class="col-md-4">
                <div class="team-post">
                    <div class="team-gal">
                        <img alt="" src="http://placehold.it/300x378">
                    </div>
                    <h2>Mike Smith</h2>
                    <span>Founder of a Company</span>
                </div>
            </div>

            <div class="col-md-4">
                <div class="team-post">
                    <div class="team-gal">
                        <img alt="" src="http://placehold.it/300x378">
                    </div>
                    <h2>Donald Smith</h2>
                    <span>Development</span>
                </div>
            </div>

        </div>
    </li>
</ul>

We saw that each <li> which repeats a new "page" is generated for the slider, that is, in the structure it needs to treat that to each X number of records it generates a <li>, something like.

  • Put your code (php) that you tried, and we can help you. ".

  • 1

    Where does the content come from h2 and span? the images are all the same, this content is also different each team-post? (your link doesn’t work for me...)

  • 2

    Clarify your specific issue or add other details to highlight exactly what you need. The way it’s written here, it’s hard to know exactly what you’re asking. See the How to Ask page for help clarifying this question.

  • 1

    Basically what he wants is, looking at the above structure, that every 3 times they are separated by a li to be able to arrive at the result of the slide that he put as a model.

  • Yes, we understand what he wants, the problem is exactly this. In addition to wanting a ready answer, without having work, not even posted the details so we can help to give an answer, your answer for example, would be complete if it informed correctly, the database data, how is his php, etc. Sopt or Soen, are totally against providing work ready, we are not a free programming community.

  • @Marceloaymone I agree with you, the question is very vague.

  • I have php code, I just can’t edit the question anymore because they put it on hold. What I want is very simple and @Diegovieira understood correctly, the html structure is a template but obviously in php the images and text come from the database. I believe the question was very simple and easy to understand but, I have php code, and it was not a question like "do it for me", it was like, collaborate with suggestions please.

  • 1

    Then it would fit in another reason to suspend the question, because it is based on opinions.

Show 3 more comments

1 answer

2


I am maintaining the same code structure that you used, where php and html are mixed, to resolve this issue you can use the % operator in this situation it will check if 3 records have been processed, for this it will divide 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 managed to perform an exact division, in your specific case. This will happen every three records or when 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 it will include a tag to close the current li and will open a new tag li. Any questions leave a comment.

A tip that has nothing to do directly with the post, try to start separate layers, separate php from html, use MVC. There are frameworks for Template like Twig and even classes with pure php for those who want to use only native code, that makes the code more clean, easier to read and maintain, as well as being a way more standardized to work. If you want to see more in depth about frameworks see the Symfony, Zend and to ORM Doctrine, there are others, these are just a few examples

  • Perfect, I hadn’t thought of it so simple and practical. Thank you. the ; at the close of while is some mistake? I tested with/without it, ends up not influencing at all.

  • @Tiagoboeing <?php }; ? > in this case ; is not necessary, it was an error when transcribing the code. can you just <?php } ?>

  • No problem, I was even going to edit the answer by removing the ; but I thought there might be some special reason. I’ll take a look at your suggestion, I found it interesting.

Browser other questions tagged

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