Codeigniter form_helper getting database rows to be values in select menu

北战南征 提交于 2019-12-19 04:25:15

问题


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:


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




回答2:


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')




回答3:


You need to return an array of strings, result() is an array of objects.

Maybe try this in your model:

return $query->result_array();



回答4:


In your view, you can add foreach there instead of in Model.

<?php
   echo form_open('admin/save_content');
   echo form_fieldset();
   foreach($select_options->result_array() as $row){
   $data[$row['id']]=$row['name'];
   echo form_dropdown('categories', $row);
   }
   echo form_submit('category_submit', 'Submit');
   echo form_fieldset_close();
   echo form_close();
?>

Not tested.




回答5:


I have edited Phil Surgeon's array helper to work with a simple db query with only two fields (id & value). So the helper class now looks like this:

<?php
function array_to_select() {
    //get args
    $args = func_get_args();
    //get args key names
    $keys = array_keys($args[0][0]);
    //set return array
    $return = array();
    foreach ($args[0] as $itteration){
        //$itteration[$keys[0]] is field id value,  $itteration[$keys[1]] is field name value
        $return[$itteration[$keys[0]]] = $itteration[$keys[1]];
    }
    return $return;
}

And you can use it again in your controller.
Hope it's usefull.




回答6:


form drop down with lot of options codeigniter form drop down menu with validation class also



来源:https://stackoverflow.com/questions/1901703/codeigniter-form-helper-getting-database-rows-to-be-values-in-select-menu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!