Welcome Guest, Not a member yet? Register   Sign In
Problem with Pagination
#1

[eluser]brojask[/eluser]
Hi everybody, I'm looking for some help. I've been working in some querys and I need to display those results with pagination. My database is Postgres and I handle the querys without Active record, and the mostly examples on the web are made with Active record.

With my code is presented the first results of the query using limits, but when I press the next page with the following results it doesn't find anything, but everything is correctly coded and works. I test it in some points and the results appear but not when I try the pagination.

I think the problem is with the URI but a cannot find the answer

This is my model:

Code:
<?php

class Buscador_model extends CI_Model {

    function __construct() {
        parent::__construct();

    }

    function buscar($frase) {
        // Execute our SQL statement and return the result
        $sql = "SELECT  i.codigo_investigacion, i.titulo,m.nombre AS materia,t.nombre AS tema,cast(i.fecha as date),i.path ,i.palabras_claves,i.resumen
                FROM scc_investigaciones i
                INNER JOIN scc_materias m ON m.codigo_materia = i.codigo_materia
                INNER JOIN scc_temas t ON t.codigo_tema = i.codigo_tema
                WHERE (translate(lower(i.palabras_claves), 'áéíóúñÑ', 'aeiounn') ILIKE '%" . $frase . "%' OR translate(lower(i.titulo), 'áéíóúñÑ', 'aeiounn') ILIKE '%" . $frase . "%') AND (i.estado <>3)
                ORDER BY i.fecha DESC";          
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            return $query;
            //return $query->result();
        } else {
            return FALSE;
        }
    }
    function buscar_paginado($frase,$maximo,$inicio) {
        $this->db->limit($maximo, $inicio);
        // Execute our SQL statement and return the result
        $sql = "SELECT  i.codigo_investigacion, i.titulo,m.nombre AS materia,t.nombre AS tema,cast(i.fecha as date),i.path ,i.palabras_claves,i.resumen
                FROM scc_investigaciones i
                INNER JOIN scc_materias m ON m.codigo_materia = i.codigo_materia
                INNER JOIN scc_temas t ON t.codigo_tema = i.codigo_tema
                WHERE (translate(lower(i.palabras_claves), 'áéíóúñÑ', 'aeiounn') ILIKE '%" . $frase . "%' OR translate(lower(i.titulo), 'áéíóúñÑ', 'aeiounn') ILIKE '%" . $frase . "%') AND (i.estado <>3)
                ORDER BY i.fecha DESC LIMIT " . $maximo . " OFFSET " . $inicio . "";          
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            //return $query;
            return $query->result();
        } else {
            return FALSE;
        }
    }
}

This is my controller:

Code:
function buscar_2() {
    //$this->load->library('pagination');
        if(!empty($_POST['frase'])){
        $frase = $_POST['frase'];        
        $valido = $this->buscador_model->buscar($frase);
        if (!$valido) {
          $this->error_buscar_2();
        }
        else {        
        $inicio = $this->uri->segment(3);
        $per_page = 2;
        $total_rows = $this->buscador_model->buscar($frase)->num_rows();
        if(empty($inicio)){
        $inicio = 0;
            }      

        $config['base_url'] = base_url().'index.php/buscador/buscar_2/';
        $config['per_page'] = $per_page;
        $config['prev_link'] = 'anterior';
        $config['next_link'] = 'siguiente';
        $config['first_link'] = '<<';
        $config['last_link'] = '>>';
        $config['total_rows'] = $total_rows;      
        $this->pagination->initialize($config);            

            $data['main_content'] = 'buscador_menu/buscar';
            $data['investigaciones'] =  $this->buscador_model->buscar_paginado($frase, $per_page, $inicio);
            $data['link'] = $this->pagination->create_links();            
            $this->load->view('includes/template', $data);
        }
        }
        else {
            return $this->error_buscar();
        }
    }
}

This is my View:

Code:
&lt;?=$this->db->last_query();?&gt;
<pre>&lt;?=print_r($investigaciones);?&gt;</pre>

    &lt;?php foreach ($investigaciones as $investigacion): ?&gt;
        <table class="table table-bordered">
            <thead>
                <tr >
                    <th colspan="6">
                        &lt;?=$investigacion->codigo_investigacion." - ".$investigacion->titulo ?&gt;
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <th>Codigo:</th><th>Rama:</th><th>Descriptor:</th><th>Fecha de creación:</th>
                    <th>Palabras clave:</th><th>Descargar:</th>
                </tr>
                <tr>
                    <td>&lt;?=$investigacion->codigo_investigacion ?&gt;</td>
                    <td>&lt;?=$investigacion->materia ?&gt;</td>                    
                    <td>&lt;?=$investigacion->tema?&gt;</td>
                    <td>&lt;?=$investigacion->fecha ?&gt;</td>
                    <td>&lt;?=$investigacion->palabras_claves ?&gt;</td>
                    <td><a class="btn" target="_blank" href="&lt;?='localhost/'.$investigacion-&gt;path?&gt;"><i class="icon-download"></i></a></td>
                </tr>                                  
            </tbody>
        </table>
<hr />
    &lt;?php endforeach; ?&gt;    
&lt;?php if (isset($link)): ?&gt;
<center> &lt;?php echo $link ?&gt; </center>
&lt;?php endif ?&gt;
#2

[eluser]Aken[/eluser]
I don't understand what exactly is wrong.
#3

[eluser]brojask[/eluser]
Hi Aken, the problem is when the pagination is displayed: the fist results appear correctly but I click the next page to show the others results but it doesn't generate them, I think the problem is when I try to take the 3 segment.
I took this video as reference:
http://www.youtube.com/watch?v=IsMK-PyS-...ure=relmfu
#4

[eluser]CroNiX[/eluser]
I don't see you sending any offset to your query to get the additional pages, like if you are viewing 100 items on page 1, page 2 needs to get the offset so it can get records 101-200. You're getting the same data over and over regardless of what page you are on.
#5

[eluser]brojask[/eluser]
Hello CroNiX, I'm sending the offset using this:

In the controller:
Code:
$inicio = $this->uri->segment(3);

When I call the model to get the results:
Code:
$data['investigaciones'] =  $this->buscador_model->buscar_paginado($frase, $per_page, $inicio);

And in my model's query:

Code:
function buscar_paginado($frase,$maximo,$inicio) {
$this->db->limit($maximo, $inicio);
#6

[eluser]brojask[/eluser]
Sorry, I think I didn't explain something important: I'm trying to do a search and display results with pagination. In the mostly web's examples, the pagination is made without a searching term




Theme © iAndrew 2016 - Forum software by © MyBB