duanjucong3124 2016-10-26 08:49 采纳率: 100%
浏览 52
已采纳

从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"

  • 写回答

6条回答 默认 最新

  • doukeng1922 2016-10-26 09:00
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥66 定制开发肯德基自动化网站下单软件
  • ¥20 vscode虚拟环境依赖包未安装
  • ¥15 odoo17关于owl开发js代码问题
  • ¥15 光纤中多普勒频移公式的推导
  • ¥15 怎么制作一个人脸识别门禁系统
  • ¥20 大华dss监控平台网络关闭登不进去
  • ¥15 请使用蚁群算法解决下列问题,并给出我完整的代码
  • ¥20 关于php录入完成后,批量更新数据库
  • ¥15 请教往复密封润滑问题
  • ¥15 cocos creator发布ios包