Getting the same data on pagination in Codeigniter

2019-06-14 12:12发布

问题:

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';

回答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