code igniter pagination with sort options - sorting

I'm not sure if what I'm about to ask is possible or not.. I'm thinking probably not but I thought I would ask anyways.
Is there any way to reset the pagination?
I've got a page with has pagination and sort option on it. Everything works fine but when one of the sort options is changed I would like to send the user back to the first page.
(I'm pretty sure the way I'm doing this isn't at all the best way to get the result I want so if you have any suggestions for improvements please feel free.)
$array = explode ("_",$this->uri->segment(4));
if(count($array) > 1){
$search_id = $array[1];
$start = $this->uri->segment(5);
$uri_segment = 5;
}else{
$start = $this->uri->segment(4);
$uri_segment = 4;
$search_id = null;
}
$config['per_page'] = $this->settings['PRODUCTS_PER_PAGE'];
$config['total_rows'] = $this->categories_model->get_num_rows($cat_id , $search_type, $search_data);
$query = $this->categories_model->get_category_pages($cat_id, $new_limit, $new_sort, $search_id, $start, $this->settings['CATEGORY_ORDER'], $search_type, $search_data, $config['total_rows']))
$config['base_url'] = base_url() . 'index.php/categories/view/' . $cat_id ."/" . $search_string ;
$config['uri_segment'] = $uri_segment;
//Congig settings for pagination
$config['full_tag_open'] = '<div id="pagination" style= "clear:both;">';
$config['full_tag_close'] = '</div>';
//Initialise pagination
$this->pagination->initialize($config);

you may keep a default sorting in controller which will not show in url.so sorting will not back to the first page.
a sample
class Post extends Jewelery_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Prime_model');
}
public function index($ordr_by = 'post_title', $sortr = 'ASC') {
if ($this->uri->segment(6)) {
$data['segment'] = $this->uri->segment(6);
} else {
$data['segment'] = 0;
}
if ($this->uri->segment(4)) {
$ordr_by = $this->uri->segment(4);
}
if ($this->uri->segment(5)) {
$sortr = $this->uri->segment(5);
}
$data['title'] = 'All Posts';
$this->load->library('pagination');
$config['base_url'] = base_url() . 'admin/post/index/' . $ordr_by . '/' . $sortr;
$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div>';
$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['uri_segment'] = 6;
$config['total_rows'] = $this->db->get('jewel_posts')->num_rows();
$config['per_page'] = 10;
$this->pagination->initialize($config);
$data['post_info'] = $this->Prime_model->master_join('jewel_posts', 'jewel_jewelry_type', 'jewel_posts.jewelry_type = jewel_jewelry_type.jewelry_type_id', 'left', FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, $ordr_by, $sortr, $config['per_page'], $data['segment']);
$this->load->view('admin/header', $data);
$this->load->view('admin/post/index');
$this->load->view('admin/footer');
}

Related

Getting the same data on pagination in Codeigniter

I am getting the same data when I am clicking on the next button of pagination in CodeIgniter with the following code
public function view($slug){
$data['title']= $slug;
$data['description']= "None";
$postdatacount = $this->Constant_model->snippettagscount($slug);
$checktags= $this->Constant_model->gettags($slug);
if($checktags>0){
if ($postdatacount>0) {
$config = array();
$config["base_url"] = base_url() ."tags/".$slug;
$config["total_rows"] = $postdatacount;
$config["per_page"] = 6;
$config["uri_segment"] = 2;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '&laquo';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '&raquo';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data["links"] = $this->pagination->create_links();
$data["title"] = "All Tags";
$data["description"] = "All Tags";
$data['snippets_tags'] = $this->Constant_model->get_tags($config["per_page"],$page,$slug);
$this->snippetfunctions->add_count('tags','tag_name',$slug);
$this->load->view('view_tag_snippets', $data);
}else {
$data["title"] = "No Snippet Found for this Tag";
$data["description"] = "No Snippet Found for this Tag";
$data["slug"] =$slug;
$this->load->view('error_tags',$data);
}
}else{
$this->load->view('404',$data);
}
}
Primary URL Made on this function is
http://127.0.0.1/Mytredin_codesup/tags/user-interface
When I am clicking on Next button following URL is made but not loading the next data but loading the same data and the same thing happens on every next page.
http://127.0.0.1/Mytredin_codesup/tags/user-interface/1
Routes I am using is
$route['tags/(:any)/(:num)'] = 'tags/view/$1/$2';
$route['tags/(:any)'] = 'tags/view/$1';
The problem is with the config $config["uri_segment"] = 2;, according to your routing the page variable is at segment 3.
Use $config["uri_segment"] = 3; instead of $config["uri_segment"] = 2;. Also change the line $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
More details read

