Codeigniter form_helper getting database rows to be values in select menu


Question

I am writing a form, which has a select menu in it, I want the values to pulled from the database, so I thought it would be something along these lines:

My view

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

My controller

function add_content() {
    $data = array();
    $this->is_logged_in();
    $this->load->model('category_model');
    $data['select_options'] = $this->category_model->get_all_online();
    $this->load->view('admin/content/add_content', $data);
}

my model

public function get_all_online() {
    $this->db->select('*');
    $this->db->from('category');
    $this->db->where('category_online', 1);
    $query = $this->db->get();

    return $query->result();

}

now when I place the $selected_options in the form dropdown I get this error,

A PHP Error was encountered

Severity: 4096

Message: Object of class stdClass could not be converted to string

Filename: helpers/form_helper.php

Line Number: 331

1
2
5/15/2016 11:44:01 PM

Accepted Answer

You need to pass an array to your dropdown, where the array key will be the value that is POSTed and the value will the text that is displayed.

To achieve this, change your controller like so:

function add_content() {
        $data = array();
        $this->is_logged_in();
        $this->load->model('category_model');
        $data['select_options'] = $this->category_model->get_all_online_select();
        $this->load->view('admin/content/add_content', $data);
}

and add this function to your model

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 $data;
}

That should do the trick

7
12/14/2009 10:20:31 PM

I personally hate to make assumptions in my Models about how my data will be used as that is the job of the controller. If you add a MY_array_helper.php and paste this in:

function array_to_select() {

$args = func_get_args();

$return = array();

switch(count($args)):

    case 3:
        foreach ($args[0] as $itteration):
            if(is_object($itteration)) $itteration = (array) $itteration;
            $return[$itteration[$args[1]]] = $itteration[$args[2]];
        endforeach;
    break;

    case 2:
        foreach ($args[0] as $key => $itteration):
            if(is_object($itteration)) $itteration = (array) $itteration;
            $return[$key] = $itteration[$args[1]];
        endforeach;
    break;

    case 1:
        foreach ($args[0] as $itteration):
            $return[$itteration] = $itteration;
        endforeach;
    break;

    default:
        return FALSE;
    break;

endswitch;

return $return;

}

Then you can do something like this:

function add_content() {
    $data = array();
    $this->is_logged_in();
    $this->load->model('category_model');
    $this->load->helper('array');
    $data['select_options'] = array_to_select($this->category_model->get_all_online(), 'id', 'title');
    $this->load->view('admin/content/add_content', $data);

}

That supports multi-dimensional arrays by passing in one or two keys, or single dimensional arrays by using the value as the value and the key.

Eg: array_to_select(array('value1', 'value2')) gives array('value1'=>'value1', 'value2'=>'value2')


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon