How do I pass a selectbox variable to the View from the Controller in CodeIgniter? - codeigniter

I want to pass a variable (selectbox id, which comes from database) to the view from the controller, but do not know how to do it.

Your original question wasn't that clear, but after reading the comments you could do this.
Controller
//whatever function you're using to populate your original array below.
$data['all'] = $this->model_name->getData();
//then run that data through a foreach loop to populate the dropdown variable.
foreach($data['all'])
{
$data['idDropDown'][$data['all']['id']]=$data['all']['ad'];
}
This will pass an array like: [455]=>Aliağa, [456]=>Balçova to the view as $idDropDown along with your original data as $all
Then in the view just used CI's form dropdown.
echo form_dropdown('id',$idDropDown);

Let's say you have a controller called article and a method index then in your view you will have :
<?php
echo form_open('article/index');
echo form_input('text');
echo form_close;
?>
And in your controller something like:
public function index()
{
if($this->input->post()) {
$this_is_catched_text = $_POST['text'];
}
}
This is without validation and other stuff. Just you get the idea how it works.

In your case it will be like this
$ilce = array(array("id" => 455, "il_id" => 35,"ad" => "Aliağa"),
array("id" => 456, "il_id" => 35, "ad" => "Balçova"));
$options = array();
foreach($ilce as $x) {
$options[$x['id']] = $x['ad'];
}
echo form_dropdown('names', $options);

Related

how to pass two arrays from model file to controller file

I want to return two arrays in a single function in the model and post the result in view but it gives an error.
And also I want to output a certain element of an array.
public function index(){
$this->load->model("model");
$array['thisarray'] = $this->model->Hello();
$arrayy['yep'] = $this->model->Hello();
$this->load->view("viewfile",$array);
$this->load->view("viewfile",$arrayy);
}
below is my model.php file.
public function Hello()
{
return ['title' => 'My Title','heading' => 'My Heading'];
return ['a'=> "helo",'b' =>"yello", 'c' =>"mello"];
}
below is my view file
<?php
echo "<pre>";
print_r($thisarray);
print_r($yep)
echo "</pre>"
?>
it gives an error saying yep is undefined variable.
this is impossible
a possible solution would be
your model
public function Hello()
{
return
[
'yep' => ['title' => 'My Title','heading' => 'My Heading'],
'thisarray' => ['a'=> "helo",'b' =>"yello", 'c' =>"mello"]
];
}
your controller
public function index()
{
$this->load->model("model");
$this->load->view("viewfile",$this->model->Hello());
}
and your view stays the same
you need to just change just your controller like this and you are all set.
public function index()
{
$this->load->model("model");
$array['thisarray'] = $this->model->Hello();
$array['yep'] = $this->model->Hello();
$this->load->view("viewfile", $array);
}
After modifying your controller like given above, you will be able to access you array as you are accessing it in your view.

Create a route that has uri-segment

I tried to call a function on the controller and the function I have created a route, but how to create a route that has uri-> segement ?
Example
$route['select-item'] = 'select_item';
Controllers
function select_item() {
$item = $this->uri->segment(3);
$data = array ('get_item' => $this->Model->My_item($item));
$this->load->view('Myview');
}
Views
<?php echo $row->item;?>
I suggest you use codeigniters wildcards on routes, You can go ahead and set your route to:
$route['select-item/(:any)'] = 'select_item/$1';
then on your controller, just do:
function select_item($item) {
$data = array ('get_item' => $this->Model->My_item($item));
$this->load->view('Myview',$data);
}
And the link in your view should work properly.

Is there a shorter way than this to check values when using same Add form for Edit

I have the same code for an Add and an Edit form. Therefore in the controller I need a check to check if a) POST vars submitted (for saving), and if not then b) the original values (for editing) and if not then no value (blank for Adding). I put them in a $data array to pass to the view. Then in the form I can put:
value="<?php echo $member_id;?>"
So my question is, in Codeigniter is there a shorter way than the following to check if POST, then if not check if the original data exists, and if not then its nothing.
$data = array(
'member_id' => ( isset($_POST['member_id']) ? $_POST['member_id'] : (isset($member->member_id ) ? $member->member_id : '') )
);
I know about set_value() but looks like that wont add in the current data when editing a form, so have not used that.
You can allways make function for it.
function get_value_or_default($array, $key, $default) {
return isset($array[$key] ? $array[$key] :
isset($default) ? $default : '';
}
Or even better:
function update_from_post($object) {
$data = array();
foreach ($object as $prop_name => value) {
$value = get_value_or_default($_POST, $prop_name, $object->{$prop_name});
$data[$prop_name] = $value;
}
Assuming you have different methods in the controller for create vs edit: (you can use the same view in different methods by specifying it in $this->load->view()):
Your create method would assume it was new, and always read the $_POST variables (if $_POST)
Your edit method would first load the object from the database, and then overwrite with $_POST variables if present.
Finally, CodeIgniter has the input helper:
$this->input->post('field_name');
returns false if that field is not in $_POST.
To use your code above:
create
$data = array(
'member_id' => $this->input->post('member_id') ? $this->input->post('member_id') : '')
);
edit
$data = array(
'member_id' => $this->input->post('member_id') ? $this->input->post('member_id') : $member->member_id )
);

How to get form_dropdown() show the selected value in Codeigniter?

