mach2
mach2

Reputation: 460

undefined method in model in codeigniter

I have made an admin page and user page, and i want to show the list of users who have registered in database when the admin logs in. For that i've created a model as follows,

  public function regi_users(){
$q = $this->db->query("SELECT username FROM public");

return $q;
}

and this i am accessing through a view i have created, to which, when admin logs in, he is redirected, as follows,

account.php

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
 </head>
 <body>
<?php

  $this->load->model('loginmodel');
    $qresult = $this->loginmodel->regi_user();

    foreach ($qresult as $row) {
      echo $row->username;
    }
 ?>

 </body>

but when my admin logs in, he's shown the following error,

Fatal error: Call to undefined method LoginModel::regi_user() in E:\wamp64\www\ci\application\controllers\account.php on line 11

what am i doing wrong here? I apologize if it is a silly question but i am kind of new to php

Thank you for your suggestions

Upvotes: 0

Views: 1633

Answers (4)

Rahul Mishra
Rahul Mishra

Reputation: 1

Model name should be LoginModel.php

Upvotes: 0

user9960496
user9960496

Reputation:

controller

class User extends CI_Controller 
{
   public function __construct() 
   {
      parent::__construct();        
      $this->load->model('YourModelName');
   }
    public function fetchUser()
    {
        $data['user']=$this->YourModelName->fetchUsers();
        $this->load->view('yourViewClass',$data);
    }
}

Model

class YourModelName extends CI_Model
{
    function __construct() 
    {
        $this->load->database();              
    }

    public function fetchUsers() 
    {
        $query = $this->db->select('*')->from('table_name');
        $query=$this->db->get();
        if($query->num_rows()>0)
        {
            $result=$query->result();
            return $result;
        }else{ 
            return 0;
        }
    }
}

view

<table>
  <thead>  
    <tr>
      <th>Firstname</th>         
    </tr> 
   </thead> 
   <tbody> 
     <?php  foreach ($user as $row) { ?>   
       <tr>
         <td><?php echo $row->username; ?></td>   
       </tr>     
     <?php } ?> 
  </tbody> 
</table>

Upvotes: 0

F5 Buddy
F5 Buddy

Reputation: 494

Controller

class User extends CI_Controller {
   public function __construct() {
      parent::__construct();        
      $this->load->model('loginmodel');
   }

  public function FunctionName($value='')
  { 
      $this->data["users"] = $this->loginmodel->regi_user();        
      $this->load->view('account',$this->data);
  }
}

Model

class Loginmodel extends CI_Model{
  function __construct() {
    parent::__construct();              
  }

  public function regi_user() {
    $query = $this->db->get('table_name')->result();
    return $query;
  }
}

View

<table class="table">
<thead>
  <tr>
    <th>Firstname</th>       
  </tr>
</thead>
<tbody>
<?php  foreach ($users as $row) { ?>
  <tr>
    <td><?php echo $row->username; ?></td>
  </tr>  
  <?php } ?>
</tbody>
</table>

Upvotes: 1

Danish Ali
Danish Ali

Reputation: 2352

Your query should be like this

public function regi_users(){
   return $this->db->select('username')->from('table_name')->get()->result_array();
}

Upvotes: 0

Related Questions