我希望传递数据控制器直接在codelgiter中查看

时间:2018-04-19 12:26:19

标签: php codeigniter

  

遇到严重错误:通知

     

消息:未定义属性:Showdata :: $ Data_Model

     

文件名:controllers / Showdata.php

     

行号:23

     

回溯:

     

文件:C:\ xampp \ htdocs \ CURD \ application \ controllers \ Showdata.php行:   23功能:_error_handler

     

文件:C:\ xampp \ htdocs \ CURD \ index.php行:315功能:require_once

     

遇到未捕获的异常类型:错误

     

消息:在null

上调用成员函数Get_Data()      

文件名:C:\ xampp \ htdocs \ CURD \ application \ controllers \ Showdata.php

     

行号:23

     

回溯:

     

文件:C:\ xampp \ htdocs \ CURD \ index.php行:315功能:require_once

控制器

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class Showdata extends CI_Controller {
   function __construct() {
       parent::__construct();
       #$this->load->helper('url');
       $this->load->model('data_model');
    }

    public function index(){
        $data['user_list'] = $this->Data_Model->Get_Data();
        $this->load->view('data_view', $data);
    }
}
?>

查看

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>User Details</title>
</head>
<body>
    <h2> Simple Data Display </h2>
    <table width="600" border="1" cellpadding="5">
        <tr>
            <th scope="col">Id</th>
            <th scope="col">User Name</th>
            <th scope="col">Email</th>
            <th scope="col">Mobile</th>
            <th scope="col">Address</th>
        </tr>
        <?php foreach ($user_list as $u_key){ ?>
        <tr>
            <td><?php echo $u_key->id; ?></td>
            <td><?php echo $u_key->name; ?></td>
            <td><?php echo $u_key->email; ?></td>
            <td><?php echo $u_key->address; ?></td>
            <td><?php echo $u_key->mobile; ?></td>
        </tr>
        <?php }?>
    </table>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

你刚拼错了。

如下所示检查可能有所帮助。

public function index()
{
     $data['user_list'] = $this->data_Model->Get_Data();  // Use data_model instead Data_model
     $this->load->view('data_view', $data);
}

答案 1 :(得分:0)

   Please try this code it will help you. This is controller code

   class Test extends CI_Controller { 

     function __construct() { 
      parent::__construct(); 
      $this->load->helper('url'); 
      $this->load->database(); 
    } 

   public function view(){

     $this->load->model('Test_model');
     $stud_data = $this->Test_model->view();
     $data['data'] = $stud_data->result();
     $this->load->view('Display', $data); 
  }
 }

 This is view part

   <!DOCTYPE html> 
    <html lang = "en"> 
    <body> 
     <table>
       <tr>
        <th>Id</th>
        <th>Name</th>
         <th>Email</th>
        <th>Password</th>
        <th>Edit</th>
        <th>Delete</th>
      </tr>
      <?php
       foreach($data as $key){ ?>
      <tr>
       <td>
          <?php echo $key->id ?> 
       </td>
       <td>
          <?php echo $key->name ?> 
       </td>
       <td>
          <?php echo $key->email ?> 
       </td>
       <td>
          <?php echo $key->password ?> 
       </td>
       <td>
           <a href="#">Edit</a>
       </td>
       <td>
           <a href="#">Delete</a>
       </td>
     </tr>
      <?php } ?>
     </table>
    </body>
    </html>