Reputation: 33
Codeigniter
I'm using a drop down and
My problem is that i'm not getting the value from the database
Here is my code
Table Fields:
id, name,category_online
Model code - 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();
}
}
?>
Here is my Controller -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);
}
}
Here is my view-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();
?>
This is what the out put looks like
http://jsbin.com/ecuzil/2/edit
No values from the database are being displayed
Upvotes: 0
Views: 976
Reputation: 28763
You need to return $data from the model file like
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;
}
Upvotes: 1