0

Codeigniter
我正在使用下拉菜单,我的问题是我没有从数据库中获取值

这是我的代码

表字段:

id, name,category_online

型号代码 - category_model.php

<?PHP   
   class category_model extends CI_Model{
       public function __construct() {
    parent::__construct();
    }
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 $query->result_array();
}
}
?>

这是我的控制器-Welcome.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
   public function __construct() {
    parent::__construct();
    }
function add_content() {
        $data = array();
       $this->load->helper('form');
        $this->load->model('category_model');
        $data['select_options'] = $this->category_model->get_all_online_select();
        $this->load->view('add_content', $data);
}
}

这是我的视图-add_content.php

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

这就是输出的样子

http://jsbin.com/ecuzil/2/edit

没有显示数据库中的值

4

1 回答 1

1

您需要从模型文件中返回$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();
    $data = array();
    $my_data = $query->result_array();
    foreach($my_data as $row){
        $data[$row['id']]=$row['name'];
    }
    return $data;
 }
于 2013-05-30T11:42:37.883 回答