来自数据库的codeigniter下拉列表

时间:2012-08-30 10:46:59

标签: php codeigniter drop-down-menu

//模型

function shop_dropdown()
{
    $this->db->select('shop'); 
    $this->db->from('shop');
    //$this->db->where('category_online', 1);
    $query = $this->db->get();
    foreach($query->result_array() as $row)
    {
        $data[$row['id']]=$row['name'];
    }
    return $data;

}

控制器//

function shop_dropdown()
{
    $data = array();

    $this->load->model('shop_model');
    $shop['select_options'] =              $this->shop_model->shop_dropdown();
    $this->load->view('shop/product_view', $shop);


}

视图//

<?php 
    echo form_dropdown('shop', $select_options);

?>

这不是没有用。请帮我从database创建一个下拉列表。如果你可以编写一个新的代码。 提前谢谢

3 个答案:

答案 0 :(得分:0)

像这样修改

function shop_dropdown()
{
   $data = array();
   $this->load->model('shop_model');
   $shop = $this->shop_model->shop_dropdown();
   $this->load->view('shop/product_view', $shop);
}

并在您的视图中

echo form_dropdown('shop', $shop->option);//option is an value taking form database

就是它。如果对你有用,则接受答案

答案 1 :(得分:0)

我不确定你是否有自动加载表单助手,如果你没有,你不能使用form_dropdown函数,除非你在控制器中加载它。我没有看到你在任何地方加载表单助手。

http://codeigniter.com/user_guide/helpers/form_helper.html

答案 2 :(得分:0)

您正在选择模型中的“商店”列。 我认为你的模型应该是这样的

function shop_dropdown()
{
$this->db->select('id,name'); //column names you want to select, can be optional if you want to select all columns. 
$this->db->from('shop');   //table name, required
//$this->db->where('category_online', 1);
$query = $this->db->get();
foreach($query->result_array() as $row)
{
 $data[$row['id']]=$row['name'];  //make sure 'id' and 'name' ,columns are present in table
}
return $data;

}

我希望你已经编辑了application / config / databse.php

相关问题