如何在Codeigniter中从数据库填充下拉列表? [英] How to populate dropdownlist from database in Codeigniter?

查看:68
本文介绍了如何在Codeigniter中从数据库填充下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two tables: 1. STUDENT- fields are- (studentid,studentname,batch) 2. BATCH- fields are- (batchid,batchname)

我想从批处理名称字段(从表 BATCH)填充一个下拉列表,并基于批处理字段(从表 Student)选择批处理名称

I want to populate a dropdown list from the field "batchname" ( from the table "BATCH") and have the batchname selected based on the field-"batch" (from the table "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();

}    

现在,我不知道如何在下拉列表中填充风景。

Now, I cannot figure out how to populate the dropdown in the "View". Would you please kindly help me with this?

在此先感谢您。

推荐答案

您需要将要显示在下拉菜单中的选项放入数组中,就像这样

You need to put the options you want displayed in the drop down into an array, like so

$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()并使用它从数据库中获取行并将其放入数组中。

What I would do is create a function in my model, say $model->dropdown_options() and use that to get the rows from the database and put them into an array.

这篇关于如何在Codeigniter中从数据库填充下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