如何使form_dropdown()在Codeigniter中显示所选值? [英] How to get form_dropdown() show the selected value in Codeigniter?

查看:44
本文介绍了如何使form_dropdown()在Codeigniter中显示所选值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库中填充一个下拉列表。在我的查看文件中,我有以下代码

I am trying to populate a dropdown list from database. In my view file I have the following code

$batch= $query ['batch']; // I pull this data from a separate model 
echo form_dropdown('shirts', $options, $batch);

现在下拉列表填充的数据很好,但问题是我没有得到值-页面加载时自动选择 $ batch。有趣的是,如果我回显$ batch,则在页面其他地方显示正确的数据,这意味着$ batch可以。

Now the drop down list is populating data fine but the problem is I don't get the value-"$batch" automatically selected when the page loads. Interestingly if I echo $batch, elsewhere in the page it shows the correct data, which means $batch is okay.

这里是我的控制器

function update($id){
$this->load->model('mod_studentprofile');
             $data['query']= $this->mod_studentprofile->student_get($id);
             $data['options']= $this->mod_studentprofile->batchget();

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

这是我的模型

 function batchget() {

      $this->db->select('batchname');
       $records=$this->db->get('batch');

            $data=array();

                        foreach ($records->result() as $row)
                {
                    $data[$row->batchname] = $row->batchname;
                }

            return ($data);
        } 

请您帮我解决这个问题。我想在页面加载时在下拉列表中自动选择值- $ batch。

Would you please kindly help me to solve this problem. I want to have the value- "$batch" automatically selected in the dropdown list when the page loads.

在此先感谢。

EDit ... my_student_get($ id)的模型

EDit... my Model for student_get($id)

  function student_get($id)
    {
        $query=$this->db->get_where('student',array('studentid'=>$id));
        return $query->row_array();
    }      

谢谢:)

推荐答案

我认为可能正在发生的事情是$ batch中的值可能与下拉列表中呈现的内容匹配,但与该特定选项的$ options中的实际关键字不匹配,该值就是该值

I think that what's probably happening is that the value in $batch may be matching what's rendering in the dropdown but not the actual key in $options for that particular option which would be the value="" portion of the html.

例如...

// this wouldn't select 'foo' as you may be thinking
$options => array('0' => 'foo', '1' => 'bar');
$batch = 'foo';
echo form_dropdown('shirts', $options, $batch);

// this would select foo
$options => array('foo' => 'foo', 'bar' => 'bar');
$batch = 'foo';
echo form_dropdown('shirts', $options, $batch);

根据OP的评论进行编辑:

Edit in response to OP's comment:

batchget()方法看起来像以正确的格式返回$ options数组,而student_get()方法正在返回row_array。看来,在视图中,您正在将student_get方法返回的键之一的值分配为$ batch中存储的选定值,然后将其作为第三个参数传递给form_dropdown()。

The batchget() method looks like it returns your $options array in the proper format and your student_get() method is returning a row_array. It appears that in the view you're assigning the value of one of the keys returned by the student_get method to be the selected value stored in $batch which is then passed in as the third argument to form_dropdown().

这似乎都是正确的。只要$ batch的值确实是$ options中的数组键之一,则form_dropdown()会将下拉选项之一设置为已选定。

This all appears to be correct. As long as the value of $batch is indeed one of the array keys that is in $options then form_dropdown() will set one of the dropdown options as having been selected.

这篇关于如何使form_dropdown()在Codeigniter中显示所选值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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