从CodeIgniter中的数据库获取下拉列表

I am new to CodeIgniter and I have been trying to populate the drop down list on the view page with data from the database with no success. I tried using the recomendations from this question but the drop down is still empty (display data from database to dropdown CodeIgniter)

Here is my view:

<label>City</label>
<select class="form-control>
    <option value="">All</option>
    <?php
    foreach($groups as $city)
    {
        echo '<option value="'.$city['cityidd'].'">'.$city['city'].'</option>';
    }
    ?>  
</select> <br/>

Here is my controller:

<?php 
class Main_controller extends CI_Controller 
{
    function __construct() 
    { 
         parent::__construct(); 
         $this->load->helper('url'); 
         $this->load->database(); 
    } 

      public function index() 
    { 
         $this->load->helper('form'); 
         $this->load->view('supplier_add'); 
    } 
}

Here is my model:

class Site_model extends CI_Model
{
    public function __construct() 
    {
        /* Call the Model constructor */
        parent::__construct();
    }
    function getAllGroups()
    {
        $query = $this->db->query('SELECT city FROM citys');

        return $query->result();
    }
}

The table name is "citys" then the corresponding column heads are "cityidd" and "city"

There are several issue found there. Make changes as below

 function __construct(){ 
    parent::__construct(); 
    $this->load->helper('url');
    $this->load_model('Site_model');
    $this->load->database(); 
} 
public function index(){ 
  $this->load->helper('form');
  $data['groups'] = $this->site_model->getAllGroups();
  $this->load->view('supplier_add',$data); 
} 

Finally model

function getAllGroups(){
    $query = $this->db->query('SELECT cityidd,city FROM citys');
    return $query->result_array();
}

and now test

First change your SELECT query as

SELECT cityidd, city FROM citys

In the index() of controller change the code as

public function index() 
{ 
     $this->load->helper('form'); 
     $data['groups'] = $this->site_model->getAllGroups();
     $this->load->view('supplier_add',$data); 
} 

you have to call your model method and pass it to view in the controller, code:

public function index() 
{ 
$this->load->helper('form');
$this->load->model('site_model'); 
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data); 
} 

if you are working on linux dont forget upper and lowercase names!

class Main_controller extends CI_Controller {

        function __construct() 
        { 
             parent::__construct(); 
             $this->load->helper('url'); 
             $this->load->database(); 
             $this->load->model('Site_model');
        } 

          public function index() 
        { 
             $this->load->helper('form'); 
             $data['groups']=$this->Site_model->getAllGroups();
             $this->load->view('supplier_add',$data); 
        } 
        }

Here is my model:

        class Site_model extends CI_Model
        {

        public function __construct() 
        {
            /* Call the Model constructor */
            parent::__construct();
        }
        function getAllGroups()
            {

                $query = $this->db->query('SELECT * FROM citys');


                return $query->result();

            }
        }

Try like this

Make these changes

IN Model convert data to array as you are using it as array in view so change to this

function getAllGroups()
{
    $query = $this->db->query('SELECT * FROM citys');
    return $query->result_array();
}

In Controller

public function index() 
{ 
    $this->load->helper('form');
    $this->load->model('site_model'); 
    $data['groups'] = $this->site_model->getAllGroups();
    $this->load->view('supplier_add',$data); 
} 

You are not loading model load the Model for example:-

        function __construct() 
        { 
             parent::__construct(); 
             $this->load->helper('url');
             $this->load->model('Site_model') 
             $this->load->database(); 
        } 

          public function index() 
        { 
             $this->load->helper('form');
             $data['record']=$this->Site_model->getAllGroups()
             $this->load->view('supplier_add', $data); 
        } 
        }

Model:- class Site_model extends CI_Model {

        public function __construct() 
        {
            /* Call the Model constructor */
            parent::__construct();
        }
        function getAllGroups()
            {

                $query = $this->db->query('SELECT city FROM citys');


                return $query->result();

            }
        }