【问题标题】:Notice Message: Array to string conversion注意消息:数组到字符串的转换
【发布时间】:2015-04-16 12:06:56
【问题描述】:

我是codeigniter和php的初学者,我面临这个问题

遇到了 PHP 错误

严重性:通知

消息:未定义变量:查询

文件名:inc/header_main_view.php

行号:175

遇到了 PHP 错误

查看:

<ul class="dropdown-menu">

    <li>
        <!-- inner menu: contains the actual data -->
        <ul class="menu">
            <?php foreach($query as $row) :?>
                <li><!-- Task item -->
                    <a href="#">
                        <h3>
                            <?php echo $row->id_task; ?><?php echo $row->name; ?>
                            <small class="pull-right">20%</small>
                        </h3>
                        <div class="progress xs">
                            <div class="progress-bar progress-bar-aqua" style="width: 20%" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100">
                                <span class="sr-only">20% Complete</span>
                            </div>
                        </div>
                    </a>
                </li><!-- end task item -->
            <?php endforeach; ?>
        </ul>
    </li>
    <li class="footer">
        <a href="<?=base_url()?>index.php/dashboard/g_tasks_menu">View all tasks</a>
    </li>
</ul>

控制器

public function get_todo($id = null)
    {
        $this->_require_login();
        $this->load->model('todo_model');
        if ($id != null) {
            $result = $this->todo_model->get([
                'id' => $id,
                'id_user' => $this->session->userdata('id_user')
            ]);
        } else {
            $result = $this->todo_model->get([
                'id_user' => $this->session->userdata('id_user')
            ]);
        }

        return $result;
    }

其他控制器

 public function index()
    {
        require('api.php');
        $api = new api();
        $query = $api->get_todo();

        $this->load->view('dashboard/inc/header_main_view', $query);
        $this->load->view('dashboard/admin_pages/dashboard_view');
        $this->load->view('dashboard/inc/footer_main_view');

    }

型号

 public function get($id = null, $order_by = null)
    {       
        if (is_numeric($id)) {
            $this->db->where($this->_primary_key, $id);
        } 

        if (is_array($id)) {
            foreach ($id as $_key => $_value) {
                $this->db->where($_key, $_value);
            }
        }

        $q = $this->db->get($this->_table);
        return $q->result_array();
    }

谁能告诉我为什么我会收到错误消息。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    会是这样的

    $data['query'] = $api->get_todo();
    $this->load->view('dashboard/inc/header_main_view', $data);
    

    【讨论】:

      【解决方案2】:

      您没有将变量传递给正确的视图。$query 在您的视图中不可用。CI 将数组或对象键转换为从控制器发送的变量。请参阅文档中的 Adding Dynamic Data to the View

      你需要像这样修复你的控制器

      $data['query'] = $api->get_todo();
      $this->load->view('dashboard/inc/header_main_view', $data);    
      //your view will get all key of $data as variable.
      //your view will get $query from above code
      

      【讨论】:

        猜你喜欢
        • 2023-03-06
        • 2017-08-11
        • 2013-04-18
        • 1970-01-01
        • 2013-11-03
        • 1970-01-01
        • 1970-01-01
        • 2016-08-29
        相关资源
        最近更新 更多