我是使用codeigniter的新手,所以现在有点困惑。
当我将玩家数据对象传递给视图时,它会正确列出所有内容。我正在尝试使用codeigniter的分页。
我在我的控制器的一个函数中加载了分页:
public function playersHub(){
$playersData = $this->cms_model->getAllPlayers();
$pageConf = array(
'base_url' => base_url('home/playersHub/'),
'total_rows' => count($playersData),
'per_page' => 20,
'uri_segment' => 3,
);
$this->pagination->initialize($pageConf);
$data = array(
'players'=> $playersData,
'content'=> 'home',
'link' => $this->pagination->create_links()
);
$this->load->view('template/template',$data);
}
在我看来:
<div class="container-fluid">
<div class="row-fluid">
<table class = "table table-strip">
<?php foreach($players as $p): ?>
<tr>
<td><?php echo $p->ufbid; ?></td>
<td><?php echo $p->uname; ?></td>
<td><?php echo $p->ulocation; ?></td>
<td><?php echo $p->uemail; ?></td>
<td><?php echo $p->umobile; ?></td>
<td><?php echo (($p->ugrand == 0) ? 'no':'yes'); ?></td>
<td><?php echo $p->points; ?></td>
<td><?php echo $p->pticks; ?></td>
</tr>
<?php endforeach; ?>
<tr><td colspan = "9"><?php echo $link; ?></td></tr>
</table>
</div>
我在构造函数方法中加载了分页。 它创建了一个分页,但数据仍然在一个页面中。当我点击2或“下一步”时,它仍然会做同样的事情。
答案 0 :(得分:2)
1.首先加载分页库:
$this->load->library('pagination');
2.从数据库中返回正确的计数,使用不同的函数返回总计数。不计算getAllPlayers()
返回的数据。使用getAllPlayers()
返回20 (per_page)
数据,以便在页面中显示,具体取决于您的offset($this->uri->segment(3))
。
例如
function playersHub(){
$perPage = 20;
$playersData = $this->cms_model->getAllPlayers($perPage, $this->uri->segment(3));
$pageConf = array(
'base_url' => base_url('home/playersHub/'),
'total_rows' => $this->model_name->count_all(),
'per_page' => $perPage,
'uri_segment' => 3,
);
$this->pagination->initialize($pageConf);
$data = array(
'players'=> $playersData,
'content'=> 'home',
'link' => $this->pagination->create_links()
);
$this->load->view('template/template',$data);
}
#function to count all the data in the table
function count_all(){
return $this->db->count_all_results('table_name');
}
#function to return data from the table depending on the offset and limit
function getAllPlayers($limit, $offset = 0){
return $this->db->select('')->where('')->get('table_name', $limit, $offset)->result_array();
}
答案 1 :(得分:0)
像这样的东西
public function playersHub(){
$playersData = $this->cms_model->getAllPlayers();
$pageConf = array(
'base_url' => base_url('home/playersHub/'),
'total_rows' => count($playersData),
'per_page' => 20,
'uri_segment' => 3,
);
$this->pagination->initialize($pageConf);
//get the start
$start = ($this->uri->segment(3))?$this->uri->segment(3):0;
//apply the pagination limits.You may need to modify your model function
$playersData = $this->cms_model->getAllPlayers($start,$pageConf['per_page']);
$data = array(
'players'=> $playersData,
'content'=> 'home',
'link' => $this->pagination->create_links()
);
$this->load->view('template/template',$data);
}