Madhawa Ratnayake
Madhawa Ratnayake

Reputation: 189

CodeIgniter: Creating a dropdown field from an array, populated from database table data column

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.

Upvotes: 0

Views: 10358

Answers (3)

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

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

Upvotes: 1

sharif2008
sharif2008

Reputation: 2798

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>

Upvotes: 5

Dalen
Dalen

Reputation: 9006

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...

Upvotes: 1

Related Questions