• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selection of countries by select from the database


I'm just starting to learn CodeIgniter.

I have a question.
The database created a table of countries and fields.

PHP Code:
 `country_idint(11NOT NULL,
 `namevarchar(128NOT NULL,
 `iso_code_2varchar(2NOT NULL,
 `iso_code_3varchar(3NOT NULL,
 `address_formattext NOT NULL,
 `postcode_requiredtinyint(1NOT NULL,
 `statustinyint(1NOT NULL DEFAULT '1'

At the moment I have on the page when registering the user, I need to write the country in the text field.

PHP Code:
<input class="form-control required" name="country" type="text" placeholder="<?php echo translate('country');?>" data-toggle="tooltip" title="<?php echo translate('country');?>"

I want the country instead of the field:

Select the select from the bats from the table country.
How to do it?

Help me please.

A model:


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);

            ->select('iso_code_2', 'name')

        if ($enabled_only) {

                ->where('status >', 0);

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

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

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

In a controller:


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

In the corresponding view:

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.


1. Created the model /application/models/Countries_model.php
Added your code.

2. In the file Home / application /controllers/Home.php added your code.

In file
How to insert your code?

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

If I have a choice form select.

Help me please.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.