模型和控制器错误CI

时间:2014-06-27 08:42:33

标签: codeigniter

在我的codeigniter控制器用户和用户模型中,我正在尝试从我的数据库中获取用户,并在视图页面上将它们作为表格格式。

虽然我的模型上有两个错误。我正在尝试使用sql。数据库已自动加载。

不确定做错了什么

错误1

A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_DB_mysqli_result::$rows

Filename: user/user_model.php

Line Number: 46

错误2

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: user/user.php

Line Number: 75

我的用户模型

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model {

    public function getTotalUsers() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . $this->db->dbprefix  . "user`");

        return $query->row('total');
    }

    public function getUsers($data = array()) {
        $sql = "SELECT * FROM `" . $this->db->dbprefix . "user`";

        $sort_data = array(
            'username',
            'status',
            'date_added'
        );  

        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
            $sql .= " ORDER BY " . $data['sort'];   
        } else {
            $sql .= " ORDER BY username";   
        }

        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }           

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }   

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }

        $query = $this->db->query($sql);

        return $query->rows; // Line 46
    }   

}

用户控制器

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MX_Controller {
public function __construct() {
    parent::__construct();
    $this->load->library('users');
    $this->load->library('config_functions');
    $this->load->model('user/user_model');
    $this->load->model('user/User_group_model');
    $this->lang->load('user/user', 'english');
    $this->lang->load('english', 'english');
}
    protected function getList() {
    if (null !==($this->input->get('sort'))) {
        $sort = $this->input->get('sort');
    } else {
        $sort = 'username';
    }

    if (null !==($this->input->get('order'))) {
        $order = $this->input->get('order');
    } else {
        $order = 'ASC';
    }

    if (null !==($this->input->get('page'))) {
        $page = $this->input->get('page');
    } else {
        $page = 1;
    }

    $url = '';

    if (null !==($this->input->get('sort'))) {
        $url .= '&sort=' . $this->input->get('sort');
    }

    if (null !==($this->input->get('order'))) {
        $url .= '&order=' . $this->input->get('order');
    }

    if (null !==($this->input->get('page'))) {
        $url .= '&page=' . $this->input->get('page');
    }

    $data['users'] = array();

    $filter_data = array(
        'sort'  => $sort,
        'order' => $order,
        'start' => ($page - 1) * $this->config_functions->get('config_limit_admin'),
        'limit' => $this->config_functions->get('config_limit_admin')
    );

    $user_total = $this->user_model->getTotalUsers();

    $results = $this->user_model->getUsers($filter_data);

    foreach ($results as $result) { // Line 75
        $data['users'][] = array(
            'user_id'    => $result['user_id'],
            'username'   => $result['username'],
            'status'     => ($result['status'] ? $this->lang->line('text_enabled') : $this->lang->line('text_disabled')),
            'date_added' => date($this->lang->line('date_format_short'), strtotime($result['date_added']))
        );
    }   

    $this->load->view('template/user/user_list', $data);
}

}

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

更改

return $query->rows; // Line 46 

return $query->result_array();

$query->result()给出了对象表示法,$query->result_array()给出了数组符号 如果您使用了$query->result(),则foreach就像:

foreach ($results as $result) { // Line 75
    $data['users'][] = array(
        'user_id'    => $result->user_id,   //the object type
        ...
    );
}

答案 1 :(得分:0)

codeigniter活动记录类中不存在“$ query-&gt; rows()”。 你需要使用$ query-&gt; result()或$ query-&gt; result_array()代替