RSS

Joomla pagination code

02 Apr

1) Model file of view

<?php

defined(‘_JEXEC’) or die(‘Restricted access’);

jimport(‘joomla.application.component.model’);

class ComponentnameModelViewname extends JModel
{
var $_data = ”;
var $_pagination    = ”;
var $total            = ”;

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

global $mainframe, $option;
// Get pagination request variables
$limit = JRequest::getVar(‘limit’, 10);
$limitstart = JRequest::getVar(‘limitstart’, 0, ”, ‘int’);
$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

$this->setState(‘limit’, $limit);
$this->setState(‘limitstart’, $limitstart);
}

function getData()
{
// if data hasn’t already been obtained, load it

if (empty($this->_data))
{
$query = $this->_buildQuery();
$this->_data = $this->_getList($query, $this->getState(‘limitstart’), $this->getState(‘limit’));
}

return $this->_data;
}

function getTotal()
{
// Load the content if it doesn’t already exist
if (empty($this->_total)) {
$query = $this->_buildQuery();

$this->_total = $this->_getListCount($query);
}
return $this->_total;
}

function getPagination()
{
// Load the content if it doesn’t already exist
if (empty($this->_pagination)) {
jimport(‘joomla.html.pagination’);
$this->_pagination = new JPagination($this->getTotal(), $this->getState(‘limitstart’), $this->getState(‘limit’) );
}
return $this->_pagination;
}

function _buildQuery()
{
// write your query here
$query = “SELECT * FROM #__tablename”;
return $query;
}

}

2) in display() function of view.html.php file of view write code

$this->assignRef(‘items’, $this->get(‘Data’));
$this->assignRef(‘pagination’, $this->get(‘Pagination’));

3) in default.php

// to show the content
foreach($this->items as $item) {}

// pagination code
echo $this->pagination->getListFooter();

Advertisements
 
Leave a comment

Posted by on April 2, 2013 in Joomla

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: