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'];
        }
    }
    return $return;
}

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

Recommended Answers

All 5 Replies

hai kaungzaythu,

i think [in your view page] you need to change this line from this

 <?php echo form_dropdown('r_e_name', $return);?>

to this

 <?php echo form_dropdown('r_e_name', $data['return']);?>

because in your controller code you placed all regions data in $data['return'] data array with index 'return'

so in our view, we need to retrive it as we stored

i think you got my point

let me know if you have any doubts in my clarification

Thanks radhakrishna.p

After that
Only one error still remains

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 331

Using $return in the view is the correct syntax as Codeigniter creates variables based off the array key names of the array you pass to the view() call. Except why are you using $this->template->show() instead of $this->load->view()?

Two other things.

1) return is a PHP reserved word and it's bad practice to use this as a variable name. Try something more identifying to your variable like $regions or $region_array.
2) Your model doesn't show a loading of the database class/connection to the database. Have you configured this in the config files?

How to search the catagories in the codeignitor

I don't know how to explain I'm new one for the codeignitor so pls help me to find

for eg: if we search in google search engine keyword used it shows suggestion like this how to do in codeignitor

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.