首先,我絕對是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