简体   繁体   中英

how to set_value use for dropdown input using codeigniter?

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>

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>

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>

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']; ?>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM