Add class to timezone select box in CodeIgniter - codeigniter

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

Related

Laravel old for selectbox options

So i'm working on a standalone laravel project (still a bit new to this..). Using the same form blade file adding and editing records. I've set old to the fields for validation checks, however i can't seem to get old working correctly for dropdowns. The select options are loaded in a foreach, and ive got a check to set it to selected if the record on edit matches the dropdown option selected, however when i add the old method to it, select a new option (E.G. "broker") and force validation to throw an error it works as expected & saves whatever i've selected (option "broker"), it saves the option on submit, but then when i go to edit another record (option "assessor") it displays the same option i previously selected (option "broker")...if any of that made sense!
Here's the foreach with option select from the blade file:
<select class="form-control" name="other_party_details[party_type_id]" id="other_party_details[party_type_id]">
<option value="">--- Please Select ---</option>
#foreach($partyTypes as $partyType)
<option value="{{ $partyType->id }}" {{old('other_party_details.party_type_id', !empty($claim->otherParty->party_type_id)) == $partyType->id ? 'selected' : '' }}>
{{ $partyType->label }}
</option>
#endforeach
</select>
What am i doing wrong?
I've exhausted google as for the most it seems to show results for multi select (mine is not), or using LaravelCollective (which i'm not), so running out of places to look!
Thanks in advance
You can try below code
<option value="{{$partyType->id}}" #if(old('party_type_id') == $partyType->id || $claim->otherParty->party_type_id == $partyType->id ) selected #endif> {{ $partyType->label }}</option>

How to add title attribute in drop down using {html_options} in smarty 3.1

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.

Call another controller function based on drop down selection

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/

Codeigniter dropdown default to last item?

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)));

How to assign attribute to dropdown menu in codeigniter?

I am using form helper to create a dropdown menu, I have looked at http://codeigniter.com/user_guide/helpers/form_helper.html but it doesn't specify how to assign attributes to dropdown menu. I want to add id and class to my dropdwon menu. How do I do that? Thanks for the help.
My code
$JNarray=array(
'company1'=>'company1,
'company2'=>'company2
);
echo form_label("company");
echo form_dropdown('company',$JNarray);
The above will create
<select name='company'> //I want to add 'id' and class attribute to select
<option value='company1'>company1</option>
<option value='company2'>company2</option>
</select>
You can do this with the fourth argument:
<?php
echo form_dropdown('company', $JNarray, NULL, 'id="company" class="some-class"');

Resources