CodeIgniter Forums
dropdown with CI - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: dropdown with CI (/showthread.php?tid=12282)



dropdown with CI - El Forum - 10-13-2008

[eluser]umefarooq[/eluser]
Hi
i want to create dropdown with CI im sending data to my view from controller
Code:
$data['width'] = array('name' => 'width','id' => 'width');
$data['height'] = array('name' => 'height','id' => 'height');
$data['scalecontent'] = array('name' => 'scalecontent','id' => 'scalecontent');

First to are text box but how can i send options for the third scalecontent along with data parameters.


dropdown with CI - El Forum - 10-13-2008

[eluser]hvalente13[/eluser]
Hi,

That's how I do it:

Model

Code:
function get_list(){
   $this->db->select('*')->from('table');
   $query = $this->db->get();

   if($query->num_rows()>0){
      $opts[''] = 'Select an option';
      foreach($query->result() as $row){
          $opts[$row->id] = $row->name;
      }
   }

   return $opts;
}

Controler

Code:
// with this option you can compare with the db data, and i will select the right option when page loads
// model function to retrieve db data

$data['name'] = $row->name;


$data['dd_data'] = $this->model_name->get_list();


View
Code:
<?php echo form_dropdown('name',$dd_data,$name); ?>

EDIT:

You can pass parameters by:

Code:
<?php echo form_dropdown(array('name'=>$scalecontent['name'],'id'=>$scalecontent['id']),$dd_data,$name); ?>

Guess I didn't forget anything. I was trying to make it generic...

Hope this helps
Hvalente13


dropdown with CI - El Forum - 10-14-2008

[eluser]umefarooq[/eluser]
hi really thanks for reply and its work very fine. but another problem im facing is that where dropdown is created on viewing source in name attribute of dropdown it show Array, not he name which im sending trough data here is the data which im sending for dropdown

$data['scalecontent'] = array('name' => 'scalecontent','id' => 'scalecontent');


dropdown with CI - El Forum - 10-14-2008

[eluser]hvalente13[/eluser]
It's all about this line here:

Code:
<?php echo form_dropdown(array('name'=>$scalecontent['name'],'id'=>$scalecontent['id']),$dd_data,$name); ?>

You have to specify $scalecontent['name'], because you have instantiated the array in your controller. That shouldn't give you Array, but the value for the 'name' key of your array scalecontent.

Check it out... I'll try to run it if there's any bug, but at first glance I don't think so