I am trying to populate a dropdown list from database. In my view file I have the following code
$batch= $query ['batch']; // I pull this data from a separate model
echo form_dropdown('shirts', $options, $batch);
Now the drop down list is populating data fine but the problem is I don't get the value-"$batch" automatically selected when the page loads. Interestingly if I echo $batch, elsewhere in the page it shows the correct data, which means $batch is okay.
Here is my Controller
function update($id){
$this->load->model('mod_studentprofile');
$data['query']= $this->mod_studentprofile->student_get($id);
$data['options']= $this->mod_studentprofile->batchget();
$data['tab'] = "Update Student Information";
$data['main_content']='update_studentprofile';
$this->load->view('includes/template',$data);
}
And here is my model
function batchget() {
$this->db->select('batchname');
$records=$this->db->get('batch');
$data=array();
foreach ($records->result() as $row)
{
$data[$row->batchname] = $row->batchname;
}
return ($data);
}
Would you please kindly help me to solve this problem. I want to have the value- "$batch" automatically selected in the dropdown list when the page loads.
Thanks in Advance.
EDit... my Model for student_get($id)
function student_get($id)
{
$query=$this->db->get_where('student',array('studentid'=>$id));
return $query->row_array();
}
Thanks :)
I think that what's probably happening is that the value in $batch may be matching what's rendering in the dropdown but not the actual key in $options for that particular option which would be the value="" portion of the html.
for example...
// this wouldn't select 'foo' as you may be thinking
$options => array('0' => 'foo', '1' => 'bar');
$batch = 'foo';
echo form_dropdown('shirts', $options, $batch);
// this would select foo
$options => array('foo' => 'foo', 'bar' => 'bar');
$batch = 'foo';
echo form_dropdown('shirts', $options, $batch);
Edit in response to OP's comment:
The batchget() method looks like it returns your $options array in the proper format and your student_get() method is returning a row_array. It appears that in the view you're assigning the value of one of the keys returned by the student_get method to be the selected value stored in $batch which is then passed in as the third argument to form_dropdown().
This all appears to be correct. As long as the value of $batch is indeed one of the array keys that is in $options then form_dropdown() will set one of the dropdown options as having been selected.
Debug stuff.
var_dump() $options, var_dump() $batch, look at the two and see where you went wrong.
The third option must be the value of the key, not the value of the label.
Anthony Jack is probably right.

Code Igniter - form_dropdown selecting correct value from the database

Im having a few problems with the form_dropdown function in CodeIgniter .... My application is in 2 parts, a user goes in, enters a form and submits it .... once its submitted a admin can go in and edit that persons form and then save it to database.
So, to display the dropdown in the initial form, im using the following ( all the options in the dropdown are coming from the database )
Model:
function get_salaries_dropdown()
{
$this->db->from($this->table_name);
$this->db->order_by('id');
$result = $this->db->get();
$return = array();
if($result->num_rows() > 0){
$return[''] = 'please select';
foreach($result->result_array() as $row){
$return[$row['id']] = $row['salaryrange'];
}
}
return $return;
}
Then in the Controller:
$data['salaries'] = $this->salary_expectation->get_salaries_dropdown();
Then finally the View:
<?php echo form_dropdown('salaries', $salaries, set_value('salaries', $salaries)); ?>
That bit works perfect in displaying the dropdown filled with values for the user to select.
So, when the user selects a value, then hits save, its saved to the database.
On the Edit page which the admin see's, im using the same code to display the dropdown filled with options, but how do i get it to automatically choose the one thats been selected by the user in the initial stage?
Cheers,
According to Codeigniter documentation
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. You can also pass
an array of multiple items through the
third parameter, and CodeIgniter will
create a multiple select for you.
Your admin controller should have something like
$data['selected'] = $this->salary_expectation->get_salary_selected();
According to this, the admin view should be like this
<?php echo form_dropdown('salaries', $salaries, $selected_value); ?>
one nasty solution to select the <option> element of <select> generated by form_dropdown() function of the form_helper is using the post input sended.
I made this because any solutions I found doesn't display the value that the user select in the form neither set_selected nor set_vaule.
Well, in my controller I have:
$countries = $this->country_model->get_dropdown_array(); // The array have something like $countries[COUNTRY_ID] = COUNTRY_NAME
$data['countries']=$countries;
In my view:
$selected_country = $this->input->post('country');
echo form_dropdown('country',$countries,$selected_country);
And works fine !!! :)
The form_dropdown function has a third parameter for the selected option. Use it like this:
<?php echo form_dropdown('piece_type',
array(
'type1' => 'Firts type',
'type2' => 'Second option'
$selected_value,
'id = "piece_type"') ?>
I had the same problem but i have overcome on this problem using code igniter syntex.
Here is the solution.
Fisrt step
Before the loop initialize two arrays
$options = array();
$select = array();
Then in the loop write this instruction
foreach($result->result_array() as $row)
{
/////////Your Condition ////////////
if($row['id'] == $myarray['mycolumn'])
{
$options [$row['id']] = $row['salaryrange'];
$select = $row['id'] ;
}else{
$options [$row['id']] = $row['salaryrange'];
}
}
Now
echo form_dropdown('dropdown_name' , $options , $select);
It is working ok
For update case, you have pass corresponding value to view, if passing variable is like $ind_post(from controller ) then write this code like:
<?php echo form_dropdown('salaries', $salaries, $ind_post->salaries,''); ?>

Resources