0

I want to used set_value for input where type=text and i have no problem with that.

I am confused to use dropdown value. i am fetch dropdown values from database and i could not understand where i use set_value .

My code:

<select class="form-control" name="sport_name" id="sport_name">
 <option value=''>--Select Sport--</option>
<?php foreach($getSport as $item):
 if($item->sport_name==$mySport) 
{?>
<option value="<?php echo $item->sport_id;  ?>" selected><?php echo $item->sport_name;  ?></option>
<?php }else{?>
<option value="<?php echo $item->sport_id;  ?>"><?php echo $item->sport_name;  ?></option>
 <? } endforeach; ?>
 </select>
1
  • As per docs, you use set_value for text inputs, and set_select for dropdown select elements. Commented Feb 1, 2016 at 21:27

4 Answers 4

1

You Can try This:

    <select class="form-control" name="sport_name" id="sport_name">
     <option value=''>--Select Sport--</option>
    <?php foreach($getSport as $item):?>
<option value="<?php echo $item->sport_id;  ?>" <?php if($item->sport_name==$mySport) 
    { echo "Selected";} ?>><?php echo $item->sport_name;  ?></option>
     <? } endforeach; ?>
     </select>
Sign up to request clarification or add additional context in comments.

Comments

0

As per the docs, you would not use set_value for a select element:

"Permits you to set the value of an input form (text input) or textarea.".

Rather, you would use set_select.

If you use a <select> menu, this function permits you to display the menu item that was selected. The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).

set_select($field[, $value = ''[, $default = FALSE]])

Parameters:

  • $field (string) – Field name

  • $value (string) – Value to check for

  • $default (string) – Whether the value is also a default one

Returns: ‘selected’ attribute or an empty string

Return type: string

Example:

<select class="form-control" name="sport_name" id="sport_name">
    <option value=''>--Select Sport--</option>
    <option value="one" <?php echo set_select('sport_name', 'one'); ?> >One</option>
    <option value="two" <?php echo set_select('sport_name', 'two'); ?> >Two</option>
    <option value="three" <?php echo set_select('sport_name', 'three'); ?> >Three</option>
</select>

Comments

0

your code should be work correctly, but best way: you can declare var called $selected and make comparision to assign it with selected word inside loop when the value of select == current selected value:

<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php 

foreach($getSport as $item):
    $selected='';
    if($item->sport_name==$mySport) 
    {
     $selected='selected';
    }
?>
<option value="<?php echo $item->sport_id;  ?>" <?php echo $selected; ?> >
<?php echo $item->sport_name;  ?></option>
<?php  endforeach; ?>
</select>

Comments

0

The best way of doing this, while remaining readability would be as follow:

 <select class="form-control" name="sport_name" id="sport_name">
      <option selected>--Select Sport--</option>
      <?php foreach($getSport as $item): ?>
           <option <?php if($item->sport_name == $mySport){ echo "selected"; } value="<?php echo $item->sport_id;?>"><?php echo $item->sport_name; ?></option>
      <?php endforeach; ?>
 </select>

Ofcourse this is thinking you've got your data correctly set as objects or if you have arrays you'd use

<?php echo $item['sport_name']; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.