use while cycle data in a query outside the cycle

0

Hello everyone is a bit I understand the title sorry,

if what I want is to use a data I get from a query of a while while in another query I'm doing within the same php document

this is my while cycle

              <?php
              $_proceso = $_GET['var'];
              $query = "SELECT id,Nombre_proceso FROM proceso WHERE Nombre_proceso = '$_proceso'";
                $_result = $conn->query($query);
                if (!$_result) die($conn->error); 
                
                while ($_row = mysqli_fetch_array($_result)){
                    $_proceso = $_row["Nombre_proceso"];
                    $_id = $_row["id"];
                        }
              ?>

This is the part where I want to use the ID data that comes out of the ascle while

<?php
                            $query = "SELECT riesgo,Criticidad,Ocurrencia,Reaccion,Valor,CASE
                                        WHEN Decision = 0 THEN 'Eliminar'
                                        WHEN Decision = 1 THEN 'Mitigar'
                                        WHEN Decision = 2 THEN 'Delegar'
                                        WHEN Decision = 3 THEN 'Aceptar'
                                        END as Decision,Plan_Accion FROM riesgo_metodo_que where id_Proceso = ".$_id = row['id']."  Order By Valor";
                            $result = $conn->query($query);
                            if (!$result) die($conn->error);
                        ?>
    
asked by antonio sanchez 13.09.2018 в 23:28
source

1 answer

0

You could place the query in a function that is called with each interaction, something like this:

$_proceso = $_GET['var'];
$query = "SELECT id,Nombre_proceso FROM proceso WHERE Nombre_proceso = '$_proceso'";
$_result = $conn->query($query);
if (!$_result) die($conn->error); 

$respuesta = "";

while ($_row = mysqli_fetch_array($_result)){
    $_proceso = $_row["Nombre_proceso"];
    $_id = $_row["id"];
    $respuesta = getDatos([$_id,$conn]);
    break;
}

function getDatos($x){
    $query = "SELECT riesgo,Criticidad,Ocurrencia,Reaccion,Valor,CASE
                WHEN Decision = 0 THEN 'Eliminar'
                WHEN Decision = 1 THEN 'Mitigar'
                WHEN Decision = 2 THEN 'Delegar'
                WHEN Decision = 3 THEN 'Aceptar'
                END as Decision,Plan_Accion FROM riesgo_metodo_que where id_Proceso = ".$x[0]."  Order By Valor";
    $result = $x[1]->query($query);
    if (!$result) die($x[1]->error);

    return $result;
}

var_dump($respuesta);

Try this way to see. Greetings

    
answered by 14.09.2018 в 01:14