2014-09-24 198 views
0

首先,我绝对是CodeIgniter的新手。CodeIgniter查看页面为空

我想用CodeIgniter开发一个简单的CRUD页面。到目前为止,我刚创建了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> 

我的模型是 - 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(); 
    } 

} 

?> 

我的控制器 - 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); 
     } 
    } 
    ?> 

我的数据库服务器是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`) 
) 

我的视图页显示输出:

Output

到目前为止好。现在,无论何时我尝试修改我的查看页面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> 

请注意,我还没有在我的模型和控制器改变任何事情。

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

编辑:虽然我找到了解决我的问题,并接受它作为答案,我仍然对一个问题感到困惑。

在我的模型 - users_model.php,我有一句台词:

$this->load->database(); 

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

$this->load->database("ci_test"); 

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

+0

你是指什么意思页面被加载?但是空的?所以表中真的有行吗? – Ghost 2014-09-24 11:23:06

+0

回声如何'

'; print_r($userlist); echo '
';看起来像 ? – 2014-09-24 11:24:03

+0

是的,你的意思是它像白色?或者它加载但表格是空的? – 2014-09-24 11:26:25

回答

5

这不是您的浏览页面的问题。 去config文件夹中打开autoload.php

$autoload['helper'] = array('url'); 

其他办法

取消对#$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(); 
    } 

} 

?> 
+0

只要我设置'$ autoload ['helper'] = array('url')',我的视图页就开始工作了。所以我将你的解决方案标记为答案。但是你还可以向我解释另一件事:我在模型中犯了另一个错误 - “users_model.php”。我写了'$ this-> load-> database()',并没有把我的数据库名称放在那里,它是'ci_test'。在本教程中,我发现它最初编写为'$ this-> load-> database(ci_test)'。这使用我的数据库名称的意义是什么?有必要吗? – 2014-09-25 04:31:27

+0

有两种连接数据库的方式: – Sanith 2014-09-29 04:24:24

0

我认为问题是在这一行:

window.location="<?php echo base_url();?>index.php/users/"+act+"/"+gotoid; 

也许你已经忘了负载帮手网址是什么?

+0

请进一步解释。什么是加载助手网址?我对CI完全陌生。 – 2014-09-24 11:41:22

+0

看这个:https://ellislab.com/codeigniter/user-guide/helpers/url_helper.html – user2883814 2014-09-24 11:44:06

1

而不是这个<?php echo base_url();?>索引。php/user/add_form 使用:<?php echo site_url('user/add_form');?>

+0

没有人,不工作。 – 2014-09-24 12:14:08

2

答案的评论$this->load->database()

有两种方式连接到数据库: “自动连接CT”中的application/config/autoload.php

$autoload['libraries'] = array('database'); 

手动连接

这个函数的第一个参数可以选择使用从您的配置文件指定一个特定的数据库组,或者你甚至可以提交连接未在配置文件中指定的数据库值。

$config['hostname'] = "localhost"; 
$config['username'] = "myusername"; 
$config['password'] = "mypassword"; 
$config['database'] = "mydatabase"; 
$config['dbdriver'] = "mysql"; 
$config['dbprefix'] = ""; 
$config['pconnect'] = FALSE; 
$config['db_debug'] = TRUE; 
$config['cache_on'] = FALSE; 
$config['cachedir'] = ""; 
$config['char_set'] = "utf8"; 
$config['dbcollat'] = "utf8_general_ci"; 

$this->load->database($config); 
+0

非常感谢。现在我明白了。我很困惑,因为那里没有任何代码的解释。 – 2014-09-29 05:08:30