Take value out of a while in php

0

The problem I have is the following.

I have this part of the code with which I show games in the database:

$catalogo = mostrarCatalogo();

while($row2 = mysqli_fetch_assoc($catalogo)){
      echo'
        <div class="producto">
            <img src="images/'.$row2["idJuegos"].'.jpg" alt="proyecto2">
            <button class="btn btn-outline-secondary boton-lotengo">LO TENGO</button>
        </div>
        ';
      }

The problem is that I do not know how to get the value of $ row2 and pass it to another method, without putting a return and remove it from the loop.

Update: I want that when I click on the button I have it, I take the id of the game that at that moment comes out through the while and can pass it to another function to insert

    
asked by Adrián Sanz 05.06.2018 в 08:40
source

3 answers

1

$ row2 is an associative array, and you have the values with the name of the column or the column number.

inside the loop you can call any function you want by passing $ row2

$catalogo = mostrarCatalogo();
    while($row2 = mysqli_fetch_assoc($catalogo)){ 
    echo' LO TENGO ';
    echo $row2['nombre_columna'];
    myFunction($row2);
    }
    function myFunction($row){
     // trabajo con row2 (dentro de la función se llamará $row) en una funcion

    }
    
answered by 05.06.2018 в 08:50
0

Just do not use return ... I do not know what you want to do, but for ex. in this code inserted in an array all the games whose IDs are odd ...

$catalogo = mostrarCatalogo();

$idsImpares = array();
while($row2 = mysqli_fetch_assoc($catalogo)){
      chequearImpar($row2["idJuegos"]);
      echo'
        <div class="producto">
            <img src="images/'.$row2["idJuegos"].'.jpg" alt="proyecto2">
            <button class="btn btn-outline-secondary boton-lotengo">LO TENGO</button>
        </div>
        ';
      }

      function chequearImpar($id) {
         if($id%2 !== 0) {
             $idsImpares[] = $id;
         }
      } 
    
answered by 05.06.2018 в 14:11
0

As the other answers say, your question is quite ambiguous, since it can have multiple solutions depending on what you want to do with the loop data.

If what you want is to use every data that you find in the loop to send it to a function to process it, you can use the call of a function inside the loop.

$catalogo = mostrarCatalogo();

while($row2 = mysqli_fetch_assoc($catalogo)){

      llamarFuncionQueNecesitas($row2) // Aqui envias en cada iteración del bucle la información

      echo'
        <div class="producto">
            <img src="images/'.$row2["idJuegos"].'.jpg" alt="proyecto2">
            <button class="btn btn-outline-secondary boton-lotengo">LO TENGO</button>
        </div>
        ';
      }

Another option would be to add all the data you are finding in the cycle to use later. Something like this:

$catalogo = mostrarCatalogo();
$return_array = array();
while($row2 = mysqli_fetch_assoc($catalogo)){

      array_push($return_array ,$row2); //guardar cada dato de la iteración para usar en otro momento

      echo'
        <div class="producto">
            <img src="images/'.$row2["idJuegos"].'.jpg" alt="proyecto2">
            <button class="btn btn-outline-secondary boton-lotengo">LO TENGO</button>
        </div>
        ';
      }

llamarFuncionQueNecesitas($return_array) //Enviar el arreglo lleno a donde necesites.

I hope it will help you solve your problem, any questions, let me know.

    
answered by 05.06.2018 в 21:10