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.

2

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>
5
  • Gave me following errors: Message: Undefined variable: room_type AND Message: Invalid argument supplied for foreach() Jul 4, 2013 at 20:49
  • 2
    here $room_type=$this->reservations_model->pop_room_type(); now try this
    – sharif2008
    Jul 4, 2013 at 20:51
  • Now another error: Message: Undefined index @ array_push($list, $type['column_name']); Jul 4, 2013 at 20:56
  • dude as a example i used column_name here column_name is your table's column name :)
    – sharif2008
    Jul 4, 2013 at 21:01
  • No. I used my table column name in the first place (rt_name) but it was a NO-GO! Any idea why? Jul 4, 2013 at 21:06
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...

2
  • I agree with that. But if you go through the code again, you'll realize that this data were imported from a database table. Jul 4, 2013 at 20:50
  • 1
    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
    – Dalen
    Jul 4, 2013 at 21:15
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

1
  • Gave me following errors: Message: Undefined variable: room_type AND Message: Invalid argument supplied for foreach() Jul 4, 2013 at 20:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.