Monday, May 14, 2012

combo box not loading option on editform using PHP array

I've got a list of records with edit links on them. When i click on the link it takes me to an edit page with the results from the database.



I can retrieve the data successfully for all the text boxes.



I'm trying to implement this via an array which fails to work.



Here is my implementation in code fragments:



        # $data is from $data=mysqli_fetch_array($result)


$product=$data['product'];
echo $product.'<br />';

#initializing array to empty
$product_list=array("Remote"=>" ","TV"=>" ","Box"=>" ");

if (array_key_exists($product,$product_list)){


$product_list["'$product'"] = 'selected="selected" ';


}

print_r($product_list);


#combo box

<select name="products">
<option value="select">Select</option>
<option value="Remote"<?php echo @$product_list["'$product'"] ?>>Remote</option>
<option value="TV" <?php echo @$product_list["'$product'"] ?>> TV</option>
<option value="Box" <?php echo @$product_list["'$product'"] ?>> Box</option>
</select>


In the page that displays all the records if i hit edit on a record that has product 'Remote' i get the following output(as per echo statements above):



Remote
Array ( [Remote] => [TV] => [Box] => ['Remote'] => selected="selected" )


The HTML form displays:



 <select name="products">                   
<option value="select">Select</option>
<option selected="selected" value="Remote">Remote</option>
<option selected="selected" value="TV"> TV</option>
<option selected="selected" value="Box"> Box</option>
</select>


In the edit page if i select a record that has product 'TV' i get the following output:



 TV
Array ( [Remote] => [TV] => [Box] => ['TV'] => selected="selected" )


HTML output is same as above. And it always sets the option to the last product which is 'Box'.



Can somebody please advise on how i can fix this? thanks!





No comments:

Post a Comment