Reputation: 961
In creating dropdown in my project encounters problems(errors). I'm very new to codeigniter.
Please check and advise what is wrong with with my code?
Model
Public function get_region()
{
$return = array();
$query = $this->db->get('region')->result_array();
if( is_array( $query ) && count( $query ) > 0 ){
$return[''] = 'please select';
foreach($query as $row)
{
$return[$row['id']] = $row['r_e_name'];
}
}
Controller
public function index()
{
$this->load->model('country_model');
$data['countries'] = $this->country_model->get(false);
$data['page_title'] = "Countries";
$data['return']=$this->country_model->get_region();
$this->template->show('countries', $data);
}
View
<tr>
<td>
<?php echo form_label('Region Name *', 'r_e_name'); ?>
</td>
<td>
<?php echo form_dropdown('r_e_name', $return);?>
</td>
</tr>
Errors
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: return
Filename: views/countries_add.php
Line Number: 17
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: helpers/form_helper.php
Line Number: 331
Upvotes: 0
Views: 9435
Reputation: 666
Model
function get_country() {
$return[''] = 'please select';
$this->db->order_by('c_e_name', 'asc');
$query = $this->db->get('country');
foreach($query->result_array() as $row){
$return[$row['id']] = $row['c_e_name'];
}
return $return;
}
function get_region(){
$return[''] = 'please select';
$query = $this->db->get('region');
foreach($query->result_array() as $row){
$return[$row['id']] = $row['r_e_name'];
}
return $return;
}
Controller
public function index()
{
$this->load->model('country_model');
$data['country'] = $this->country_model->get_country();
$data['region'] = $this->country_model->get_region();
$data['page_title'] = "Countries";
$this->template->show('countries', $data);
}
View
<tr>
<td><?php echo form_label('Country *', 'c_e_name'); ?></td>
<td><?php echo form_dropdown('c_e_name', $country);?></td>
</tr>
<tr>
<td><?php echo form_label('Region Name *', 'r_e_name'); ?></td>
<td><?php echo form_dropdown('r_e_name', $region);?></td>
</tr>
Upvotes: 2
Reputation: 1011
You have not returned the result set from model. Just put
return $return;
at the end of your function get_region() in model.
Upvotes: 0