首先,我绝对是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`)
)
我的视图页显示输出:
到目前为止好。现在,无论何时我尝试修改我的查看页面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()
内的数据库名称?这是我查看页面为空的原因之一吗?
你是指什么意思页面被加载?但是空的?所以表中真的有行吗? – Ghost 2014-09-24 11:23:06
回声如何'
';看起来像 ? – 2014-09-24 11:24:03是的,你的意思是它像白色?或者它加载但表格是空的? – 2014-09-24 11:26:25