CodeIgniter视图页面为空

Cho*_*mid 0 php mysql templates codeigniter

首先,我是CodeIgniter的新手.

我正在尝试CRUD使用CodeIgniter 开发一个简单的页面.到目前为止,我刚刚创建了View页面,没有任何CRUD功能.它只是一个普通的视图页面,其中包含一个包含从数据库中检索的模型数据的表.

这是我的视图页面的代码,工作正常,文件名 - show_users.php

<!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>CI CRUD</title>
</head>

<body>
<h2> Simple CI CRUD Application </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>
Run Code Online (Sandbox Code Playgroud)

我的模特是 - users_model.php

<?php

class Users_model extends CI_Model {

    function __construct()
    {
        parent::__construct();

        $this->load->database();

    }   

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

}

?>
Run Code Online (Sandbox Code Playgroud)

我的控制器是 - users.php

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

      public function index()
      {
          $data['user_list'] = $this->users_model->get_all_users();
          $this->load->view('show_users', $data);
      }
  }
  ?>
Run Code Online (Sandbox Code Playgroud)

我的数据库服务器是MySQL,数据库名称ci_test和我的表是users:

CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
`address` varchar(255) NOT NULL,
`mobile` varchar(15) NOT NULL,
PRIMARY KEY (`id`)
)
Run Code Online (Sandbox Code Playgroud)

我的视图页面显示了此输出:

产量

到现在为止还挺好.现在,每当我尝试修改我的视图页面时show_users.php,页面都会被加载,但是为空.

这是我修改过的视图页面的代码,我遇到了上面提到的问题:

  <!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>CI CRUD</title>
  <script type="text/javascript">
  function show_confirm(act,gotoid)
  {
        if(act=="edit")
             var r=confirm("Do you really want to edit?");
        else
             var r=confirm("Do you really want to delete?");
        if (r==true)
        {
             window.location="<?php echo base_url();?>index.php/users/"+act+"/"+gotoid;
        }
   }
   </script>

 </head>

 <body>
 <h2> Simple CI CRUD Application </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>
 <th scope="col" colspan="2">Action</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>
 <td width="40" align="left" ><a href="#" onClick="show_confirm('edit',<?php echo $u_key->id;?>)">Edit</a></td>
 <td width="40" align="left" ><a href="#" onClick="show_confirm('delete',<?php echo $u_key->id;?>)">Delete </a></td>
 </tr>
 <?php }?>
 <tr>
 <td colspan="7" align="right"> <a href="<?php echo base_url();?>index.php/user/add_form">Insert New User</a></td>
 </tr>
 </table>
 </body>

 </html>
Run Code Online (Sandbox Code Playgroud)

请注意,我的模型和控制器中没有任何改变.

任何人都可以指出问题出在哪里?对此有任何方便有效的解决方案吗?我正在使用Notepad ++进行开发,没有IDE.

编辑:虽然我找到了解决问题的方法并接受了答案,但我仍然对一个问题感到困惑.

在我的模型中 - users_model.php我有一条线:

    $this->load->database();
Run Code Online (Sandbox Code Playgroud)

但是在教程中,它是这样编写的:

    $this->load->database("ci_test");
Run Code Online (Sandbox Code Playgroud)

所以基本上,我忘了提供我的数据库名称,即ci_test.我仍然没有编辑我的模型代码,但我的视图页现在工作得很好.那有什么意义呢?是否有必要在里面提到数据库名称$this->load->database()?这是我的查看页面为空的原因之一吗?

小智 5

这不是您的视图页面的问题.转到配置文件夹打开autoload.php

$autoload['helper'] = array('url');
Run Code Online (Sandbox Code Playgroud)

另一种方式

取消注释 #$this->load->helper('url'); to $this->load->helper('url');

<?php

class Users_model extends CI_Model {

    function __construct()
    {
        parent::__construct();

        $this->load->database();
        $this->load->helper('url');

    }   

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

}

?>
Run Code Online (Sandbox Code Playgroud)