How to assign attribute to dropdown menu in codeigniter? - 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"');

Related

How to show selected checkbox in multiselect checkbox list in laravel?

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>

Add class to timezone select box in 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 category/subcategory dropdowns in magento custom module

I have created custom module for magento admin.There is a form that have 2 drop-downs(one for category and second for its subcategory).How can I relate theme.I want all sub categories in second drop-down on change of first dropdown(state,city like).How can I do this in magento using jquery/ajax.
here i am giving idea to how to get sub category list from ajax call. you should have to try with your self to call ajax on the change event of main category list and get from below code to generate sub category list.
<select id="category" class="myinput-text required-entry widthinput" name="category">
<?php
$parentid=5; // parent id which you want sub category
$categories=explode(',',Mage::getModel('catalog/category')->load($parentid)->getChildren());
foreach($categories as $cat){
$category=Mage::getModel('catalog/category')->load($cat);
?>
<option value="<?php echo $category->getId();?>"><?php echo $category->getName();?></option>
<?php } ?>
</select>
hope it will sure help you.

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

Resources