2014-09-24 237 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