我對使用form_dropdown()
有疑問。關於使用Codeigniter的form_dropdown的問題
以下作品的代碼,但我不確定我是否要做新的數組中的視圖或有與陣列$games
做一個更好的方式 - 從$data['games']
通過呢?
我應該在控制器中完成所有的處理,並通過一個準備好的數組來發送下拉菜單嗎?
我在視圖中試過這個:echo form_dropdown('games', $games);
,但得到了錯誤「類stdClass的對象無法轉換爲字符串」,我認爲這是因爲它的對象數組,我必須將其轉換?
表:遊戲
GM_ID - 詮釋
GM_NAME - VAR
MODEL:
class Test_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_game_names()
{
$queryg = $this->db->query("SELECT * FROM games");
return $queryg->result();
}
}
控制器
class Test extends CI_Controller {
public function index()
{
$this->load->model('test_model');
$data['games'] = $this->test_model->get_game_names();
$this->load->view('view_test',$data);
}
}
VIEW
$this->load->helper('form');
echo form_open('send');
$list = array(); //is this the best way to do it??
foreach($games as $row)
{
$list[$row->GM_ID] = $row->GM_NAME; //is this the best way to do it??
}
echo form_dropdown('games', $list); //then pass this array?
echo form_close();