TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
KO2.x: Pagination with header sorting
  • Hello, i tried to search the forum but i did not find anything; any1 knows if there is a tutorial or an example on how to use kohana pagination and make the header labels sorting the column?

    thanks
  • Here is a template for you, I must say that pagination has nothing to do with header sorting.<?php

    class Controller_Welcome extends Controller {

    public function action_index()
    {
    $page = new Pagination;

    $total_rows = TRUE;

    $data = Model::factroy('welcome')
    ->lists($_GET, $page->offset, $page->items_per_page, $total_rows);

    $page->total_items = $total_rows;

    $this->response = View::factory('welcome', array(
    'pagination' => $page,
    'data' => $data
    ));
    }
    }

    class Model_Welcome extends Model {

    public function lists(array $params, $page_from = 0, $page_offset = 8, & $total_rows = FALSE)
    {
    $sql = 'FROM `table` ';

    // Customize where from params
    //$sql .= 'WHERE ... '

    // caculte the total rows
    if($total_rows === TRUE)
    {
    $total_rows = $this->_db->query(Database::SELECT,
    'SELECT COUNT(`issue_id`) num_rows '.$sql
    )->get('num_rows');

    if($total_rows == 0)
    return array();
    }

    // Customize order by from params
    if( ! empty($params['order']))
    {
    $sql .= 'ORDER BY '.$params['orderby'];
    if(empty($params['by']) $sql .= ' ASC'
    else $sql .= strtolower($params['by']) === 'desc' ? ' DESC' : ' ASC';
    }

    $sql .= " LIMIT $page_from, $page_offset";

    return $this->_db->query(Database::SELECT, 'SELECT * '.$sql);
    }

    }


    welcome.php
    <table><caption>demo</caption><thead><tr>
    <th>No.</th><th><a href="<?php
    $_GET['by'] = (empty($_GET['by']) OR $_GET['by'] === 'desc') ? 'asc' : 'desc';
    $_GET['order'] = 'foo';
    echo $page->url($page->current_page);
    ?>" title="">Foo</th><th><a href="<?php
    $_GET['order'] = 'bar';
    echo $page->url($page->current_page);
    ?>" title="">bar</th>
    </tr></thead><tbody>
    <tr><th></th><td></td></tr>
    </tbody></table>
  • Thank you, but this seems to me your version of pagination. Kohana 2.x has already pagination included, i wonder if there is a tutorial using kohana pagination lib. Anyway thanks i will try to adapt your solution to kohana 2.x pagination.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion