我对核心PHP有很好的掌握,我刚刚开始学习CodeIgniter。我根据CodeIgniter的教程创建了一些页面。但我被困在本教程中:http://www.codeigniter.com/user_guide/tutorial/news_section.html
以下是代码: /application/config/routes.php:
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['users/(:any)'] = 'users/view/$1';
$route['users'] = 'users';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
/application/models/Users_model.php:
class Users_model extends CI_Model
{ public function __construct()
{ $this->load->database();
}
public function get_users($username = FALSE)
{ if ($username === FALSE)
{ $query = $this->db->get('users');
return $query->result_array();
}
$query = $this->db->get_where('users', array('username' => $username));
return $query->row_array();
}
}
/application/controllers/Users.php:
class Users extends CI_Controller
{ public function __construct()
{ parent::__construct();
$this->load->model('users_model');
$this->load->helper('url_helper');
}
public function index()
{ $data['users'] = $this->users_model->get_users();
$data['title'] = 'List of Users';
$this->load->view('templates/header', $data);
$this->load->view('users/index', $data);
$this->load->view('templates/footer');
}
public function view($username = NULL)
{ $data['user'] = $this->users_model->get_users($username);
if (empty($data['user']))
{ show_404();
}
$data['title'] = $data['user']['display_name'];
$this->load->view('templates/header', $data);
$this->load->view('users/view', $data);
$this->load->view('templates/footer');
}
}
/application/views/users/index.php:
<div class='main'>
<table border='1'>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>Username</th>
</tr>
<?php foreach ($users as $user) { ?>
<tr>
<td><?php echo $user['display_name'] ?></td>
<td><?php echo $user['email'] ?></td>
<td>@<a href="<?php echo site_url('users/'.$user['username']) ?>"><?php echo $user['username'] ?></a></td>
</tr>
<?php } ?>
</table>
</div>
/application/views/users/view.php:
<h2><?php $user['display_name'] ?></h2>
<p>@<?php $user['username'] ?></p>
MySQL'用户'表的结构:
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(255),
username VARCHAR(255),
display_name VARCHAR(50)
);
(您可以在下面的代码中用您想要的域名或localhost替换....
(4个点)。)
..../index.php/users
页面的输出:
<html>
<head>
<title>List of Users</title>
</head>
<body>
<h1>List of Users</h1>
<div class='main'>
<table border='1'>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>Username</th>
</tr>
<tr>
<td>Nikunj Bhatt</td>
<td>nikunj@example.com</td>
<td>@<a href="..../index.php/users/NikunjBhatt">NikunjBhatt</a></td>
</tr>
</table>
</div>
</body>
</html>
此输出正确,但是当我点击用户名时,它会重定向到..../index.php/users/NikunjBhatt
页面,此页面会在Google Chrome中显示错误“此网页不可用”“ERR_CONNECTION_REFUSED”。
那么,问题出在哪里?我缺少什么?
答案 0 :(得分:0)
尝试编写如下代码: -
this.page.identifier = 'test';