I create an array of numbers for ordering a gallery. I wanted to know if there was a way to have the dropdown default to the last item instead of the first? So with the example code when the page loads it would have the 6th element (value 5) selected as default.
<php? // Codeigniter that generates the select dropdown.
form_dropdown('order', $order);
?>
<select name="order">
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
<option value="5">6</option>
</select>
With your update, I am assuming you want to achieve the result below with the CodeIgniter form_dropdown() function?
<option value="5" selected="selected">6</option>
What you would have to do is add a few parameters to the function like this:
echo form_dropdown('order', '', '5');
Try this,
form_dropdown('order', $order, 5);
Check form_dropdown in CI's userguide for this
If you want to fetch the last element dynamically then use end() and key() functions to fetch the key of the last element of the array.
So here goes the code.
form_dropdown('order', $order, key(end($order)));
Related
Why doesn't this work?
Here is my select tag:
<select class="form-control" v-model="provider">
<option value="0">Select Provider</option>
<option v-for="provider in providers" :value="provider.provider_id">{{provider.name}}</option>
</select>
Code which loads the data:
loadProviders(){
axios.get('api/provider').then(({data}) => (this.providers = data.data));
data is then stored in:
data(){
return{
providers : {}
}
}
I've checked the developer networks tab of Chrome and it does return the data from the database.
However the value(provider.name) doesnt show up in the dropdown options menu.
This issue has already been solved: the model for Provider had an error all along.
The issue is, that you are destructuring the API response, but don't take into account that when assigning to data.
axios.get('api/provider').then((data) => (this.providers = data.data));
and
axios.get('api/provider').then(({data}) => (this.providers = data));
both work.
Update your select box
<select v-model="provider">
<option v-for="(value,key) in provider"
:value="value.provider_id">
{{value.name}}
</option>
</select>
Make sure you received proper data from your API.
assign data to provider variable. it will appeare in select box
I have a multi-select checkbox list. I want to show stored value in list using checkbox selected.
User informations are stored in Partner_Prefence table and user religion column named as p_religion
$profile_data= DB::table('partner_prefence')->select('p_religion')->first();
Fetching religions from religions table
$religion_data=DB::table('religion')->select('religion_id','religion_name')->orderby('religion_name')->get();
Multiselect list
<select multiple="multiple" name="religion[]">
#foreach($religion_data as $religion)
<option value="{{$religion->religion_id}}" {{$profile_data->p_religion == $religion->religion_id ? 'selected' : ''}}>{{$religion->religion_name}}</option>
#endforeach
</select>
I'm having trouble with showing which religions user have
{{$profile_data->p_religion == $religion->religion_id ? 'selected' : ''}}
as I understand you have multi select form, so you need show selected multiple column..
You're storing ids as a string but it's hard check that certain number in string. İf you convert string into a array, you can easly check with in_array() method. This method will return true if given value exist in given array
<select multiple="multiple" name="religion[]">
{{-- no need to explode every time, it will reduce your performance --}}
#php($religions = explode(',', $profile_data->p_religion))
#foreach($religion_data as $religion)
<option
value="{{$religion->religion_id}}"
{{--if user religion id exist in religions then mark as selected--}}
{{in_array($religion->religion_id,$religions) ? "selected" : ""}}>
{{$religion->religion_name}}
</option>
#endforeach
</select>
Is the p_religion column saving multiple IDs if it is a multi-select list? Would using in_array() work then instead of using $profile_data->p_religion == $religion->religion_id.
in_array ($religion->religion_id, explode(',', $profile_data->p_religion))
Added the explode() call on the off chance you are storing an imploded array.
You could also try and use the blade syntax for an if statement inline to see if it displays differently.
<select multiple="multiple" name="religion[]">
#foreach($religion_data as $religion)
<option value="{{$religion->religion_id}}" #if($profile_data->p_religion == $religion->religion_id) selected #endif>
{{$religion->religion_name}}
</option>
#endforeach
</select>
Here is my code.
PHP CODE
$smarty->assign('myOptions', array(
1800 => 'ABC',
9904 => 'DEF',
2003 => 'GHI')
);
$smarty->assign('mySelect', 9904);
SMARTY CODE
{html_options name=foo options=$myOptions selected=$mySelect}
DESIRED HTML OUTPUT
<select name="foo">
<option value="1800" title="ABC">ABC</option>
<option value="9904" title="DEF" selected>DEF</option>
<option value="2003" title="GHI">GHI</option>
</select>
You can't; not with the default html_options function at least. You can try to modify it or better create your own plugin to allow more parameters or complex arrays, but other than that, the best way to do it is generating the list of options in the template using a foreach.
Another option would be to use javascript to add the title attribute to each <option> for this or all selects once the page has been loaded.
By doing <?php $this->load->helper('date'); echo timezone_menu('UM8'); ?> I can get a bunch
of helpful timezone options in a select box from CI which generated as:
<select name="timezones">
<option value="UM12">(UTC -12:00) Baker/Howland Island</option>
<option value="UM11">(UTC -11:00) Samoa Time Zone, Niue</option>
...
</select>
However, I want to add a class (for example, <select name="timezones" class="form-control">) to that select box to inherit style from Bootstrap3. Anyone knows how to do this?
The second parameter sets the class(es) for the select element.
timezone_menu('UM8', 'form-control');
This is covered in the CI Date Helper documentation
I'm building a site that hosts stories that have multiple chapters and on my view I would like the user to be able to select from a drop down menu and go to the chapter they have selected.
I have the drop down populating with information from the database, I'm just having trouble calling another controller based on that selection.
In my view this is my drop down
<select>
<?foreach($chapter as $row) :?>
<option value="<?=$row->chapter_title?>"><?=$row->chapter_title?></option>
<?endforeach;?>
</select>
I did try and add a link to the controller in the option, but that didn't work.
<select>
<?foreach($chapter as $row) :?>
<option value="<?=$row->chapter_title?>">
<?=anchor('story/viewChapter/'.$row->chapter_id, $row->chapter_title);?>
</option>
<?endforeach;?>
</select>
Is there any other way I can do this that does not involve javascript?
Unfortunately, you will have to use javascript:
If your using jquery you could try something like this:
$('#select_id').change(function(){
var chapter = $(this).val();
$(location).attr('href','http://www.mysite.com/books?chapter='+chapter);
});
Here is an example:
http://jsbin.com/efidar/1/