Paging with the codeigniter

Asked

Viewed 1,385 times

0

Could one of you help me find the error in this paging code with codeigniter. it prints the links in the right way, but it shows more items than it should per page, and when I change the page it shows the same items, ie the links seem to work more the paging no, someone helps?

Follows the code

$this->load->library('pagination');

    $maximo = 10;

    $config['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['next_link'] = 'Próximo';
    $config['next_tag_open'] = '  ';
    $config['next_tag_close'] = '  ';


    $config['last_link'] = FALSE;


    $config['prev_link'] = 'Anterior';
    $config['prev_tag_open'] = '  ';
    $config['prev_tag_close'] = '  ';

    $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);


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

    $data=[            
        "categorias" => $query_categorias,
        "paginas"  =>$paginas,
          ];

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

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

1 answer

1

Can’t identify the error just by looking at this part of the code in the controller, but possibly the error must be related to the value that the method count_all_results is returning, which should be different from the total of items you want to display, summing all the results per page, or also, the error may be the offset you are using to filter the data from interval to interval in the table in the database that must have some error.

Here I’ll show you an example of how I implement paging in the IC, so you can use it as a basis to fix your error yourself, since I can’t identify it-Ló by looking at the code snippet you provided.

1° In order to reuse the code and be more organized using less code in the controller method, I will create a pagination configuration file located in the folder config/, whose default name is pagination.php with the following content:

<?php

$config['num_links'] = 10;

$config['per_page'] = 20;

$config['full_tag_open'] = '<ul class="pagination float-right border-rounded-5">';
$config['full_tag_close'] = '</ul>';

$config['first_link'] = 'Primeiro';
$config['first_tag_open'] = '<li class="first-pagination">';
$config['first_tag_close'] = '</li>';

$config['last_link'] = '&Uacute;ltimo';
$config['last_tag_open'] = '<li class="last-pagination">';
$config['last_tag_close'] = '</li>';

// $config['next_link'] = '&gt;';
$config['next_tag_open'] = '<li class="next-pagination">';
$config['next_tag_close'] = '</li>';

// $config['prev_link'] = '&lt;';
$config['prev_tag_open'] = '<li class="prev-pagination">';
$config['prev_tag_close'] = '</li>';

$config['cur_tag_open'] = '<li class="current-pagination">';
$config['cur_tag_close'] = '</li>';

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

Note: using this file config/pagination.php, all places where you use the pagination library the values defined in it have been pre-set as default for the pagination configuration.

2° In the code below I will show how I implement the controller method of the page where the page will be displayed:

public function listar()
{

    //$limit Equivalente a sua variável $maximo
    $limit = 10;
    //$offset 3° segmento da URL que serve como base da paginação intervalada dos registros do banco de dados
    $offset = $this->uri->segment(3, 0);

    //Recupera os dados no banco de dados de acordo com o limite difinido de dados a serem retornados e começando partir do registro $offset
    $lista = $this->produto_model->retrieve($limit, $offset);

    // Carregando a biblioteca pagination
    $this->load->library('pagination');

    //Configurando a url base da paginação
    $configPagination['base_url'] = base_url() . 'usuario/listar';
    //Total de registros para que se possa gerar os links da paginação
    $configPagination['total_rows'] = $this->usuario_model->count();

    //Setar as configurações
    $this->pagination->initialize($configPagination);

    //Gerar paginação
    $pagination = $this->pagination->create_links();

    $dados = array(
        'data_list' => $lista,
        'pagination' => $pagination,
    );

    $this->load->view('produtos/listar', $dados);

}
  • Thank you Yure Pereira, it will certainly help me a lot to understand, thank you very much.

Browser other questions tagged

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