Why do i get the same values in all pages in CI pagination?

I'm kinda new in CI and i'm trying to create a proper pagination for my page.
I have the following model and controller and even if i manage to get the pagination links, when i visit the other pages i get the same entries again and again.
I also have applied an Ajax "GET" filtering for fetching results from db and i managed to reuse the url parameters when i change pages.
The problem is that i don't know if i should try to create an infinite scroll function or maintain the default CI pagination.
I'd like to hear your opinions too!
Thank you in advance!
public function get_filtered_pets($limit, $start){
$this->db->order_by('pet_created_at', 'DESC');
$this->db->join('users', 'users.user_id = pets.pet_user_id');
$this->db->join('pet_categories', 'pet_categories.pet_category_id = pets.category_id');
$pet_data = array();
if ( $this->input->get('petStatus') ) {
$pet_data['pet_status'] = $this->input->get('petStatus');
}
if ( $this->input->get('petCategory') ) {
$pet_data['category_id'] = $this->input->get('petCategory');
}
if ( $this->input->get('petGender') ) {
$pet_data['pet_gender'] = $this->input->get('petGender');
}
if ( !empty($pet_data) ) {
$this->db->where($pet_data);
}
$query = $this->db->get('pets');
if ($start > 0) {
$this->db->last_query();
}
return $query->result_array();
}
And my controller
public function index(){
$query = $this->db->get('pets');
$pet_num = $query->num_rows();
$config = array();
$config["base_url"] = base_url() . "pets/index/";
$config["total_rows"] = $pet_num;
$config["per_page"] = 12;
$config['use_page_numbers'] = TRUE;
$config['reuse_query_string'] = TRUE;
$config['attributes'] = array('class' => 'page-link');
$config["full_tag_open"] = '<ul class="pagination">';
$config["full_tag_close"] = '</ul>';
$config["first_link"] = "«";
$config["first_tag_open"] = "<li>";
$config["first_tag_close"] = "</li>";
$config["last_link"] = "»";
$config["last_tag_open"] = "<li>";
$config["last_tag_close"] = "</li>";
$config['next_link'] = '>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '<li>';
$config['prev_link'] = '<';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tag_close'] = '<li>';
$config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['pets'] = $this->pet_model->get_filtered_pets($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
//View files
$this->load->view('templates/header', $data);
$this->load->view('pets/index', $data);
$this->load->view('templates/footer', $data);
}
You are not using the value of $limit and $start nowhere. See Limiting or Counting Results.
You should:
public function get_filtered_pets($limit, $start){
$this->db->limit($limit, $start);
/* ... */
return $query->result_array();
}
And also, when calling this function you have to multiply: per_page * page, to get the offset (starting index).
$this->pet_model->get_filtered_pets($config["per_page"], $page * $config["per_page"])
To count results correctly, I normally have two functions on model: "get($search, $offset)" and "count($search)".
The function count has exactly the same code on get, except the select fields.
See this code, I think you will get it: https://pastebin.com/dZG90Lzm.
ps: There's ways to optimize it and avoid duplicate code.

pagination doesn't work in codeigniter

I made an optional search that makes the user search with username or customer or date from/to the result returning well to the first page but if I click on another page the result return nothing
this my front and all that input optional
i think the problem the pagination path because if i fill all inputs the pagination path become
controllername/userid/customerid/fromdate/todate
and if skip any input I find the pagination path is missing that
this my controller
public function search($page_num = 1){
$this->load->library('pagination');
$this->load->helper("url");
$this->load->model(array('CustomReport_m','user','Customer_m')); // This array to save number of lines only
$username=$this->input->post('select_user');
$username = ($this->uri->segment(3)) ? $this->uri->segment(3) : $username;
$customer=$this->input->post('select_customer');
$customer = ($this->uri->segment(4)) ? $this->uri->segment(4) : $customer;
$datefrom=$this->input->post('from');
$datefrom = ($this->uri->segment(5)) ? $this->uri->segment(5) : $datefrom;
$dateend=$this->input->post('to');
$dateend = ($this->uri->segment(6)) ? $this->uri->segment(6) : $dateend;
$total_row = $this->CustomReport_m->search_count($username,$customer,$datefrom,$dateend);
//$config['use_page_numbers'] = TRUE;
$config['base_url'] = site_url("CustomReport/search/$username/$customer/$datefrom/$dateend");
$config['total_rows'] = $total_row;
$config['per_page'] = 10;
$choice = $config["total_rows"]/$config["per_page"];
$config["num_links"] = floor($choice);
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '&laquo';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '&raquo';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
if ($this->uri->segment(7)) {
$page = ($this->uri->segment(7));
} else {
$page = 1;
}
$str_links = $this->pagination->create_links();
$data["links"]= explode(' ', $str_links);
$data['users']=$this->user->display_users();
$data['userdata'] = $this->user->userdata();
$data['customers']= $this->Customer_m->index();
// if(isset($_POST['submit'])){
$data["rsl"] =$this->CustomReport_m->search($username,$customer,$datefrom,$dateend,$config["per_page"],$page);
$this->load->view('customreport_v',$data);
// }else{
// redirect('CustomReport','refresh');
// }
}
This is my model
public function search_count($username,$customer,$datefrom,$dateend) {
$this->db->select('*');
$this->db->from('transactions');
if (!empty($customer)){
$this->db->where('t_customer_id',$customer);
}
if (!empty($username)){
$this->db->where('t_u_id',$username);
}
if (!empty($dateend)){
$this->db->where('t_date <=',$dateend);
}
if (!empty($datefrom)){
$this->db->where('t_date >=',$datefrom);
}
return $this->db->get()->num_rows();
}
public function search($username,$customer,$datefrom,$dateend,$limit,$start)
{
$start=$start-1;
$this->db->select('*');
$this->db->from('transactions');
if (!empty($customer)){
$this->db->where('t_customer_id',$customer);
}
if (!empty($username)){
$this->db->where('t_u_id',$username);
}
if (!empty($dateend)){
$this->db->where('t_date <=',$dateend);
}
if (!empty($datefrom)){
$this->db->where('t_date >=',$datefrom);
}
$this->db->limit($limit,$start);
$query=$this->db->get();
if($query->num_rows() > 0){
return $query->result();
}else{
return $query->result();
}
}
this my route
$route['CustomReport/(:num)']="CustomReport/index/$1";

codeigniter pagination links doesnt work properly

I am using Codeigniter library pagination for my search.The problem is even if i have 2 results from the database table,clicking the next or previous link shows only one result.
code :
controller
function search()
{
$this->load->library('pagination');
$perpage =1;
$page = 1;
$search_content = $this->input->get('con_search');
$data->search = $this->public_model->search_content($search_content, $perpage);
$total = count($this->public_model->search_content($search_content));
$config['base_url'] = current_url()."?con_search=".$search_content;
$config['total_rows'] = $total;
$config['per_page'] = 1;
$config['cur_page'] = $page;
$config['use_page_numbers'] = TRUE;
$config['query_string_segment'] = "page";
$config['display_pages'] =TRUE;
$config['page_query_string'] = TRUE;
$config['cur_tag_open'] = '<span class="current">';
$config['cur_tag_close'] = '</span>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$config['prev_tag_open'] = '<span class="prevtag">';
$config['prev_tag_close'] = '</span>';
$config['next_tag_open'] = '<span class="nexttag">';
$config['next_tag_close'] = '</span>';
$this->pagination->initialize($config);
$data->footer = $this->public_model->get_footer();
$this->load->helper('text');
$this->load->view('header', $data);
$this->load->view('search');
$this->load->view('footer');
}
model:
function search_content($search_txt, $perpage = 0)
{
if($perpage){
$this->db->limit($perpage);
}
$this->db->select('title, description, alias');
$this->db->where('status', 'yes');
$this->db->like('description', $search_txt);
$res = $this->db->get('tbl_content')->result_array();
return $res;
}
view :
if($search && is_array($search)) {
foreach($search as $ind=>$val)
{
$this->db->select('menu_type_id');
$this->db->where('menu_alias', $val['alias']);
$res = $this->db->get('tbl_menu')->row_array();
if($res) {
if($res['menu_type_id'] == 2)
$url = site_url('page/'.$val['alias']);
else
$url = site_url('page/footer/'.$val['alias']);
}
?>
<div class="box_1">
<div class="deal_contents">
<div class="bx_0"><?php echo $val['title'] ?></div>
<div class="bx_1" style="width:670px;"><?php echo strip_tags(character_limiter($val['description'],200)) ?></div>
<div class="bx_2" style="text-align:right; width:640px;">Read More</div>
</div>
</div>
<?php
}
echo $this->pagination->create_links();
}
thanks in advance
Do like this
$this->load->library('pagination');
$limit = 10;
$total = $this->legend_model->get_legend_count($language_id);
$config['base_url'] = base_url().'legend/index/';
$config['total_rows'] = $total;
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['first_link'] = '<< First';
$config['last_link'] = 'Last >>';
$config['next_link'] = 'Next ' . '>';
$config['prev_link'] = '<' . ' Previous';
$config['num_tag_open'] = '<span class="number">';
$config['num_tag_close'] = '</span>';
$config['cur_tag_open'] = '<span class="current"><a href="#">';
$config['cur_tag_close'] = '</a></span>';
$this->pagination->initialize($config);
$data['offset'] = $offset;
$data['legends'] = $this->legend_model->get_legend($language_id, $limit, $offset);
$this->template->write('title', 'Legend : Manage Legend');
$this->template->write_view('content', 'legend/index', $data);
$this->template->render();

Codeigniter Pagination Need Solution - number link work but page doesnot display as appropriate

I have a problem with pagination in CodeIgniter 2.x
This is my code in Controller:
$config['base_url'] = base_url().'crawl/all/'.$file.'/';
$config['total_rows'] = $total;
$config['per_page'] = 10;
$config['num_links'] = 3;
$config['uri_segment'] = 4;
$config['full_tag_open'] = '<ul id="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_tag_open'] = '<li class="next">';
$config['next_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active">';
$config['cur_tag_close'] = '</li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$data['total'] = $total;
$data['path'] = $this->path.'/'.$file;
$data['file'] = $file;
$data['batch'] = $this->batch->all($file, $config['per_page'], $config['uri_segment']);
and my view is just: <?php echo $this->pagination->create_links() ?>
The links for pages numbers work (they move from 1 to 2 etc), but the data does not display correctly.
You are missed out the page id because when you clicks on the page the page no has to send as query string and it has to add for the config.
Example code:
$limit = $this->config->item('paging_limit');
$offset = $id * $limit;
$data['rows'] = $this->news_model->get_moreNews($limit, $offset);
$config["image_url"]=$this->config->item("base_url");
$config['base_url'] = base_url().'/news/morenews/';
$config['total_rows'] = $this->db->count_all('news');
$config['per_page'] = $limit;
$config['chapter'] = $this->input->post('chapter');
$config['page'] = $id;
$this->paginationsimple->initialize($config);
$paginator=$this->paginationsimple->create_links();
$data['paginator'] = $paginator;

Resources