2013-05-30 59 views
0


我使用的下拉和 我的問題是,我不是從數據庫笨下拉值未顯示

這裏所獲得的價值是我的代碼

表字段:

ID,姓名,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

沒有來自數據庫的值

回答

1

你需要從模型文件返回$數據

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; 
} 
+0

現在是遇到錯誤 一個PHP錯誤 嚴重性:注意 消息:未定義的變量:數據 文件名:型號/category_model.php 行號:14 遇到PHP錯誤 嚴重性:警告 消息:()提供的foreach無效參數 文件名:助手/ form_helper.php 行號:331 –

+0

然後定義爲在模型文件 – Gautam3164

+0

陣列I改變的返回$查詢 - > result_array的$數據();到$ data ..現在我得到那個錯誤 –