Persist search parameter in pagination codeigniter

1
Hello, I'm doing a search system for a store with codeigniter, the pagination is working, but when I change the page if I lose the search parameter, I would like to know how to persist the search so that pagination occurs without problems.

Follow the code.

    $maximo = 20;
    $busca = $this->input->get('produto');

    $config['base_url'] = base_url("Busca/pesquisa");
    $config['total_rows'] = $this->db->count_all_results('produtos');
    $config['per_page'] = $maximo;
    $config ['use_page_numbers'] = TRUE;
    $config["uri_segment"] = 3;

    $config['first_link'] = FALSE;
    $config['first_tag_open'] = '  ';
    $config['first_tag_close'] = '  ';

    $config['next_link'] = '>';
    $config['next_tag_open'] = '  ';
    $config['next_tag_close'] = '  ';


    $config['last_link'] = FALSE;
    $config['last_tag_open'] = '  ';
    $config['last_tag_close'] = '  ';

    $config['prev_link'] = '<';
    $config['prev_tag_open'] = '&nbsp;&nbsp;';
    $config['prev_tag_close'] = '&nbsp;&nbsp;';

    $config['cur_tag_open'] = '<b style="color:#23527C;">';
    $config['cur_tag_close'] = '</b>';

    $config['num_tag_open'] = '&nbsp;';
    $config['num_tag_close'] = '&nbsp;';


    if(!$this->uri->segment(3,0) ){//verifica se existe o terceiro seguimento na url

        $num_page=0;//inicializa a pagina para calculo correto da paginação

        $config ['use_page_numbers']=1;//seta o numero qué é vazio na primeira pagina da pesquisa

    }else{
         $num_page =$this->uri->segment(3,0)-1; //caso exista o terceiro seguimento da url nos pegamos o valor dele e 
         //tiramos 1 para que o calculo fique correto e retorne o valor correto na paginação
    }

    $divisao = $config ['use_page_numbers'];//a variavel divisao recebe o valor da url da pagina como parametro

    $calc = $num_page * $maximo; //multiplica-se o numero na pagina pelo valor maximo

    $inicio = $calc / $divisao; //o valor inicial é produto do resultado de calc divido pelo valor da pagina

    $query_busca = $this->db->query("SELECT DISTINCT * FROM produtos WHERE nome LIKE '%".$busca."%' LIMIT $inicio,$maximo ");   

    $this->pagination->initialize($config);     
    $paginas = $this->pagination->create_links();
    
asked by anonymous 28.10.2015 / 12:13

1 answer

1
Hello, here I tried to solve the problem, here follows a code that can do pagination with search and still keep the data, I thought I should use the session but I found it more viable to use url. I'm closing the topic thanks for the help, and enjoy the code.

        $por_pagina = 8;
        $inicio = ( ( isset($_GET['page']) )? ($_GET['page'] -1) :0)*$por_pagina;

        $this->db->like('nome', $busca);
        $total_produtos = $this->db->count_all_results('produtos');


        $config['base_url'] = base_url('Busca/pesquisa/');
        $config['total_rows'] = $total_produtos;
        $config['per_page'] = $por_pagina;
        $config['page_query_string'] = TRUE;
        $config['use_page_numbers'] = TRUE;
        $config['reuse_query_string'] = FALSE;
        $config['num_links'] = 10;
        $config['query_string_segment'] = 'page';
        $config['enable_query_strings'] = TRUE;
        $config['reuse_query_string'] = TRUE;
        $config['first_link'] = FALSE;
        $config['next_link'] = '>';
        $config['next_tag_open'] = '&nbsp;&nbsp;';
        $config['next_tag_close'] = '&nbsp;&nbsp;';
        $config['prev_link'] = '<';
        $config['prev_tag_open'] = '&nbsp;&nbsp';
        $config['prev_tag_close'] = '&nbsp;&nbsp';
        $config['cur_tag_open'] = '<b style="color:#23527C;">';
        $config['cur_tag_close'] = '</b>';
        $config['num_tag_open'] = '&nbsp;';
        $config['num_tag_close'] = '&nbsp;';

        $this->pagination->initialize($config);

        $paginacao = $this->pagination->create_links();

        $resultado = $this->db->query("SELECT * FROM produtos WHERE nome like '%$busca%' LIMIT $inicio, $por_pagina;" )->result();


        $data = [
            "categorias" => $query_categorias = $this->db->query("SELECT DISTINCT * FROM categorias")->result(),
            "paginas" => $paginacao,
            "resultados" => $resultado
        ];

        $data_content = array(
            'content' => $this->load->view('exibe_busca/index', $data, true)
        );

        $this->load->view('layouts/template', $data_content);

    } 
    
28.10.2015 / 16:24