Selecionador Default option of a database

2

I have a table "put" this table contains the following fields

id puesto
1  empleado 
2  director
3  programador

already having the ID correcpondiente to the user and the data contained by a submith in $id = $_GET['id']; I want my select field to show its value according to the one that corresponds to the user, example:

if John has the "user" table in the "field" field, the id = 2, that the select shows:

<option value="<?php echo $id; ?>"><?php echo $p['puesto']; ?></option>

or translated

<option value="2">director</option>

To achieve this, and investigated the following:

<?php
$tabla_puesto =sqlsrv_query($conn, "select * from puesto");
//$conn contiene la conecion a la base de datos
$id = $_GET['id'];
//aqui obtengo el id que corresponde al usuario en este caso seria usuario=2
$sql_usuario= "SELECT * FROM usuario WHERE Id=$id ORDER BY Id ASC";
//consulta select para identificar BD
$result = sqlsrv_query($conn, $sql_usuario);
while($res = sqlsrv_fetch_array($result))
{
$puesto=$res['puesto'];
//muestro el valor de pesto 
$sql_puesto = sqlsrv_query($conn, "select * from puesto where id=$puesto");
                            if($c=sqlsrv_fetch_array($sql_puesto)) {
                                $tabla_puesto_puesto=$c['puesto'];
                            } 
}
//aqui seleciono la tabla puesto y relaciono que el valor de id con el valor del puesto corespondiente a su id de puesto, igualamos y consegumos el puesto con $tabla_puesto_puesto
?>

at this point I already have $ table_posed_post = director
the select where I want the position to appear in the selection this is my real problem is the following:

<select>
        <?php foreach($tabla_puesto as $p):?>
<option value="<?php echo $p['id']; ?>">
        <?php echo $p['puesto']; ?>
</option>
         <?php endforeach; ?>
</select>

This select does not work but I would like to know what the error is or if there is some simpler way, I hope I have described my problem correctly

    
asked by claus 09.08.2018 в 18:44
source

1 answer

2

I'll give you an example of how it would be less redundant, so as not to use cyclic structures ...

<?php
    //$conn contiene la conecion a la base de datos
    $tabla_puesto =sqlsrv_query($conn, "select * from puesto");
    //aqui obtengo el id que corresponde al usuario en este caso seria usuario=2
    $id = $_GET['id'];
    //muestro el valor de pesto 
    $puesto=$res['puesto'];
    //consulta select para identificar BD
    $sql_usuario = sqlsrv_query($conn, "SELECT * FROM usuario WHERE Id=$id ORDER BY Id ASC");
    //traer la info del puesto
    $sql_usuario = sqlsrv_fetch_array($sql_usuario);
    //se recuperan los valores
    $sql_puesto = sqlsrv_query($conn, "SELECT * FROM puesto WHERE id=$puesto");
    //se trae la info del usuario
    $sql_puesto=sqlsrv_fetch_array($sql_puesto);
    //se recuperan los valores

    //se valida si las dos consultas obtuvieron resultados
   if ($sql_puesto >0 && $sql_usuario 0) {
       $tabla_puesto_puesto=$res['puesto'];
   }

    //aqui seleciono la tabla puesto y relaciono que el valor de id con el valor del puesto corespondiente a su id de puesto, igualamos y consegumos el puesto con $tabla_puesto_puesto
?>

here is the html in which this other error NOTE: I do not know why you use the table, but if you want it to be selected in the post, then make a ternary with the id and put selected

<select>
                                //tienes ":" y es ";"
        <?php foreach($tabla_puesto as $p):?>
<option value="<?php echo $p['id']; ?>">
        <?php echo $p['puesto']; ?>
</option>
         <?php endforeach; ?>
</select>}

I hope you serve Bro ... ReNiceCode ...

    
answered by 09.08.2018 / 19:16
source