skip to Main Content

I am using Codeigniter pagination library with first time. Now when user search anything from search form, I want to pass URL variable to pagination at the end of URL like this http://localhost/sitename/brand/campaigns/6/?search="hello" for maintain search results on second page.

Any idea how to do with codeigniter?

Here is my code:

Form

<form name="frmSearch" id="frmSearch" method="get" action="<?php echo base_url(); ?>brand/campaigns">
    <input type="text" name="search" id="search" placeholder="Search campaigns" onchange="return trim(this)">
    <button type="submit" class="search-submit"></button>
</form>

Controller (campaigns_list.php)

public function __construct(){
    parent::__construct();
    $this->load->model('Campaigns_list_model');
    $this->load->library('pagination');
}

public function index($offset=0){
    IsNotLoggedIn(); // Check if a user is not logged in

    // For SEO
    $data['meta_title'] = 'Search';
    $data['meta_descripton'] = '';
    $data['meta_keywords'] = '';

    // Pagination code for campaigns code start
    $config['total_rows'] = $this->Campaigns_list_model->TotalCampaigns();

    // Check status value is number
    $status = $this->uri->segment(3);
    if(!ctype_digit($status)){
        $status = "1";
    }

    $config['base_url'] = base_url()."brand/campaigns/".$status;
    $config['per_page'] = 6;
    $config['uri_segment'] = '4';

    $config['full_tag_open'] = '<div class=""><ul class="pagination pull-right">';
    $config['full_tag_close'] = '</ul></div>';

    $config['first_link'] = '« First';
    $config['first_tag_open'] = '<li class="prev page">';
    $config['first_tag_close'] = '</li>';

    $config['last_link'] = 'Last »';
    $config['last_tag_open'] = '<li class="next page">';
    $config['last_tag_close'] = '</li>';

    $config['next_link'] = 'Next →';
    $config['next_tag_open'] = '<li class="next page">';
    $config['next_tag_close'] = '</li>';

    $config['prev_link'] = '← Previous';
    $config['prev_tag_open'] = '<li class="prev page">';
    $config['prev_tag_close'] = '</li>';

    $config['cur_tag_open'] = '<li class="active"><a href="">';
    $config['cur_tag_close'] = '</a></li>';

    $config['num_tag_open'] = '<li class="page">';
    $config['num_tag_close'] = '</li>';

    $this->pagination->initialize($config);
    $query = $this->Campaigns_list_model->GetCampaigns(6, $this->uri->segment(4));

    $data['Campaigns'] = null;

    if($query){
        $data['Campaigns'] =  $query;
    }
    // Pagination code for campaigns code end

    $this->load->view('include/brand_header', $data);
    $this->load->view('brand/campaigns_list', $data);
    $this->load->view('include/brand_footer');
}

2

Answers


  1. Is it required for you to have the search tag in the url? I mean, you can save the search tag on a session var and maybe work with that. That’s what I usually do depending the situation. This should be, providing that you have a way for the user to reset his search terms.

    If you do require to use it like so, you can do something like that:

    if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET, '', "&");

    Taken from the following question (you should check this as well):
    codeigniter pagination url with get parameters

    Login or Signup to reply.
  2. <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class My_controller extends CI_Controller {
    
        public function __construct() {
    
                parent::__construct();
                //here we will autoload the pagination library
                $this->load->library('pagination');
            }
        public function paginationExample()
        {
            //echo $this->uri->segment(2);die;
    
            $config = array();
            $config["base_url"] = base_url('My_controller/paginationExample');
            $config['total_rows'] =   $this->db->count_all("Your table");//here we will count all the data from the table
            $config['per_page'] = 5;//number of data to be shown on single page
            $config["uri_segment"] = 2;
            $this->pagination->initialize($config);
            $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
            $data["allData"] = $this->My_model->paginationExample($config["per_page"], $page);
            $data["links"] = $this->pagination->create_links();//create the link for pagination
            $this->load->view('your view',$data);
        }
    }
    

    For more how to do pagination in codeigniter try this: http://w3code.in/2015/10/how-to-do-pagination-in-codeigniter/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search