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

时间:2016-10-26 08:49:43

标签: php mysql codeigniter

我是CodeIgniter的新手,我一直在尝试使用数据库中的数据填充视图页面上的下拉列表,但没有成功。我尝试使用此问题的推荐,但下拉列表仍为空(display data from database to dropdown CodeIgniter

以下是我的观点:

<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/>

这是我的控制器:

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

这是我的模特:

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

表名是“citys”,然后相应的列头是“cityidd”和“city”

6 个答案:

答案 0 :(得分:1)

首先将SELECT查询更改为

SELECT cityidd, city FROM citys

在控制器的index()中将代码更改为

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

答案 1 :(得分:1)

你必须调用你的模型方法并将其传递给控制器​​中的视图,代码:

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

如果您正在使用Linux,请不要忘记大写和小写名称!

答案 2 :(得分:1)

那里有几个问题。进行如下更改

<div>we got {{catNumber}} cats</div>

最后模型

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

现在测试

答案 3 :(得分:0)

类Main_controller扩展了CI_Controller             {

viewmodel

这是我的模特:

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

试试这个

答案 4 :(得分:0)

进行这些更改

IN模型将数据转换为数组,因为您将其用作视图中的数组,因此请更改为

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

在控制器中

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

答案 5 :(得分:0)

您没有加载模型加载模型 例如:-

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

型号: - class Site_model扩展了CI_Model             {

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

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


                return $query->result();

            }
        }