How to get the next item from the database

Asked

Viewed 65 times

1

I’m trying a big problem some time, where I try to get the next item of an array, coming from a BD, but it doesn’t work. The goal is to pass a slide with different images of registered users in a BD.

<div class="container">
    <div class="section-heading center-holder">
        <h2>Alguns de nossos clientes</h2>
        <div class="section-heading-line"></div>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod<br>tempor incididunt ut labore et
            dolore magna aliqua.</p>
    </div>
    <div class="row mt-60">
        <ul class="slider">
            <?php
                $control_active = 2;
                $cont = 1;

                $users = "SELECT id_usuario, slug, nome, foto, email, views, table_status 
                FROM usuarios 
                WHERE table_status = 1 
                AND foto <> '' 
                AND views > 500
                AND slug NOT IN ('sergiomm')
                AND slug NOT LIKE '%dieta%'";

                $conn       = new conexao();
                $consulta   = $conn->consulta($users);
                $result     = $conn->busca($consulta);
                $total      = $conn->conta($consulta);

                if ($total > 3){
                while ($row = $conn->busca($consulta)){                                         

                    $linkInicial    = 'http://linkto.bio/';
                    $linkFinal      = str_replace('|:|', '', $row['foto']);
                    $linkCompleto   = $linkInicial . $linkFinal;
                    if($control_active == 2){               
                ?>
            <li>
                <input type="radio" id="slide<? echo $cont; ?>" name="slide" checked>
                <img src="<? echo $linkCompleto; ?>" alt="<? echo $row['slug']; ?>"/>
                <img src="<? echo $linkCompleto; ?>" alt="<? echo $row['slug']; ?>"/>
                <img src="<? echo $linkCompleto; ?>" alt="<? echo $row['slug']; ?>"/>
                <img src="<? echo $linkCompleto; ?>" alt="<? echo $row['slug']; ?>"/>
                <label for="slide<? echo $cont; ?>"></label> 
            </li>
            <?php 
                $control_active = 1; 
                    }else{                      
            ?>

            <? } $cont += 1; } } ?>
        </ul>
    </div>

</div>
<br/><br/><br/>
<!-- /Nossos usuários -->

I wanted you on the line <img src="<? echo $linkCompleto; ?>" alt="<? echo $row['slug']; ?>"/> take the later images.

Can someone help me figure this out.

  • For each row you want to pick up all the later lines?

  • Yes, the first beauty, she does it right, takes the first item, but the other 3 lines repeat the same data.

  • I tried with next(), and with for also.

  • First, is the image saved in a server directory? According to the field exists in the database? Could you post the full PHP code?

  • Yes, Nicolas. The image exists and so does the field.

  • @Nicolaspereira I edited with the complete code.

  • @Is this working? Are you returning something already pro $Row? I didn’t understand your question

  • Yes @Nicolaspereira, it returns the data from SELECT, I treat this data and take the image, passing from $Row['photo'] to the variable. This works "perfectly" for the first item, but I want to know how I get the next image (that exists!), for the next item.

Show 3 more comments

3 answers

1


At that point

<?for($i=1; $i<4; $i++){ ?>

    <li>
        <input type="radio" id="slide<? echo $cont; ?>" name="slide" checked>

you are generating a LI 4 times according to your FOR. And in each LI generated you make another LOOP

<?php   
    while ($row = $conn->busca($consulta)){                                         
          $linkInicial  = 'http://linkto.bio/';
          $linkFinal        = str_replace('|:|', '', $row['foto']);
          $linkCompleto = $linkInicial . $linkFinal;
?>

    <img src="<? echo $linkCompleto; ?>" alt="Imagem da usuário <? echo $row['slug']; ?>"/>

<? } ?>

Thus, in the first iteration of FOR, when you arrive at WHILE it generates LINKS to all the images recovered from the database. In the second iteration of FOR he no longer has images to generate, as they were all generated before.

If what you want is to generate 4 Lis with all the images in the database, first do the WHILE storing all links and slugs in arrays, with a controlled index

<?
    $indice = 0;
    while ($row = $conn->busca($consulta)){                                         
        $linkInicial  = 'http://linkto.bio/';
        $linkFinal        = str_replace('|:|', '', $row['foto']);
        $linkCompleto[$indice] = $linkInicial . $linkFinal;
        $slug[$indice] = $row['slug'];
        $indice++;
    }
?>

then do your FOR to mount the Lis with all images

<? for($i=0; $i<3; $i++){ ?>

    <li>
        <input type="radio" id="slide<? echo $i; ?>" name="slide" checked>

        <img src="<? echo $linkCompleto[$i]; ?>" alt="Imagem da usuário <? echo $slug[$i]; ?>"/>

        <? } ?>

        <label for="slide<? echo $cont; ?>"></label>
    </li>

<? } ?>

