Paging with codeigniter

0

Can any one of you help me find the error in this paging code with codeigniter. he prints the links in the correct way, however he shows more items than he should per page, and when he changes pages he shows the same items, ie the links seem to work more than paging, does anyone help?

Follow 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);
    }
}
    
asked by anonymous 23.10.2015 / 18:57

1 answer

1

Do not give to identify the error only by looking at this part of the code in the controller, but possibly the error must be related to the value that the count_all_results method is returning, which must be different from the total of items that you want to display by adding all the results per page, or also, the error can be the offset you are using to filter the range-to-range data in the table in the database that should have some error.

Here's an example of how I implement paging in CI, so you can use it as a basis to fix your bug yourself, as I can not identify it by looking at the snippet you provided.

1 ° In order to reuse the code and get more organized using less code in the controller method, I will create a paging configuration file located in the config / folder 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 config / pagination.php file, all places where you use the library pagination values set in it are pre-defined as the default paging setting.

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

}
    
27.10.2015 / 03:12