0

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

1 Answer 1

1

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;
 }
Sign up to request clarification or add additional context in comments.

4 Comments

Now the error is A PHP Error was encountered Severity: Notice Message: Undefined variable: data Filename: models/category_model.php Line Number: 14 A PHP Error was encountered Severity: Warning Message: Invalid argument supplied for foreach() Filename: helpers/form_helper.php Line Number: 331
then define the $data as array in model file
I changed the return $query->result_array(); to $data ..Now I am getting that error
note that, $data should be processed in controller not in model (explicitly I mean foreach loop).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.