vendredi 1 juillet 2016

mysql fetch to a multidimensional associative arrays php

I have a sql query which returns a bunch of rows containing id,field1,field2,field3 and I want to create an array to be accessed this way:

$array[$id]['field1']

I tried this:

$stmt2->bind_result($idOpcion,$field1,$field2,$field3);
while ($stmt2->fetch()) {

            $options = array(
                "$id" => array(
                    "field1" => $field1,
                    "field2" => $field2,
                    "field2" => $field3)
                );
}

and to access:

$id = 501; //Just for clarify the example

<option <?php if $options[$id]['field1']==$dimension->getIdDimension()){echo "selected";}?>
value="<?=$dimension->getIdDimension()?>">
<?=$dimension->getNombreDimension()?></option>

And I get the message:

NOTICE: Undefined offset: 501

What I'm doing bad?

As you can imagine, the main goal is to load/select the proper option in a select, based on the stored data. Any other solution?.

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire