Welcome Guest, Not a member yet? Register   Sign In
Selection of countries by select from the database
#2

A model:

Code:
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Countries_model extends CI_Model {

    public function dropdown($enabled_only = true) {

        $enabled_only = !empty($enabled_only);

        $this->db
            ->select('iso_code_2', 'name')
            ->from('countries');

        if ($enabled_only) {

            $this->db
                ->where('status >', 0);
        }

        $rows = $this->db
            ->order_by('name', 'asc')
            ->get()
            ->result_array();

        if (empty($rows)) {
            // Just in case, ensuring array type.
            $rows = array();
        }

        return array_column($rows, 'name', 'iso_code_2');
    }
}

In a controller:

Code:
$this->load->helper('form');
$this->load->model('countries_model');

$country_list = $this->countries_model->dropdown();
// Pass $country_list variable to the view.

In the corresponding view:

Code:
echo form_dropdown('country_code', $country_list, '' /* set_value('country_code', $country_code) */ , 'id="country_code" class="form-control required"');

This example could be simplified, bu I did not do it intentionally, for better readability.
Reply


Messages In This Thread
RE: Selection of countries by select from the database - by ivantcholakov - 03-21-2018, 08:54 AM



Theme © iAndrew 2016 - Forum software by © MyBB