How I send an array in PDO to use it in a smarty {html_options}

0

I am updating a section that uses mysql to PDO to deploy in a {html_options} of Smarty , and something that works well in mysql I do not know how to do it with PDO

This is the original code that works well now:

//Mando estados
$q="SELECT id, estado FROM estados";
$data=$db->execute($q);
$idEstados=array();
$estados=array();
for ($i=0; $i<sizeof($data);$i++) {
    array_push($idEstados,$data[$i]->id);
    array_push($estados,$data[$i]->estado);
}
$smarty->assign('idEstados',$idEstados);
$smarty->assign('estados',$estados);

Then in the .tpl everything is shown correctly, including "selected" which is the most important to me, otherwise I would do a {foreach} to display the states.

<select name="destino_idEstado" class="form-control">
    <option>Seleccione un Estado</option>   
    {html_options output="$estado" values="$idEstado" selected=$data->idEstado}
</select>

What I'm doing with PDO works fine if I do a {foreach}, but I need to use the "selected" and it's easier for me to do it with the {html_options} of smarty, but I do not know how to send the array from PDO so that work.

$sql1="SELECT id,estado FROM testados";

$stmt = $dba->prepare($sql1); 
$stmt->execute(); 

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

    $idEstado = $row['id'];
    $estado = $row['estado'];

    $smarty->assign('idEstado', $idEstado);
    $smarty->assign('estado',$estado);
}

This way it does NOT mark error, it just does not display the corresponding one.

Could someone guide me?

    
asked by CarlOSX 14.02.2017 в 20:05
source

1 answer

0

You are reassigning the php and smarty variables in each iteration of the loop, so they will only retain the last iteration.

Transforming the second so that it behaves as in your first example, the following should work.

$sql1="SELECT id,estado FROM estados";

$stmt = $dba->prepare($sql1); 
$stmt->execute(); 
$idEstados=array();
$estados=array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

    array_push($idEstados,$row['id']);
    array_push($estados,$row['estado']);

}

$smarty->assign('idEstados', $idEstados);
$smarty->assign('estados',$estados);
    
answered by 15.02.2017 / 00:16
source