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;


Messages In This Thread
Problem with Pagination - by El Forum - 10-24-2012, 03:54 PM
Problem with Pagination - by El Forum - 10-24-2012, 04:20 PM
Problem with Pagination - by El Forum - 10-25-2012, 07:21 AM
Problem with Pagination - by El Forum - 10-25-2012, 10:20 AM
Problem with Pagination - by El Forum - 10-25-2012, 10:30 AM
Problem with Pagination - by El Forum - 10-25-2012, 10:35 AM



Theme © iAndrew 2016 - Forum software by © MyBB