【问题标题】:undefined method in model in codeignitercodeigniter中模型中的未定义方法
【发布时间】:2018-11-17 23:30:45
【问题描述】:

我已经做了一个管理页面和用户页面,我想在管理员登录时显示在数据库中注册的用户列表。 为此,我创建了如下模型,

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

return $q;
}

这是我通过我创建的视图访问的,当管理员登录时,他被重定向到该视图,如下所示,

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>

但是当我的管理员登录时,他显示了以下错误,

致命错误:调用未定义的方法 LoginModel::regi_user() in E:\wamp64\www\ci\application\controllers\account.php 在第 11 行

我在这里做错了什么?如果这是一个愚蠢的问题,我深表歉意,但我对 php 有点陌生

感谢您的建议

【问题讨论】:

标签: php mysql sql codeigniter models


【解决方案1】:

控制器

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

型号

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

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

查看

<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】:

    你的查询应该是这样的

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

    【讨论】:

      【解决方案3】:

      控制器

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

      型号

      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;
              }
          }
      }
      

      查看

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

      【讨论】:

        猜你喜欢
        • 2013-10-20
        • 1970-01-01
        • 2020-04-04
        • 2016-11-04
        • 1970-01-01
        • 1970-01-01
        • 2019-02-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多