Welcome Guest, Not a member yet? Register   Sign In
How to use database rows in a dropdown box?
#1

[eluser]Sinclair[/eluser]
Hi, I'am gettin nuts on How to do this:

Code:
function index()
    {
        $this->load->model('Ate_model');
        $this->data['data1'] = $this->Ate_model->ate_orientacao();

        
        echo form_dropdown('shirts', $this->data['data1']);

        
        $this->load->view('welcome_message', $this->data);
    
    }

How did you list database rows in a dropdown box?

Best Regards,
#2

[eluser]cdbrkpnt[/eluser]
Code:
$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

$shirts_on_sale = array('small', 'large');

echo form_dropdown('shirts', $options, 'large');

// Would produce:

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

this is what CI guide says ....
http://ellislab.com/codeigniter/user-gui...elper.html
#3

[eluser]Sinclair[/eluser]
Hi,

Thanks for the reply.

My array from database is:

Code:
Array ( [0] => Array ( [id_orientacao] => A ) [1] => Array ( [id_orientacao] => B ) [2] => Array ( [id_orientacao] => C ) )

This array does not do what I need. How can I change the array?


Best Regards,
#4

[eluser]saidai jagan[/eluser]
use foreach() ....
#5

[eluser]umefarooq[/eluser]
here is helper i have created for database driven dropdown

Code:
function form_db_dropdown($data=''){
        
        $rows = $data['rows'];
            
        (array_key_exists('options',$data))?$options = $data['options']:$options = '';
        
        if(count($rows)) {
            foreach($rows as $row){
                $options[$row->id] = $row->name;              
            }
        }
          
        (array_key_exists('selected',$data))?$selected = $data['selected']:$selected = '';
            
        (array_key_exists('js',$data))?$js = $data['js']:$js = '';
            
        return form_dropdown($data['name'],$options,$selected,$js);
            
    }

preparation data for dropdown

$sql = 'select id,name from table';

$query = $this->db->query($sql);

$data['parent'] = array('name'=>'parent','rows'=>$query->result(),'options'=>array('0'=>'Plese Select'),
                'selected'=>'select one');

echo form_db_dropdown($parent);

if you don't have id and name columns in your table then you can have query as like this

Code:
$sql = 'select col1 as id,col2 as name from table';
#6

[eluser]cdbrkpnt[/eluser]
[quote author="Sinclair" date="1261635797"]Hi, I'am gettin nuts on How to do this:

Code:
function index()
    {
        $this->load->model('Ate_model');
        $this->data['data1'] = $this->Ate_model->ate_orientacao();

        
        echo form_dropdown('shirts', $this->data['data1']);

        
        $this->load->view('welcome_message', $this->data);
    
    }

How did you list database rows in a dropdown box?

Best Regards,[/quote]

Code:
function index()
    {
        $this->load->model('Ate_model');
        $this->data['data1'] = $this->Ate_model->ate_orientacao();
        $dataarray = $this->data['data1'] ;

         $dropdown= array();
         foreach($dataarray as $index=>$rows){
           $dropdown[$index]= $rows['id_orientacao'];
         }
        echo form_dropdown('shirts', $dropdown);
        $this->load->view('welcome_message', $this->data);
    }

Check this once ..
#7

[eluser]gigas10[/eluser]
I do mine like this
Code:
CONTROLLER
function index(){
$data['depts'] = $this->MAdmin->getDepartments();
$this->load->view('index', $data);
}

MODEL
class MAdmin{
function getDepartments(){
$q = $this->db->get('global_departments');
if($q->num_rows() > 0) {
      return $q->result();
}
return null;
}

Then in my view:
&lt;?php
foreach($depts AS $row):
    $dept_dd[$row->department] = &$row->department;
endforeach;
?&gt;

&lt;?=form_dropdown('dept', $dept_dd);?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB