CodeIgniter Forums
Selection of countries by select from the database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: Selection of countries by select from the database (/showthread.php?tid=70291)



Selection of countries by select from the database - grechca - 03-21-2018

Hello.

I'm just starting to learn CodeIgniter.

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

PHP Code:
CREATE TABLE IF NOT EXISTS `country` (
 
 `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'
ENGINE=MyISAM AUTO_INCREMENT=258 DEFAULT CHARSET=utf8

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.


RE: Selection of countries by select from the database - ivantcholakov - 03-21-2018

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.


RE: Selection of countries by select from the database - grechca - 07-28-2018

@ivantcholakov

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
/application/views/front/user/register/index.php
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.