phpmysqlsqlcodeignitermodels

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


Solution

  • 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>