Reputation: 3
I'm new in using codeigniter so am a bit confused now.
When I pass the players data object to the view, it lists all correctly. I'm trying to use codeigniter's pagination.
I loaded the pagination in a function in my controller:
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);
}
In my view:
<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>
I loaded the pagination in my constructor method. It creates a pagination but the data is still in one page. When I click 2 or "next", it still does the same thing.
Upvotes: 0
Views: 157
Reputation: 366
Something like this
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);
}
Upvotes: 0
Reputation: 7475
1.Load the pagination library first:
$this->load->library('pagination');
2.Return the correct count from the database, make a different function to return the total count. dont count the data returned by the getAllPlayers()
. Use getAllPlayers()
to return 20 (per_page)
data to show in the page depending on the offset($this->uri->segment(3))
in your case.
Eg.
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();
}
Upvotes: 2