Reputation: 687
I tried a lot but cant seem to figure out this, any help appreciated. My articles have votes, and I have a page which is supposed to show most voted articles. Am using codeigniter btw. The controller:
function most_voted()
{
$per_page = 3;
$cur_page = $this->uri->segment(4);
/*
if($cur_page == "") $cur_page = 1;
else $cur_page = (integer)$cur_page;
*/
$offset = ($cur_page - 1) * $per_page;
if($offset < 0) $offset = 0;
$this->load->model('article_model');
$result_rows = $this->article_model->GetMostVoted($per_page,$cur_page);
$total_rows = sizeof($result_rows) + 10;
echo "total rows is : ".$total_rows.'<br>';
echo "cur page is : $cur_page <br>";
//$this->load->library('pagination');
$config['base_url'] = base_url().'articles/most_voted/page/';
$config['uri_segment'] = 4;
$config['num_links'] = 3;
$config['first_link'] = '<<First';
$config['last_link'] = 'Last>>';
$config['prev_link'] = '< Previous';
$config['next_link'] = 'Next >';
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$this->pagination->initialize($config);
$data['articles'] = $result_rows;
$data['view_file_name'] = 'articles/all_articles_view';
$this->load->view('includes/template',$data);
//echo $this->db->last_query();
}
The model :
function GetMostVoted($limit,$offset)
{
$this->db->order_by('votes','desc');
$q=$this->db->get('cgh_articles',$limit,$offset);
if($q->num_rows() > 0)
{
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
The problem : Issue is that though I get pagination like < Previous 1 2 3 4 5 Next > but clicking on 2 goes to url : page/3 clicking on 3 goes to url : page/6 clicking on 4 goes to url : page/9 and so on. I want a click on 2 to go to page/2, 3 to page/3, and so on. Any suggestions as to whats going wrong?
If you need any more info, please let me know, thanks.
Upvotes: 0
Views: 3391
Reputation: 4098
The way the pagination class works is that it returns the item at which the page should start.
For example, url : hostanme/controller/page/9 means that the page should be rendered starting with the 9th most voted article. See the example in the codeigniter documentation.
Also, you have $total_rows = sizeof($result_rows) + 10;
. Why is that ?
Upvotes: 3