2009-12-14 63 views
2

我正在寫一個表單,其中有一個選擇菜單,我想從數據庫中取出值,所以我認爲這將是沿着這些行的東西:Codeigniter form_helper獲取數據庫行是選擇菜單中的值

我的觀點

<?php 
    echo form_open('admin/save_content'); 
    echo form_fieldset(); 
    echo form_dropdown('categories', $select_options); 
    echo form_submit('category_submit', 'Submit'); 
    echo form_fieldset_close(); 
    echo form_close(); 
?> 

我控制器

function add_content() { 
    $data = array(); 
    $this->is_logged_in(); 
    $this->load->model('category_model'); 
    $data['select_options'] = $this->category_model->get_all_online(); 
    $this->load->view('admin/content/add_content', $data); 
} 

我的模型

public function get_all_online() { 
    $this->db->select('*'); 
    $this->db->from('category'); 
    $this->db->where('category_online', 1); 
    $query = $this->db->get(); 

    return $query->result(); 

} 
現在

,當我放置$selected_options形式下拉我得到這個錯誤,

遇到

一個PHP錯誤

嚴重性:4096

消息:Object類的stdClass的 不能被轉換爲字符串

文件名:幫手/ form_helper.php

行號:331

+0

太傻了,我不得不把明顯的查詢結果有陣後拉出來的查詢 - >結果 – Udders 2009-12-14 16:28:36

回答

7

你需要一個數組傳遞給你的下拉列表中,其中數組關鍵是將顯示的文本公佈在價值和價值。

要做到這一點,更改您的控制器像這樣:

function add_content() { 
     $data = array(); 
     $this->is_logged_in(); 
     $this->load->model('category_model'); 
     $data['select_options'] = $this->category_model->get_all_online_select(); 
     $this->load->view('admin/content/add_content', $data); 
} 

這個功能添加到模型

public function get_all_online_select() { 
     $this->db->select('id, name'); //change this to the two main values you want to use 
     $this->db->from('category'); 
     $this->db->where('category_online', 1); 
     $query = $this->db->get(); 
     foreach($query->result_array() as $row){ 
      $data[$row['id']]=$row['name']; 
     } 
     return $data; 
} 

這應該做的伎倆

1

您需要返回一個字符串數組,result()是一個對象數組。

也許試試這個模型中:

return $query->result_array(); 
3

我個人不喜歡做假設在我的模型中關於如何使用我的數據,因爲這是控制器的工作。如果添加MY_array_helper.php並粘貼此在:

function array_to_select() { 

$args = func_get_args(); 

$return = array(); 

switch(count($args)): 

    case 3: 
     foreach ($args[0] as $itteration): 
      if(is_object($itteration)) $itteration = (array) $itteration; 
      $return[$itteration[$args[1]]] = $itteration[$args[2]]; 
     endforeach; 
    break; 

    case 2: 
     foreach ($args[0] as $key => $itteration): 
      if(is_object($itteration)) $itteration = (array) $itteration; 
      $return[$key] = $itteration[$args[1]]; 
     endforeach; 
    break; 

    case 1: 
     foreach ($args[0] as $itteration): 
      $return[$itteration] = $itteration; 
     endforeach; 
    break; 

    default: 
     return FALSE; 
    break; 

endswitch; 

return $return; 

}

然後,你可以做這樣的事情:

function add_content() { 
    $data = array(); 
    $this->is_logged_in(); 
    $this->load->model('category_model'); 
    $this->load->helper('array'); 
    $data['select_options'] = array_to_select($this->category_model->get_all_online(), 'id', 'title'); 
    $this->load->view('admin/content/add_content', $data); 

}

支持多維數組通過傳遞一個或兩個密鑰,或通過使用值作爲值和密鑰的單維數組。

例如:array_to_select(陣列( 'VALUE1', '值2'))給出陣列( 'VALUE1'=> 'VALUE1', '值2'=> '值2')

0

在你的視圖中,可以在那裏添加foreach而不是在Model中。

<?php 
    echo form_open('admin/save_content'); 
    echo form_fieldset(); 
    foreach($select_options->result_array() as $row){ 
    $data[$row['id']]=$row['name']; 
    echo form_dropdown('categories', $row); 
    } 
    echo form_submit('category_submit', 'Submit'); 
    echo form_fieldset_close(); 
    echo form_close(); 
?> 

未測試。

0

我已經編輯菲爾醫生的助手陣列與只有兩個字段(ID &值)一個簡單的數據庫查詢工作。所以輔助類現在看起來像這樣:

<?php 
function array_to_select() { 
    //get args 
    $args = func_get_args(); 
    //get args key names 
    $keys = array_keys($args[0][0]); 
    //set return array 
    $return = array(); 
    foreach ($args[0] as $itteration){ 
     //$itteration[$keys[0]] is field id value, $itteration[$keys[1]] is field name value 
     $return[$itteration[$keys[0]]] = $itteration[$keys[1]]; 
    } 
    return $return; 
} 

而且你可以在你的控制器中再次使用它。
希望它是有用的。

相關問題