PHP/Mysql Database populated dropdown list spitting out index number instead of string value -
i can't figure out why drop down list, populated mysql, giving me number instead of string value. below code drop down list, displays item names. problem when pull selected item later number instead of string item name user selected.
<select name="product1" class="form-control" id="sel1"> <?php require('./phpconnect.php'); $dropdown = array(); $downquery = "select * masterdesc"; $response = @mysqli_query($dbc, $downquery); if($response){ while(($row = @mysqli_fetch_array($response,mysql_assoc))){ $dropdown[] = $row['description']; } } foreach($dropdown $key => $value){ echo '<option value=' . $key . '>' . $value . '</option>'; } echo "</select>"; mysqli_close($dbc); ?>
here code later used pull selected item.
$product1 = $_post['product1'];
any thoughts?
it spitting put index number because retrieving array description values (from "$dropdown[] = $row['description'];" have returned db , when there no keys listed - array index $key - therefore number
you need change following
foreach($dropdown $key => $value){ echo '<option value=' . $key . '>' . $value . '</option>'; }
to , set value of option same displayed content (or leave number , reference later $dropdown[index].
foreach($dropdown $value){ echo '<option value="' . $value. '">' . $value . '</option>'; }
Comments
Post a Comment