0

Model:

function pop_room_type() {
 $this->db->select('rt_name')->from('room_type');
 $query=$this->db->get();
 return $query->result_array();
}

Controller:

function index() {
 $this->load->model('reservations_model');
 $data['room_type'] = $this->reservations_model->pop_room_type();
 //echo "<pre>";print_r($data['room_type']);echo"</pre>";
 $this->load->view('/main/new_reservation', $data);
}

View:

<?php 
//$js = 'name='$room_type'';
echo form_dropdown('room_type', $room_type);
?> 

However, there's a one BIG issue whereas the "name" of all select options are the same. All options in this dropdown carries the same value as in the column name of the (room_type) table: rt_name. I tried several things but still couldn't assign unique values to the name field of the options in the dropdown.

asked Jul 4, 2013 at 20:03
2
  • 1
    That's strange, the first argument of form_dropdown is the name attribute. So your code should give you name="roomtype". Commented Jul 4, 2013 at 20:14
  • 1
    @silkfire Refer this->ellislab.com/codeigniter/user-guide/helpers/form_helper.html :: According to that first argument is the name for the entire dropdown, not the micro options in it. :) You'll have a clear idea about the issue if you remove comment on the print_r line. See this: paste.ubuntu.com/5844745 Commented Jul 4, 2013 at 20:21

3 Answers 3

5

Because they are all from the same column.like this..

array(
 array('column_name'=>'value1'),
 array('column_name'=>'value2'),
 array('column_name'=>'value3'),
 array('column_name'=>'value4')
)

try this (controller):-

 $list=array();
 foreach($room_type as $type )
 {
 array_push($list, $type['column_name']); 
 }
 $this->load->view('/main/new_reservation', array('list'=>$list));

and in the view:-

 <?php 
 //$js = 'name='$room_type'';
 echo form_dropdown('room_type', $list);
 ?> 

now you have dropdown like this:-

<select name="room_type">
 <option value="0">value1</option>
 <option value="1">value2</option>
 <option value="2">value3</option>
 <option value="3">value4</option>
 </select>
answered Jul 4, 2013 at 20:27

5 Comments

Gave me following errors: Message: Undefined variable: room_type AND Message: Invalid argument supplied for foreach()
here $room_type=$this->reservations_model->pop_room_type(); now try this
Now another error: Message: Undefined index @ array_push($list, $type['column_name']);
dude as a example i used column_name here column_name is your table's column name :)
No. I used my table column name in the first place (rt_name) but it was a NO-GO! Any idea why?
1

You should construct an array where the keys are the value attribute of the option and values are the description of the option.

As stated on the docs an using an array such this:

$options = array(
 'small' => 'Small Shirt',
 'med' => 'Medium Shirt',
 'large' => 'Large Shirt',
 'xlarge' => 'Extra Large Shirt',
 );

will produce a select element as follow:

<select name="shirts">
 <option value="small">Small Shirt</option>
 <option value="med">Medium Shirt</option>
 <option value="large">Large Shirt</option>
 <option value="xlarge">Extra Large Shirt</option>
</select>

in your array the keys are always the same (the column name) you need to diferenciate them in order to achieve what you want.

Probably in the model you have to select also the room's id...

answered Jul 4, 2013 at 20:28

2 Comments

I agree with that. But if you go through the code again, you'll realize that this data were imported from a database table.
I see that, infact my advise is to fetch one more field from the database table so that you'll have both the keys and the values for your array: room_type_id => room_type or something like that
1

Here is the sample array that makes the options in the dropdown your array should look like this

$options = array(
 "" => "Select",
 "1" => "Test",
 "2" => "Testing",
);
echo form_dropdown('name_of_selectbox', $options);

it will display like

<select name="name_of_selectbox">
<option value="">Select</option>
<option value="1">Test</option>
<option value="2">Testing</option>
</select>

Other way you can do it like this

 $room_typearray=array();
 $room_typearray[]="Select";
 foreach($room_type as $room){
 $room_typearray[$room['rt_name']]=$room['rt_name'];
 }
echo form_dropdown('room_type', $room_typearray);

Here is the reply of your comment

function index() {
 $this->load->model('reservations_model');
 $data['room_type'] = $this->reservations_model->pop_room_type();
 //echo "<pre>";print_r($data['room_type']);echo"</pre>";
 $this->load->view('/main/new_reservation', $data);
}

You have to load the results in the $data['room_type'] and pass it to view first try var_dump($room_type); and see that you have data in it or not undefined error occurs when you have used any variable that is not defined

Hope it makes sense

answered Jul 4, 2013 at 20:29

1 Comment

Gave me following errors: Message: Undefined variable: room_type AND Message: Invalid argument supplied for foreach()

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.