How to serialize PHP data by connecting to SQL to show in a select in html (Does not show the value)

0

I would like you to help me with this code that I am solving for a university project.

It turns out that I have 3 Tables that I want to show it in different select I am working with the MVC, where we have a general controller, for this controller we use the following function:

public function registrar(){
    $db=new clasedb();
    $conex=$db->conectar(); // Conectando...

    /*  ---------- Primer Arreglo -----------  */
        $sql="SELECT * FROM usuarios"; 
        $res_user=mysqli_query($conex,$sql);
    $user=array();
    $i=0;
    while ($data=mysqli_fetch_array($res_user)) {
        for ($j=0; $j < 7 ; $j++) { 
            $user[$i][$j]=$data[$j];
        } $i++;
    }
    /*  ---------- Primer Arreglo -----------  */

    /*  ---------- Segundo Arreglo -----------  */
        $s_sql="SELECT * FROM lector";
        $res_lect=mysqli_query($conex,$s_sql);
    $lect=array();
    $j=0;
    while ($dato=mysqli_fetch_array($res_lect)) {
        for ($k=0; $k < 3 ; $k++) { 
            $lect[$j][$k]=$dato[$k];
        }
    }
    /*  ---------- Segundo Arreglo -----------  */

    /*  ---------- Tercer Arreglo -----------  */
        $t_sql="SELECT * FROM libros";
        $res_lib=mysqli_query($conex,$s_sql);
    $libro=array();
    $k=0;
    while ($valor=mysqli_fetch_array($res_lib)) {
        for ($l=0; $l < 4 ; $l++) { 
            $libro[$j][$l]=$valor[$l];
        }
    }
    /*  ---------- Tercer Arreglo -----------  */


    header("location: ../Vistas/Entrega/entrega.php?usuario=".serialize($user)."&lector=".serialize($lect)."&libros=".serialize($libro));
}

Then it is sent for this form:

    <?php
    extract($_REQUEST);
    include ("../../Modelos/clasedb.php");
    $user=unserialize($usuario);
    $lect=unserialize($lector);
    $libro=unserialize($libros);
?>
<!DOCTYPE html>
    <html>
        <head>
            <title>Formulario</title>
        </head>
    <body>
        <a href="../../index.php"> Volver </a>
        <a href="../../Controladores/ControladorEntrega.php?operacion=index">Listar Entrega</a>
        <h1>Registro de Entrega</h1>
        <form action="../../Controladores/ControladorEntrega.php" method="POST" name="form"> 

            <label>Seleccione el Usuario que entrega el Libro</label>
            <select name="id_usuario">
                <?php for ($i=0; $i < count($user); $i++) { ?>
                    <option value="<?=$user[$i][0]?>"> <?=$user[$i][5] ?> </option>
                <?php } ?>
            </select>
            <br>
            <label>Seleccione el Lector que recibe el libro</label>
            <select name="id_lector">
                <?php for ($j=0; $j < count($lect); $j++) { ?>
                    <option value="<?=$lect[$j][0]?>"> <?=$lect[$j][1] ?> </option>
                <?php } ?>
            </select>
            <br>
            <label>Seleccione el libro a entregar</label>
            <select name="id_libro">
                <?php for ($k=0; $k < count($libro); $k++) { ?>
                    <option value="<?=$libro[$k][0]?>"> <?=$libro[$k][3] ?> </option>
                <?php } ?>
            </select>
            <br> <br>
            <label>Ingrese la Fecha de entrega </label> <input type="date" name="fecha_entrega"> <br>               
            <input type="hidden" name="operacion" value="guardar">
            <br>
            <input type="submit" name="guardar" value="guardar">
            <input type="reset" name="limpiar" value="limpiar">
    </body>
</html>

In the second SELECT it is only showing the Last data, while in the last one it is totally blank. Honestly I only know the very basics of languages, that's why I'm asking for help in case something is wrong.

    
asked by Kenny Gonzalez 01.07.2018 в 18:23
source

1 answer

0

In the second arrangement, only one is shown because the index $j is missing.

/*  ---------- Segundo Arreglo -----------  */
            $s_sql="SELECT * FROM lector";
            $res_lect=mysqli_query($conex,$s_sql);
        $lect=array();
        $j=0;
        while ($dato=mysqli_fetch_array($res_lect)) {
            for ($k=0; $k < 3 ; $k++) { 
                $lect[$j][$k]=$dato[$k];
            } 
            $j++;
        }
        /*  ---------- Segundo Arreglo -----------  */

And third you must initialize and increase the index $k and apply it.

/*  ---------- Tercer Arreglo -----------  */
            $t_sql="SELECT * FROM libros";
            $res_lib=mysqli_query($conex,$s_sql);
        $libro=array();
        $k=0; 
        while ($valor=mysqli_fetch_array($res_lib)) {
            for ($l=0; $l < 4 ; $l++) { 
                $libro[$k][$l]=$valor[$l];
            }
            $k++;
        }
        /*  ---------- Tercer Arreglo -----------  */
    
answered by 01.07.2018 / 19:01
source