Codeigniter: Using set_select() if dropdown data comes from the db - codeigniter

I have a dropdown <select> field which gets its values from the db. I'm currently looking for a way to use set_select() but have been unsuccessful. Here's my existing view:
<select name="userbase">
<?php echo $userbase_list; ?>
</select>
The variable $userbase_list is a string containing all the <option value="">text</option> html to be inserted. Since this <select> is generated from the db, does that mean I can't use the CI set_select() anymore?

$array_of_options= array("key_one"=>"val_one", "key_two"=>"val_two");
foreach($array_of_options as $k => $v)
{
$selected = ($v === $db_result) ? 'selected=selected' : '';
echo '<option '.$selected.' value='.$k.'>' . $v . '</option>';
}

Related

Two dropdown lists in codeigniter

I have two drop down lists in my form and want to get data from database. after getting first drop down successfully, I tried to do the same for the next one. But it gives an error.
The error I get
My code:
Model
Controller
View
Show your data in all foreach loop like this:
<?php foreach($borrower_name as $row){ ?>
<option value="<?php echo $row['borrower_name ']?>" ><?php echo $row['borrower_name '];?></option>
<?php } ?>
OR
foreach($borrower_name as $row)
{
echo '<option value="'.$row['borrower_name '].'">'.$row['borrower_name '].'</option>';
}
In your modal file replace return $query->result(); with return $query->result_array();

Codeigniter : Auto Fill DropDown From Database not working

I am trying to fetch data in a drop down field from database in Code Igniter. Here is the code for it :
<input type="hidden"
id="tour_package_id"
class="form-control"
name="tour_package_id" />
<?php $i='class="form-control"
name="tour_name"
id="tour_name"';
echo form_dropdown('tour_package_id', $tour_list,
set_value('tour_package_id', $tour_package_id),$i);?></div>
I am getting the following error for that.
Severity: Notice
Message: Undefined variable: tour_package_id
Filename: Packages/add_location.php
Line Number: 40
Tried all the stuff but its not working.
Thanks
Bhagya
is a drop down a combobox right?
try this:(this is a hard code just change it)
example:
Table(tbl_Category)
ID | Category_Name
1 Hardware
2 Software
Model
public function getCategory(){
$this->db->select('*');
$this->db->from('tbl_Category');
$query = $this->db->get();
return $query;
}
Controller(The trigger to view)(load the model also)
public function show(){
$this->data["result"] = $this->Model->getCategory();
$this->load->view('Category',$this->data); //this->data will get the result value to your view
}
The view(Category.php)
<select>
<?php foreach($result as $results){ ?>
<option value="<?php echo $results->ID; ?>"><?php echo $results->Category; ?></option>
<?php } ?>
</select>
The foreach inside the select makes it great! like if i add more Category the option will automatically adds up! Try this! hope this helps!

codeigniter form_dropdown return wrong value

My Model,
function get_partners_name()
{
$q = $this->db->select("id,fname,lname")
->from('partner');
$all_partners = $q->get()->result();
$data = array();
foreach($all_partners as $partner)
{
$data[$partner->id] = $partner->fname.' '.$partner->lname;
}
return $data;
}
my controller var_dump($partners) data is,
array (size=2)
38 => string 'Mahesh' (length=6)
40 => string 'Rahul' (length=4)
My view,
<?php array_unshift($partners, "-Select-");?>
<?php echo form_dropdown('parnter', $partners,'',''); ?>
so in dropdown i want to display,
<option value='0'>-Select-</option>
<option value='38'>Mahesh</option>
<option value='40'>Rahul</option>
But it display,
<option value='0'>-Select-</option>
<option value='1'>Mahesh</option>
<option value='2'>Rahul</option>
from the PHP manual.
'array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.'
so your keys are being renumbered when using unshift.
since the select option should never be selected I would suggest hardcoding it into the view. also don't give it a value if you don't want users to select it.
<select name='foo'>
<option disabled>-Select-</option>
<?php
foreach($partners as $partner){
echo '<option value="';
echo $partner->id;
echo '">';
echo $partner->name;
echo '</option>';
}
?>
</select>

CodeIgniter function for returning special dropdown

I want to create a function in my model that does something a bit different and need a bit of help.
I have a database table with names, description, ids etc etc .... there is only one of each name, but i want to return 3 entries for every 1 name, prefixed with a number on it.
So, for example, if i have 2 entries in my database with the names 'joe' and 'bob' .... i want my dropdown box to look like the following
<select>
<option value="joe1">joe1 - description</value>
<option value="joe2">joe2 - description</value>
<option value="joe3">joe3 - description</value>
<option value="bob1">bob1 - description</value>
<option value="bob2">bob2 - description</value>
<option value="bob3">bob3 - description</value>
and so on for additional entries in the DB.
i understand how to do a normal dropdown box, but not one like this.
Any help would be grand :)
Try something like this:
function get_users()
{
$this->db->select("id,name,desc");
$this->db->from("table_name");
$q = $this->db->get();
$rows = $q->row_array();
}
In controller method:
$data = array();
$data['users'] = $this->model_name->get_users();
$this->load->view('view_name',$data);
In view:
<?php
echo "<SELECT>";
foreach($users as $user)
{
for ($i=1; $i<=3; $i++)
{
$id = $user['id'].$i;
echo '<option value="'.$id.'">'.$id.' - '.$user['desc'].'</option>';
}
}
echo "</SELECT>";
?>

Repopulating Select form fields in CodeIgniter

I want to re-populate (i.e refill) values posted from a select form field after validation. I know codeigniter user guide proposed using set_select() function but their example from the guide assume you've hard-coded (in HTML) the array of values for the 'select' form element. In my case I used the Form Helper and my select field values are pulled from an array() like so:
$allstates = array('blah','blah2','blah3','blah4','blah5');
echo form_label('Select State Affiliate', 'state');
echo form_dropdown('state', $allstates);
Needless to say, $allstates array is dynamic and changes over time. So how do I code such that my select field can be re-populated with the value the user selected?
You can set the value that should be preselected via the third paramater of form_dropdown. The value that the user selected in the previous step is in $this->input->post('state'). So you would use:
echo form_dropdown('state', $allstates, $this->input->post('state'));
From the user guide:
The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected
$options = array(
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
);
$shirts_on_sale = array('small', 'large');
echo form_dropdown('shirts', $options, 'large');
// Would produce:
<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>
Sometimes, especially when working with objects, it might be easier to do this without the form helper:
<select name="my_select>
<?php foreach ( $my_object_with_data as $row ): ?>
<option value="" <?php if ( isset( $current_item->title ) AND $row->title == $current_item->title ) { echo 'selected="selected"';} ?> >
<?php echo $row->title; ?>
</option>
<?php endforeach; ?>
</select>
I know it's very ugly, but in a lot of cases this was the easiest way of doing it when working with objects instead of an associative array.

Resources