Persist search parameter in codeigniter paging

Asked

Viewed 763 times

1

Hello, I’m making a search system for a store with codeigniter, paging is working, but when you change the page you lose the search parameter, I would like to know how to persist the search so that the paging occurs without problems.

Follows 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();

1 answer

1

Hello, trying here I managed to solve the problem, here follows a code that can make a pagination with search and still keep the data, I thought I should use the session but I found more feasible to ultilizar 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);

    } 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.