我是Codeigniter的新手。这是我在Codeigniter的第一次经历。我的分页有问题。我在论坛和博客上尝试了很多代码。最后,我按照教程链接:CodeIgniter from Scratch- Day 16 - Displaying & Sorting Tabular Data - Nettuts+
这是我的代码:
user.php的
public function index($offset=0)
{
$limit = 10;
$this->load->model('user_model');
$results = $this->user_model->get_all_users($limit,$offset);
$data['users'] = $results['rows'];
$data['num_results'] = $results['num_rows'];
$this->load->library('pagination');
$config = array();
$config['base_url'] = site_url('admin/user');
$config['total_rows'] = $data['num_results'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$date['pagination'] = $this->pagination->create_links();
$data['page_title'] = "List Of Users";
$this->load->view('admin/users', $data);
}
user_model.php
function get_all_users($limit, $offset)
{
$q = $this->db
->select('user_id,user_email,login_name,phone_num,user_status')
->from('tbl_user')
->limit($limit,$offset);
$ret['rows'] = $q->get()->result();
$q = $this->db
->select('COUNT(*) as count', FALSE)
->from('tbl_user');
$tmp = $q->get()->result();
$ret['num_rows'] = $tmp[0]->count;
return $ret;
}
users.php
<?php
$this->load->view('includes/header');
$this->load->view('includes/menu-admin');
?>
<div id="container">
<?php if(isset($users)) { ?>
<p><?php $this->load->view('includes/footer'); ?></p>
<table id="users_table" class="board">
<tr>
<th class="blue-gradient">#</th>
<th class="blue-gradient">Email Address</th>
<th class="blue-gradient">Login Name</th>
<th class="blue-gradient">User Status</th>
<th class="blue-gradient">Current Status</th>
</tr>
<?php $count=1; ?>
<?php foreach ($users as $user) { ?>
<tr id="user_<?php echo $user->user_id; ?>" class="darker-on-hover">
<td><?php echo $count; ?></td>
<td><?php echo anchor('admin/profile/view/'.$user->user_id,$user->user_email , 'class="view-profile-details"'); ?></td>
<td><?php echo $user->login_name; ?></td>
<td><?php echo $user->user_status; ?></td>
<td>
<?php echo anchor('admin/user/edit/'.$user->user_id , '<img src="'.base_url().'images/edit.png" title="Edit User"/>'); ?>
<?php echo anchor('admin/user/remove/'.$user->user_id , '<img src="'.base_url().'images/remove.png" title="Remove User"/>', 'class="remove-user-event"'); ?>
</td>
</tr>
<?php $count++; } ?>
</table>
<?php } ?>
<div id="pageNum">
<?php if(strlen($pagination)): ?>
Pages: <?php echo $pagination; ?>
<?php endif; ?>
</div>
</div>
如果我错了,请告诉我?
答案 0 :(得分:2)
像这样设置你的配置
$config['base_url'] = site_url('admin/user/');
$config['total_rows'] = $data['num_results'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
请注意/在site_url中。通常会发生分页的网址就像这样
http://localhost/myapp/index.php/admin/user10
哪个错了。如果你放一个斜线,你将有正确的分页链接
http://localhost/myapp/index.php/admin/user/10
编辑:
您的控制器应如下所示
Class User Extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index($offset=0)
{
$limit = 10;
$this->load->model('user_model');
$results = $this->user_model->get_all_users($limit,$offset);
$data['users'] = $results['rows'];
$data['num_results'] = $results['num_rows'];
$this->load->library('pagination');
$config = array();
$config['base_url'] = site_url('user/index');
$config['total_rows'] = $data['num_results'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$date['pagination'] = $this->pagination->create_links();
$data['page_title'] = "List Of Users";
$this->load->view('admin/users', $data);
}
}
请参阅班级名称和site_url('class/method')
以及您的案例site_url('user/index')