codeigniter - model and controller error CI -


in codeigniter controller user , user model trying users database , have them table format on view page.

i getting 2 errors on model though. trying use sql. database auto loaded.

not sure done wrong

error 1

a php error encountered  severity: notice  message: undefined property: ci_db_mysqli_result::$rows  filename: user/user_model.php  line number: 46 

error 2

a php error encountered  severity: warning  message: invalid argument supplied foreach()  filename: user/user.php  line number: 75 

my user model

<?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(*) total `" . $this->db->dbprefix  . "user`");          return $query->row('total');     }      public function getusers($data = array()) {         $sql = "select * `" . $this->db->dbprefix . "user`";          $sort_data = array(             'username',             'status',             'date_added'         );            if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {             $sql .= " order " . $data['sort'];            } else {             $sql .= " order 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     }     } 

user controller

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

}

you may try this:

change

return $query->rows; // line 46  

to

return $query->result_array(); 

$query->result() gives object notation, , $query->result_array() gives array notation.
if have used $query->result(), foreach like:

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

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -