【问题标题】:How to populate dropdownlist from database in Codeigniter?如何在 Codeigniter 中从数据库中填充下拉列表?
【发布时间】:2011-09-06 08:47:13
【问题描述】:

我有两张表: 1. STUDENT- 字段是- (studentid,studentname,batch) 2. BATCH- 字段是- (batchid,batchname)

我想从字段“batchname”(来自表“BATCH”)填充一个下拉列表,并根据字段-“batch”(来自表“Student”)选择批次名称

我的控制器 -

function update($id){
$this->load->model('mod_studentprofile');
$data['query']= $this->mod_studentprofile->student_get($id); //to retrieve information from the table "STUDENT" 
$data['query']= $this->mod_studentprofile->batch_get();

$data['main_content']='update_studentprofile';
$this->load->view('includes/template',$data);
}

我的模型 -

 function batch_get()
{
  $query = $this->db->get('batch');
  return $query->result();

}    

现在,我无法弄清楚如何在“视图”中填充下拉列表。你能帮我解决这个问题吗?

提前致谢。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    你需要把你想要在下拉菜单中显示的选项放到一个数组中,像这样

    $options = array(
      'red'   => 'Red',
      'green' => 'Green',
      'blue'  => 'Blue',
    );
    
    // The params here are:
    // 'color'    => name of the drop down
    // '$options' => options for the drop down
    // 'red'      => the selected value of the drop down
    echo form_dropdown('color', $options, 'red');
    

    我要做的是在我的模型中创建一个函数,比如$model->dropdown_options(),然后使用它从数据库中获取行并将它们放入一个数组中。

    【讨论】:

      【解决方案2】:

      请参阅 Jamie Rumbelow 的“MY_Model”类,他有一个完全符合您所描述的方法。

      https://github.com/jamierumbelow/codeigniter-base-model/blob/master/core/MY_Model.php

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-09
        • 1970-01-01
        • 1970-01-01
        • 2014-12-25
        • 1970-01-01
        相关资源
        最近更新 更多