Reputation: 1
I wanna display this query result as form_dropdown
Model
public function deptlist_view() {
$this -> load -> database();
$query = $this -> db -> query('select var_staffdepartment from tbl_admin');
return $query -> result();
}
Controller
public function view_dept() {
$this -> load -> model('model_database');
$deptdata['query'] = $this -> model_users -> deptlist_view();
$this -> load -> view('signup', $deptdata);
}
View
<?php $dept= ???? ?>
<?php echo form_open('welcome/signup_validation'); ?>
<table align="center">
<tr>
<td>
<p> Departments :</p>
</td>
<td><?php echo form_dropdown('staffdept', $dept); ?></td>
</table>
<?php echo form_close(); ?>
Could you please show how to fix this Problem? It would be very helpfull. thanks in advance.
Upvotes: 0
Views: 179
Reputation: 1324
Controller:
public function view_dept()
{
$this->load->model('model_database');
$query = $this->model_users->deptlist_view();
$data['staffdept'] = $query->result(); //
$this->load->view('signup', $data);
}
View:
<?php
$options = array('Select a department');
foreach ($staffdept as $staff):
$options[$staff->ID] = $staff->name; //insert the sql query results here
endforeach;
echo form_dropdown('staffdept', $options, $staff->ID);
?>
To add a class or ID to the dropdown, edit the last 'echo form_dropdown' line in your code
$other = "id='staffdept', class='staffdept'";
echo form_dropdown('staffdept', $options, $staff->ID, $other);
Upvotes: 0
Reputation: 36551
you need to pass the dropdown options in an array like..
$options = array(
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
);
so i created a new array .. added options in correct array format and displayed in the view
try this
Controller
public function view_dept() {
$this -> load -> model('model_database');
$query = $this -> model_users -> deptlist_view();
$tempArray=array();
foreach($query as $row){
$tempArray[$row->var_staffdepartment]=$row->var_staffdepartment;
}
$data['department']=$tempArray;
$this -> load -> view('signup', $data);
}
view
<?php echo form_open('welcome/signup_validation'); ?>
<table align="center">
<tr>
<td>
<p> Departments :</p>
</td>
<td><?php echo form_dropdown('staffdept', $department); ?></td>
</table>
<?php echo form_close(); ?>
Upvotes: 1