2011-09-07 37 views
4

我想從數據庫中填充下拉列表。在我看來,我的文件有下面的代碼如何獲取form_dropdown()在Codeigniter中顯示選定的值?

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

現在下拉列表填充數據很好,但問題是我不明白的價值 - 「$一批」自動選擇頁面加載時。有趣的是,如果我回聲$批處理,在頁面的其他地方它顯示正確的數據,這意味着$批處理是好的。

這裏是我的控制器

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); 
     } 

你請請幫我解決這個問題。我希望在頁面加載時在下拉列表中自動選擇值「$批量」。

在此先感謝。

編輯...我對student_get模型($ ID)

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

謝謝:)

+0

@ Chris Schmitz。謝謝你的回覆。在選擇視圖中呈現的HTML is- $ batch = $ query ['batch']; echo form_dropdown('shirts',$ options,$ batch); –

回答

5

我認爲,這可能發生的是,在$批次的值可以匹配什麼樣的渲染在下拉菜單,但不是$ options中的特定選項的實際關鍵字,它將是html的value =「」部分。

例如...

// 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的評論:

的batchget()方法看起來像它返回你的$選擇以適當的格式排列和你student_get()方法正在返回一個row_array。看起來在視圖中,您將將student_get方法返回的其中一個鍵的值指定爲存儲在$ batch中的選定值,然後將其作爲第三個參數傳遞給form_dropdown()。

這一切似乎都是正確的。只要$ batch的值確實是$ options中的數組鍵之一,那麼form_dropdown()會將其中一個下拉選項設置爲已被選中。

+0

感謝您的回覆。你是對的,但可能你沒有注意到我已經在我的模型 - $ data [$ row-> batchname] = $ row-> batchname;我認爲這符合你的指示。請檢查我的模型,如果沒問題。再次感謝。 –

1

調試的東西。

var_dump()$optionsvar_dump() $batch,看看這兩個,看看你出錯了。

第三個選項必須是鍵的值,而不是標籤的值。

安東尼傑克可能是對的。

相關問題