codeigniter pagination not working as expected -
i tried lot cant seem figure out this, appreciated. articles have votes, , have page supposed show voted articles. using codeigniter btw. 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 : ".$total_rows.'<br>'; echo "cur page : $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() $row) { $data[] = $row; } return $data; } }
the problem : issue though pagination < previous 1 2 3 4 5 next > clicking on 2 goes url : page/3 clicking on 3 goes url : page/6 clicking on 4 goes url : page/9 , on. want click on 2 go page/2, 3 page/3, , on. suggestions whats going wrong?
if need more info, please let me know, thanks.
the way pagination class works returns item @ page should start.
for example, url : hostanme/controller/page/9 means page should rendered starting 9th voted article. see example in codeigniter documentation.
also, have $total_rows = sizeof($result_rows) + 10;
. why ?
Comments
Post a Comment