Remarks There is no need for an accountant within the FOR, you can use the $i itself to control. Normally, arrays start with index 0, so I changed the FOR to be from 0 to 3 (4 positions) It’s always easier to control things in arrays in PHP. Loops within loops make things more complicated.

I hope I’ve helped ;)

  • 1

    My friend, you saved the day. Thank you so much for giving me this help!

0

            $users = "SELECT id_usuario, slug, nome, foto, email, views, table_status 
            FROM usuarios 
            WHERE table_status = 1 
            AND foto <> '' 
            AND views > 500
            AND slug NOT IN ('thiago_hd', 'sergiomm', 'consultacaixa2019', 'maisempregocv', 'clubeamostrasgratis', 'saudetotal', 'sniperafiliados')
            AND slug NOT LIKE '%dieta%' limit 5";

            $conn       = new conexao();
            $consulta   = $conn->consulta($users);
            $result     = $conn->busca($consulta);
            $total      = $conn->conta($consulta);

            if ($total > 1){    
        ?>

        <? for($i=1; $i<4; $i++){ ?>

        <li>
            <input type="radio" id="slide<? echo $cont; ?>" name="slide" checked>

            <?php   
            while ($row = $conn->busca($consulta)){                                         
              $linkInicial  = 'http://linkto.bio/';
              $linkFinal        = str_replace('|:|', '', $row['foto']);
              $linkCompleto = $linkInicial . $linkFinal;
            ?>

                <img src="<? echo $linkCompleto; ?>" alt="Imagem da usuário <? echo $row['slug']; ?>"/>

            <? } ?>

            <label for="slide<? echo $cont; ?>"></label>
        </li>

        <?  $cont += 1; } ?>

        <? } ?>
    </ul>

RESULT -

    <ul class="slider">


        <li>
            <input type="radio" id="slide1" name="slide" checked>


                <img src="http://linkto.bio/arquivos/2018/09/vivian-telles.jpg" alt="Imagem da usuário viviantelles"/>


                <img src="http://linkto.bio/arquivos/2018/07/ecosafety.jpg" alt="Imagem da usuário botasecosafety"/>


                <img src="http://linkto.bio/arquivos/2018/06/sua-aventura.jpg" alt="Imagem da usuário suaaventura"/>


                <img src="http://linkto.bio/arquivos/2018/06/elba-ramalho.jpg" alt="Imagem da usuário locomotivelba"/>


            <label for="slide1"></label>
        </li>


        <li>
            <input type="radio" id="slide2" name="slide" checked>


            <label for="slide2"></label>
        </li>


        <li>
            <input type="radio" id="slide3" name="slide" checked>


            <label for="slide3"></label>
        </li>


                </ul>

0

When you do $linkComplete = $linkInitial . $linkFinal; inside the while, it mounts this variable and you display it three times. If you display only one line " alt=""/> you will display once the correct value. The other two lines will show repeated.

However, if you want to show the previous values, you should use an array, with something like $linkComplete[Indice] = $linkInitial . $linkFinal; Then, when it came time to show off, you’d wear " alt=""/> " alt=""/> "alt=""/>

In that case you would only have the 3 values ready after the while loop run 3 times. Also, your if($control_active == 2) may also disrupt your logic.

  • Hi Pablo, good morning. Thanks for the reply, it turned out that I could not do that way you said, but I changed my approach I gave '"right'", appeared the subsequent data, but now they do not appear in the next item on the list.

  • I posted the code above, with the resolution

  • 1

    Moses, I published another answer, as a response to your solution. I think I have now been clearer and understood better your problem

Browser other questions tagged

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