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条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同