• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to get property of non-object

#1
Hi all, 
I want to make a table with the members list of my website, but i have Trying to get property of non-object as error.

Here is my controller:


Code:
       public function memberslist(){
               $template = array(
                       'table_open'            => '<table class="table table-hover table-responsive" style= "background-color:white;">',
                       'tbody_open'            => '<tbody class="table-striped">',
               );
                                $this->table->set_template($template);
               $tableau = array($this->admin_model->memberslist());
               $data['tableau'] = $this->table->generate($tableau);
               $data['title'] = 'Liste des membres';
               $this->load->view('admin/memberslist', $data);
       }

Here is my model:


Code:
     public function memberslist(){
         $this->db->select('*');
         $this->db->from('users');
         $this->db->order_by('username', 'ASC');
         $query = $this->db->get();
         $userlist = array();
         foreach ($query->result_array() as $row)
         {
           $userlist .= array($row->id, $row->username, $row->vip, $row->administrator);
         }
         return $userslist;        
     }
I want make a table in my table with all members list, am i wrong?
Reply

#2
You can pass a database result object directly to table->generate(). It saves you all the trouble of looping through result_array().

The model becomes

PHP Code:
/**
 * @return an instanceof CI_DB_result
 */
public function memberslist()
{
 return 
$this->db
 
->order_by('username''ASC')
 ->
get('users');


The controller will work as you have it.
Reply

#3
(01-25-2018, 12:00 PM)dave friend Wrote: You can pass a database result object directly to table->generate(). It saves you all the trouble of looping through result_array().

The model becomes

PHP Code:
/**
 * @return an instanceof CI_DB_result
 */
public function memberslist()
{
 return 
$this->db
 
->order_by('username''ASC')
 ->
get('users');


The controller will work as you have it.

CI_DB_mysqli_result as array i have this error now
Reply

#4
I found a solution:

My model:

PHP Code:
     public function memberslist(){
 
         $this->db->select('id, username, vip, administrator');
 
         $this->db->from('users');
 
         $this->db->order_by('username''ASC');
 
         $query $this->db->get();
 
         $result $query->result_array();
 
         return $result       
      


My controller:
PHP Code:
       $template = array(
 
           'table_open' => '<table class="table table-hover table-responsive" style= "background-color:white;">',
 
           'tbody_open' => '<tbody class="table-striped">',
 
           );
 
       $this->table->set_template($template);
 
       $tableau $this->admin_model->memberslist();
 
       $data['tableau'] = $this->table->generate($tableau);
 
       $data['title'] = 'Liste des membres';
 
       $this->load->view('admin/memberslist'$data);
    } 
Reply

#5
I see where my advice went bad.

In the controller (your original post) the line

PHP Code:
$tableau = array($this->admin_model->memberslist()); 

should be changed to

PHP Code:
$tableau $this->admin_model->memberslist(); 
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.