making a select dropdown from database in codeigniter - codeigniter

I am new with codeigniter.I want to make a select dorpdown that gets its value and title from database.I tried some codes but it did not work.There are my codes:
Model
function get_sec_info(){
$records=$this->db->query('SELECT sec_id,name FROM section');
if($records->num_rows() > 0)
return $records->result();}
Controller
function sec_ifo(){
$data['rec']=$this->mymodel->get_sec_info();
$this->load->view('article',$data);}
View
<select name="section">
<?php foreach($rec as $row) {?>
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>"
<?php } ?>
It does not show any error and any option to show

In your controller you are passing value like
$data['red']=$this->mymodel->get_sec_info();
and in you view you are taking value like
<?php foreach($rec as $row) {?>
So change the vairiable $rec to $red in your view like
<?php foreach($red as $row) {?>

Your option tag is badly formed. The opening option tag is missing its closing bracket, and the closing quotation mark for the option tag's value is in the wrong place.
Corrected:
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>

You should use CI form helper function instead of using html code
In model
function get_sec_info()
{
$records=$this->db->query('SELECT sec_id,name FROM section');
$ret_arr = array();
if($records->num_rows() > 0)
{
while($records->result() as $r)
$ret_arr[$r->sec_id] = $r->name;
}
return $records->result();
}
In your view
echo form_dropdown("section",$rec);
Remove your code below in view:
<select name="section">
<?php foreach($rec as $row) {?>
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>"
<?php } ?>
Make sure you have loaded form helper.

Related

select shows the top record in the db and not the selected

I have a select on a form that updates the backend db ok. My problem is that when the form is displayed, it always shows the top record in the db and not the selected.
I've googled and read answers on stackoverflow among others but everything I try either doesn't work or breaks functionality.
$registrants points to a select on a registrants table.
$affiliates points to a select on an affiliates table
Here is the select:
<select name="affiliate_id" class="form-control">
<?php foreach($affiliates as $display => $value) { ?>
<option value='<?= $value['affiliate_id'] ?>' <?php if($registrants['affiliate_id'] == $value['affiliate_id']) { ?>selected='selected'<?php } ?>><?= $value['affiliate_name'] ?>
</option>
<?php } ?>
</select>
Thanks for looking it over.
You could clean up your code a bit and create a variable $selected, which either is empty or a string 'selected':
<select name="affiliate_id" class="form-control">
<?php
foreach($affiliates as $value):
$selected=$registrants['affiliate_id'] == $value['affiliate_id']?'selected':'';
?>
<option value="<?=$value['affiliate_id']?>" <?=$selected?> >
<?= $value['affiliate_name'] ?>
</option>
<?php
endforeach;
?>
</select>
or you could use the CI function form_dropdown(), for that don't forget to load the form helper. This method simplifies a bit the use of drop down lists:
$this->load->helper('form');
$data=array('class'=>'$affiliates');
// get the selected item
<?php
foreach($affiliates as $value){
$selected=$registrants['affiliate_id'] == $value['affiliate_id']?$value['affiliate_id']:'';
if($selected){break;}
}
?>
<?=form_dropdown('affiliate_id', $affiliates, $selected, $data);?>

Getting session value without refreshing in Joomla

I made a Joomla module which contains a form element select box. On selecting, it submits the form and the value of selected item is stored in a session variable.
<?php
defined('_JEXEC') or die;
$session = JFactory::getSession();
if (isset($_POST["country"])){
$session->set('cont', $_POST["country"]);
}
?>
<div id="dropdown">
<form action="" method="post" name="country"><label for="country"></label>
<select id="country" name="country" onchange="this.form.submit()">
<?php
$rows = Array('UAE', 'KSA', 'OMAN');
foreach($rows as $row){
if($row == $session->get('cont')){
$isSelected = ' selected="selected"';
}
else {
$isSelected = '';
}
echo "<option ".$isSelected.">".$row."</option>";
}
?>
</select>
</form>
</div>
and in K2 item page the code is
<?php
if($session->get('cont') == "KSA"){
echo "SR-".number_format($this->item->ksaprice);
}elseif($session->get('cont') == "OMAN"){
echo "OMR-".number_format($this->item->omanprice);
}else
echo "AED-".number_format($this->item->uaeprice);
?>
Now the problem is when I select a country, page reloads, the session is created successfully but the price doesn't change as the page needs to be refreshed again to get the session's value.
I need to change the price without refreshing the page again.

populate dropdown in codeigniter form using set_value

i have a form that uses a drop down and im using codeigniter , form helper and form validation
so when i get a validation error in my form , all the correctly entered fields are populated using set_value of codeigniter , however this doesnt work for dropdown
im doing :
<?php echo form_dropdown('mid', $id_map, set_value('mid'), 'id="mid"') ?>
when get error in form , always the first value of dropdown is selected and not previously set
Any ideas , what am i doing wrong?
The correct method to use set_select() is
<select name="myselect">
<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option>
</select>
So Andras Ratz method/answer won't work.
Your method is correct.
<?php echo form_dropdown('mid', $id_map, set_value('mid'), 'id="mid"') ?>
However remember that set_value() is only going to return the value if you have setup a validation rule for it, in your case for mid. You could try as below -
<?php echo form_dropdown('mid', $id_map, (isset($_POST['mid']) ? $_POST['mid'] : ''), 'id="mid"') ?>
or just set a validation rule for mid too
It's because you have to use the set_select() function , not the set_value();
You can check that here:
http://codeigniter.com/user_guide/helpers/form_helper.html
And the right syntax:
EDIT :
<?php echo form_dropdown('mid', $id_map, set_select('mid'), 'id="mid"'); ?>
If you have a default value, that should go in the 2. parameter for set_select.
I hope this will work.
<?php // Change the values in this array to populate your dropdown as required
$options = array(
'active' => 'Active',
'inactive' => 'Inactive',
);
echo form_dropdown('mid', $options,$id_map, ' id="mid"');
?>
What about if the the form select content is selected from a foreach loop like this
<?php foreach ($roles as $role) {
echo "<option value=" . $role->id . ">" . $role->role_name . "</option>";
}
?>
If you do not have a validation rule on the actual dropdown field, then
echo form_dropdown('mid',$id_map);
is sufficient.

Magento - How do I get a list of all allowed countries in optionsarray?

I can get countries like this:
$countryCollection = Mage::getModel('directory/country')->getResourceCollection()->loadByStore();
And they are listed somewhere in the object, but, how do I toOptionsArray them out?
I'm after only the options per website rather than complete listi.e. I want Angola, Antarctica and so on out of the list. (Sorry Angolans and penguins.)
Its actually the obvious answer:
$countryList = Mage::getModel('directory/country')->getResourceCollection()
->loadByStore()
->toOptionArray(true);
also check out http://fishpig.co.uk/magento-tutorials/list-countries-for-drop-down-in-magento for more info on creating drop-down lists and such with the country list.
<?php
$_countries = Mage::getResourceModel('directory/country_collection')
->loadData()
->toOptionArray(false);
$allowed = Mage::getStoreConfig('general/country/allow');
if (count($_countries) > 0) { ?>
<div class="input-box">
<select name="country" id="country" class="validate-select" title="Country" >
<option value="">-- Please Select --</option>
<?php foreach($_countries as $_country){
if(!in_array($_country['value'],explode(',',$allowed))){
continue;
} ?>
<option value="<?php echo $_country['value']; ?>" <?php echo $formData['country'] == $_country['value'] ? ' selected="selected"' : '';?>>
<?php echo $_country['label'] ?>
</option>
<?php
} ?>
</select>
<?php } ?>

How to get timezone value

how to get the timezone drop down menu in frontend magento?
Here is the code to get all timezones:-
$timezones = Mage::getModel('core/locale')->getOptionTimezones();
echo "<pre>";
print_r($timezones);
echo "</pre>";
You will be getting an array. You can loop through the array and populate the selection/dropdown list like below:-
<select>
<?php
foreach($timezones as $timezone) {
?>
<option value="<?php echo $timezone['value']; ?>"><?php echo $timezone['label']; ?></option>
<?php
}
?>
</select>
Thanks.

Resources