I have form that needs data from the database. The form is for adding an office. An office belongs to a department, so when a new office is added it should be added to the right department. What I want to do is when I call the add_form view the dropdown in this view should be populated by data which is obtained from the database. Here is my code:
Here is the code that searches through the departments table and retrieve all the departments from the contacts_model
Code:
public function get_departments() {
//query
$query = $this->db->get('departments');
//return the result
return $query->result_array();
}
Here is the code that loads the add form view in the phones model
Code:
//if the passed value is one show the add department view
if($param == 2){
$data['title'] = 'Add College Office';
$departments['departs'] = $this->contacts_model->get_departments();
$this->load->view('templates/header', $data);
$this->load->view('addoffice.php', $departments);
$this->load->view('templates/footer');
}
Here is the code to display my view
Code:
<h1 align="center">ADD COLLEGE OFFICE</h1>
<div align="center">
<p><?php echo validation_errors(); ?></p>
<p><?php echo form_open('phones/addOffice') ?></p>
<table width="303" border="1">
<tr>
<th width="119" scope="row"><?php echo form_label('Office', 'office'); ?></th>
<td width="168">
<?php
$data = array(
'name' => 'office',
'id' => '',
'value' => '',
'maxlength' => '150',
'size' => '100',
'style' => 'width:50%');
echo form_input($data); ?>
</td>
</tr>
<tr>
<th scope="row"><?php echo form_label('Department', 'department'); ?></th>
<th scope="row"><?php
$options = array();
foreach($departs as $option){
$options = array('option' => $option['names'],
'value' => $option['id']);
}
//$options = $tmp;
echo form_dropdown('departments', $options, ''); ?></th>
<tr>
<th colspan="2" scope="row"><?php echo form_submit('submit', 'Add College Office') ?></th>
</tr>
</table>
<?php echo form_close(); ?>
</div>
When I run the code the dropbox only shows the first Department and its id from the database