0

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"

2
  • inside the construct() you load module like this $this->load->model('Site_model');. Then in index function $data['city']=$this->Site_model->getAllGroups(); Commented Oct 26, 2016 at 8:53
  • is there any error or show output you are getting? Commented Oct 26, 2016 at 8:53

6 Answers 6

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

What if I have many drop down lists that I want to populate from the database? Do I add the query for the other drop downs or how do I go about here? E.g the table is still "citys" then the other corresponding column heads are "id" and "paymentmodes" for another option
Yes use different query for that with different method like getAllGroups()
1

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

Comments

1

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!

Comments

0

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

Comments

0

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

Comments

0

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

            }
        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.