0

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

2

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

10 Comments

The same errors show again even when i add "return $return;".
only one error occur foreach loop A PHP Error was encountered Severity: Warning Message: Invalid argument supplied for foreach() Filename: helpers/form_helper.php Line Number: 331
it seems like the error is in this line $data['countries'] = $this->country_model->get(false);
I think foreach error come from foreach($query->result_array() as $row) { $return[$row['id']] = $row['r_e_name']; }
that method seems not returning valid array to be supplied in your dropdown, because I already tested the script and there is nothing wrong. Just comment that line and it will work fine.
|
0

You have not returned the result set from model. Just put

return $return;

at the end of your function get_region() in model.

2 Comments

The same errors show again even when i add "return $return;".
try changing your variable name as return is a reserved word

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.