0

I am trying to create an HTML select field with option tags built from database values in CodeIgniter

When I try to retrieve the data of the database to the dropdown it only displays the first letter instead of the full database value text.

My view:

<select name="country" style="width: 200px;">
    <?php
    foreach ($countries as $country) {  
        echo '<option value="' . $country['PKCOUNTRY'] . '">' . $country['COUNTRYNAME'] . '</option>';
    }
    ?>
</select>

My model:

class Countries_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

     public function get_countries()
    {
        $query = $this->db->get('ISaathiDev.MCountry');
        if ($query->num_rows >= 0) {
            foreach ($query->result_array() as $row) {
                $data[$row['pkCountry']] = $row['CountryName'];
            }
            return $data;
        }
    }
}

My controller:

class countries extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Countries_model');
    }
    
    public function country()
    {
        $this->load->database();
        $data['countries'] = $this->Countries_model->get_countries();
        $this->load->view('countries_view', $data);
    }   
}

enter image description here

5
  • 1
    var_dump($countries); ? Commented Dec 28, 2013 at 11:54
  • what is $countries ????? array or object ? Commented Dec 28, 2013 at 11:55
  • Your $countries should be 2d array.but i don't think it was Commented Dec 28, 2013 at 11:55
  • Yeah, it's a datatype error. Passing into the foreach() statement. paste the var_dump()? Commented Dec 28, 2013 at 11:55
  • how about formatting your array to $newarray['PKCOUNTRY'] = 'COUNTRYNAME' then use the form_dropdown('country',$newarray) option? Commented Dec 28, 2013 at 13:22

2 Answers 2

0

Please use the following php code:-

get_Countries(){
    $this->db->select('pkCountry,CountryName');
    return $this->db->get('ISaathiDev.MCountry')->result_array();
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your model's loop is populating a dynamically-keyed, flat associative array, but your view is trying to access data as if it was an array of statically-keyed arrays. The foreach() in your view just needs to declare a key and a value variable to offer access to these dynamic values.

View:

<select name="country" style="width: 200px;">
    <?php
    foreach($countries as $pk => $name) {   
        printf(
            '<option value="%s">%s</option>',
            $pk,
            html_escape($name)
        );
    }
    ?>
</select>

Or if you prefer to use CodeIgniter's form builder functions for reduced code bloat and auto-escaping, ensure that the form helper is loaded ($this->load->helper('form');), then print the returned markup from form_dropdown().

echo form_dropdown('country', $countries, '', 'style="width:200px;"');

Your model can be reduced to this

public function getCountries(): array
{
    return array_column(
        $this->db
            ->get('ISaathiDev.MCountry')
            ->result_array(),
        'CountryName',
        'pkCountry'
    );
}

Comments

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.