php - Select list array - prepopulate the choice to the top of the array -
i printing simple select list out looping through associative array populated elsewhere.
<select id="choice" name="choice"> <?php foreach ($this->choice $key => $value) { echo "<option name=".$value." value=".$key.">" .$value. "</option>" ; } ?> </select>
this select list gives options available array. however, trying pre-populate list selected value, example if user updating user information, set choice @ top of list (ideally without choice appearing further down list).
i have access data in variable have queried:
$this->selected_choice //value $this->selected_id //key
now have been testing solutions
<select id="choice" name="choice"> <option name="<?php echo $this->selected_choice; ?>" value="<?php echo $this->selected_id;?>">"<?php echo $this->selected_choice; ?></option> <?php foreach ($this->choice $key => $value) { echo "<option name=".$value." value=".$key.">" .$value. "</option>" ; } ?> </select>
this solution display users choice on top of list, foreach loop display option there no indication shouldn't.
this question guys, how can the selected_choice appears @ top of list, not appear in foreach array.
i hoping there simple solution problem, not important, come across quite lot.
option have no "name". name attribute for select tag. if option selected, need pass "selected" attribute in body of option ex: <option value="1" selected>tramtada</option>
do way:
<select id="choice" name="choice"> <?php foreach ($this->choice $key => $value) { echo "<option <?php if($key == $this->selected_id) {?> selected <?php } ?> value=".$key.">" .$value. "</option>" ; } ?> </select>
Comments
Post a Comment