我正在写一个表单,里面有一个选择菜单,我想从数据库中提取值,所以我认为应该是这样的:

我的看法

<?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(); 
?> 

我的 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); 
} 

我的模型

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(); 
 
} 

现在,当我将 $selected_options 放在表单下拉列表中时,出现此错误,

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

请您参考如下方法:

您需要将一个数组传递给您的下拉列表,其中数组键将是发布的值,而值将是显示的文本。

为此,请像这样更改您的 Controller :

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); 
} 

并将此功能添加到您的模型中

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

这应该可以解决问题


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!