Welcome Guest, Not a member yet? Register   Sign In
pagination?
#1

[eluser]MMCCQQ[/eluser]
hi

i using pagination library..and don't work;
Controller
Code:
<?php

class Seccion extends Controller{
    
    function Seccion(){
        
            parent::Controller();


    }
    
    function Noticias(){
        
        $this->load->library('pagination');



        $this->db->where('cat','Noticias');
    
     $query = $this->db->get('general');
    
     if($query->num_rows() > 0){
        
        $noticias['noticias'] = $query;
        
    }
    
        $main['contenido'] = $this->load->view('seccion/noticias',$noticias,true);
        
        $data['modules_left'] = $this->load->view('frontend/contenido', $main, true);
        
        $data['modules_right'] = FALSE;
        
        
$config['base_url'] = 'http://importzona.com/Z-move/seccion/noticias/';
$config['total_rows'] = '5';
$config['per_page'] = '20';

$this->pagination->initialize($config);
         $this->load->view('templatev2', $data);
    }
    
    
    
    
    
}


?>
View (Example)
Code:
<?php foreach($noticias->result() as $row):?>

<?=$row->titulo;?>


<?php endforeach;?>
<? echo $this->pagination->create_links();?>
#2

[eluser]Isern Palaus[/eluser]
Hey MMCCQQ,

I didn't know how to use the pagination class and I talked to Zaatar, the coder of CodeExtinguisher (check this post: http://ellislab.com/forums/viewthread/67126/). I based my code on the module that he uses and the result was that:

Code:
<?php
class Cmm_m extends Model
{
    
    function __construct ()
    {
        parent::__construct();
    }
    
    function  getPaginated($table,$num=-1,$offset=-1)
    {
        $sql = "SELECT * from ".$table." order by id desc";
        if($num != -1)
            $sql .= " limit ";
        if($num != -1 AND $offset != -1)
            $sql .= "$offset,";
        if($num != -1)
            $sql .= "$num";
        
        $query = $this->db->query($sql);
        return $query->result_array();
    }
}
?>

The Controller that uses the cmm_m model:
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class News extends Controller
{

    function News()
    {
        parent::Controller();
        
        $this->load->helper('user');
        $this->load->helper('html');
        $this->load->helper('typography');
        
        $this->load->model('cmm_m');
        
        $data['logged'] = logged_check($this->session->userdata('logged_in'));
    }
    
    function index() {
        redirect('news/show');
    }

    function show()
    {            
        $this->load->library('pagination');
        $this->load->helper('date');

        $config['base_url']   = base_url().'index.php/news/show';
        $config['total_rows'] = $this->db->count_all('posts');
        $config['per_page']   = '10';

        $this->pagination->initialize($config);
        
        $num = $this->uri->segment(3);
        
        if($num === "all"){
            $data['posts'] = $this->cmm_m->getPaginated('posts',$config['per_page']);
        }
        else{
            if(!isset($num) OR !is_numeric($num)) $num = 0;
            $data['posts'] = $this->cmm_m->getPaginated('posts',$config['per_page'],$num);
        }

        $this->load->view('news/view',$data);
    }
}
?>

And the view:
Code:
<?php
$this->load->view("base/header");
?>
    <div id="section">
        <h1>Not&iacute;cies</h1>
        
        <div id="pagination">&lt;?php echo $this->pagination->create_links();?&gt;</div>
        
        &lt;?php    
        foreach ($posts as $id => $post) {                        
            echo '<div class="content"><b>'.anchor('news/id/'.$post['id'],$post['title']).'</b>'.nbs(1).'('.mdate("%d-%m-%Y",$post['created_on']).')';
            echo nbs(2).auto_typography($post['body']).'</div>';
        }
        ?&gt;
        
        <div id="pagination">&lt;?php echo $this->pagination->create_links();?&gt;</div>
    </div>
&lt;?php
$this->load->view("base/footer");
?&gt;

I wish that help you.

Un saludo,
-- Isern Palaus
#3

[eluser]MMCCQQ[/eluser]
how can i get url show/1 not show/10

http://importzona.com/Z-move/seccion/noticias/
#4

[eluser]Jauhari[/eluser]
Just Perfect, but I have little question.

How to show all records? the URL and the anchor links?
#5

[eluser]Saymont[/eluser]
Hi,

I'm lost with this pagination class too.
I tried to see how bambooinvoice.org did but I could not understand.

Anyone can help us? Tongue
#6

[eluser]Frank Rocco[/eluser]
Here is what I have working.

function index()
{
// Apply where clause
$this->db->where('userGroup','HELPDESK');

$config['base_url'] = base_url().'/index.php/welcome/index';
$config['total_rows'] = $this->db->count_all_results('logins');
$config['per_page'] = 3;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);

$offset = (int) $this->uri->segment(3, 0);
// Order by user name, asc
$this->db->orderby('userName','asc');
$data["query"] = $this->db->get('logins', $config['per_page'], $offset);

$data['pagination1'] = $this->pagination->create_links();

$this->load->view('index',$data);
}

View
&lt;?php
foreach ($query->result() as $row)
{
echo $row->userName . '<br/>';
}
?&gt;
<br/>
&lt;?php echo $pagination1 ?&gt;

HTH

Frank
#7

[eluser]Saymont[/eluser]
Thank you.

How does the pagination class send the information that the $query must list just the $config[’per_page’]?
#8

[eluser]Frank Rocco[/eluser]
The offset tells the query where to start returning records.
The per_page tells the query how many records to return.
so if the per_page is 5 and yu ar on the first page.
When you go to the next page, the offset will be 6 and return 5 more records.

This works for mysql, but not for sqlserver 2000.
I am trying to find a work around.

HTH

Frank
#9

[eluser]Saymont[/eluser]
Thank you Frank,

But where on the select query the "per_page" is loaded ? I cant find anything about it:

take a look:

Controller:

Code:
function index()
{
        
$config['base_url'] = site_url('tickets/index');
//the total rows is returning the correct value. already checked
$config['total_rows'] = $this->Ticket_model->tickets_abertosTotais();
$config['per_page'] = 2;
$config['uri_segment'] = 3;

$this->pagination->initialize($config);
        
$offset = (int) $this->uri->segment(3, 0);
$data['tickets_abertosTotaisResultado'] = $this->Ticket_model->tickets_abertosTotaisResultado($offset,$config['per_page']);

$data['paginacao'] = $this->pagination->create_links();
    
$this->load->view('/ticket/ticket_index_view',$this->_comum($data));
        
}

Model

Code:
function tickets_abertosTotaisResultado()
{
        $query = $this->db->query ("
        SELECT XXXX FROM XXXX WHERE XXXXX
        ");
        return $query->result();
    
}

View (simple):

Code:
&lt;?  foreach($tickets_abertosTotaisResultado as $row): ?&gt;

  &lt;?= $row->ticket_id ?&gt;   </br>

&lt;? endforeach; ?&gt;
&lt;?=$paginacao?&gt;


Can anyone help me?
Thank you...
#10

[eluser]Frank Rocco[/eluser]
You need to add
LIMIT xx OFFSET xx to your query in your model.

HTH

Frank




Theme © iAndrew 2016 - Forum software by © MyBB