dqzve68846 2018-06-07 23:48
浏览 34
已采纳

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

  • 写回答

3条回答 默认 最新

  • duanjianshen4871 2018-06-08 06:44
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算