<?php
/***************************************************************************************/
/** **/
/** Copyright (c)'2010 by Igor Homyn **/
/** **/
/** hide@address.com **/
/** **/
/** http://www.phpclasses.org/package/6689-PHP-Dynamic-pagination-of-MySQL-table.html **/
/** **/
/***************************************************************************************/
class AJAXpagination {
var $request;
var $lines;
var $pg_key;
var $rows;
var $rownumber;
var $page_navigation;
var $sort_buttons;
var $name_file;
function AJAXpagination($sql, $pagination_key, $perpage = LINES_PER_PAGE) {
$this->request = $sql;
$this->lines = $perpage;
$this->pg_key = $pagination_key;
$this->name_file = $this->make_pagination_url($_SERVER['REQUEST_URI']);
if (!(isset($_GET['option']) && ($_GET['option'] == 'getdata'))) {
$_SESSION['pg'][$this->pg_key]['request'] = $this->request;
$_SESSION['pg'][$this->pg_key]['lines'] = $this->lines;
}
$this->prepare_page();
}
function make_pagination_url($uri) {
$splitted_request = explode('?', $uri);
if (!isset($splitted_request[1])) {
return $uri.'?pgkey='.$this->pg_key;
}
$uri = '';
$uri_keys = explode('&', $splitted_request[1]);
reset($uri_keys);
while (list($key, $val) = each($uri_keys)) {
$uri_ky = explode('=', $val);
if (($uri_ky[0] != 'pg') && ($uri_ky[0] != 'pgkey') && ($uri_ky[0] != 'option')) {
if ($uri != '') {
$uri .= '&';
}
$uri .= $val;
}
}
if ($uri != '') {
return $splitted_request[0].'?'.$uri.'&pgkey='.$this->pg_key;
} else {
return $splitted_request[0].'?pgkey='.$this->pg_key;
}
}
function prepare_page() {
$ajax_click = str_replace('tbl1', $this->pg_key, AJAX_PG_ONCLICK);
if (isset($_SESSION[$this->pg_key])) {
$page = $_SESSION[$this->pg_key];
} else {
$page = 1;
}
if (isset($_GET['pg']) && is_numeric($_GET['pg'])) {
if (!isset($_GET['pgkey']) || ($_GET['pgkey'] == $this->pg_key)) {
$page = $_GET['pg'];
$_SESSION[$this->pg_key] = $page;
}
}
$splitted_request = explode(' from ', $this->request);
$result = mysql_query('select count(*) rownumber from '.$splitted_request[1]);
if ($row = mysql_fetch_array($result)) {
$this->rownumber = $row['rownumber'];
}
$last_page = ceil($this->rownumber/$this->lines);
if ($last_page < 1) {
$last_page = 1;
}
if ($page > $last_page) {
$page = $last_page;
}
$this->rows = array();
$result = mysql_query($this->request.' limit '.(($page-1) * $this->lines).', '.$this->lines);
while ($row = mysql_fetch_array($result)) {
reset($row);
while (list($key, $val) = each($row)) {
$row[$key] = stripslashes($row[$key]);
}
$this->rows[] = $row;
}
mysql_free_result($result);
if ((count($this->rows) < $this->lines) && ($this->rownumber > $this->lines)) {
$z = $this->rows[0];
reset($z);
while (list($key, $val) = each($z)) {
$z[$key] = '';
if (substr($key, 0, 3) == 'img')
$z[$key] = 'images/1w.gif';
}
$k = count($this->rows);
while ($k < $this->lines) {
$this->rows[] = $z;
$k += 1;
}
}
if ($page > 2) {
$this->page_navigation = '<a href="'.$this->name_file.'&pg=1" onclick="'.$ajax_click.'"><<</a> ';
} else {
$this->page_navigation = '<< ';
}
if ($page > 1) {
$this->page_navigation .= '<a href="'.$this->name_file.'&pg='.(($page-1) > 1 ? ($page-1):1).'" onclick="'.$ajax_click.'"><</a> ';
} else {
$this->page_navigation .= '< ';
}
$l_pages = round(PAGES_IN_NAV_LINE/2);
$r_pages = PAGES_IN_NAV_LINE - $l_pages;
if ($page < $l_pages) {
$i = 1;
$r_pages += $l_pages - $page;
$l_pages = $page;
} else {
$i = $page - $l_pages + 1;
}
if (($page+$r_pages) > $last_page) {
$r_pages = $last_page - $page;
$l_pages = PAGES_IN_NAV_LINE - $r_pages;
$i = $page - $l_pages + 1;
}
if ($i < 1) {
$i = 1;
}
while ($i < $page) {
$this->page_navigation .= ' .. <a href="'.$this->name_file.'&pg='.$i.'" onclick="'.$ajax_click.'">'.$i.'</a>';
$i++;
}
$this->page_navigation .= ' .. '.$page.' .. ';
$i = $page;
while (($i < $last_page) && ($i < ($page+$r_pages))) {
$i++;
$this->page_navigation .= '<a href="'.$this->name_file.'&pg='.$i.'" onclick="'.$ajax_click.'">'.$i.'</a> .. ';
}
if ($page < $last_page) {
$this->page_navigation .= '<a href="'.$this->name_file.'&pg='.($page+1).'" onclick="'.$ajax_click.'">></a> ';
} else {
$this->page_navigation .= '> ';
}
if ($page < ($last_page-1)) {
$this->page_navigation .= '<a href="'.$this->name_file.'&pg='.$last_page.'" onclick="'.$ajax_click.'">>></a>';
} else {
$this->page_navigation .= '>>';
}
if ($this->rownumber <= $this->lines) {
$this->page_navigation = '';
}
return 0;
}
}
function JSONdata() {
if (isset($_GET['pgkey']) && ($_GET['pgkey'] != '')) {
$gpgkey = $_GET['pgkey'];
} else {
print json_encode('error no pgkey');
exit;
}
if (isset($_GET['pg']) && ($_GET['pg'] != '')) {
$gpg = $_GET['pg'];
} else {
print json_encode('error no pg');
exit;
}
$_SESSION[$gpgkey] = $gpg;
$x = new AJAXpagination($_SESSION['pg'][$gpgkey]['request'], $gpgkey, $_SESSION['pg'][$gpgkey]['lines']);
reset($x->rows);
while (list($key, $val) = each($x->rows)) {
reset($val);
while (list($key1, $val1) = each($val)) {
$x->rows[$key][$key1] = iconv("Windows-1251", "UTF-8", $x->rows[$key][$key1]);
}
}
$x->location = $_SERVER['HTTP_REFERER'].'#'.$gpgkey.'_pg_'.$gpg;
return json_encode($x);
}
define('PAGES_IN_NAV_LINE', 10);
define('LINES_PER_PAGE', 10);
define('AJAX_PG_FUNC', '
<script type="text/javascript">
var t = \'\';
var loct = document.location.href;
if (SessionPageDiff(loct)) {
loct = \'\';
onTimer();
} else {
t = setTimeout("onTimer()",400);
}
function SessionPageDiff(loct) {
return true; // just a placeholder function, smart logic should be implemented there
}
function onTimer () {
if (loct != document.location.href) {
var url = \'\';
loct = document.location.href;
if (loct.indexOf(\'?\') > 0) {
componentList = loct.split(\'?\');
componentList2 = componentList[1].split(\'#\');
componentList3 = componentList2[1].split(\'_pg_\');
url = componentList[0] + \'?\' + componentList2[0] + \'&pgkey=\' + componentList3[0] + \'&pg=\' + componentList3[1];
} else {
if (loct.indexOf(\'#\') > 0) {
componentList = loct.split(\'#\');
componentList3 = componentList[1].split(\'_pg_\');
url = componentList[0] + \'?pgkey=\' + componentList3[0] + \'&pg=\' + componentList3[1];
} else {
url = \'\';
}
}
if (url != \'\') {
var log = new Spinner($(componentList3[0]));
log.show(true);
var request = new Request.JSON({url: url + \'&option=getdata\',
onComplete: function(jsonObj) {
ajaxComplete(jsonObj, componentList3[0], log)
}}).send();
}
}
t = setTimeout("onTimer()", 400);
}
function ajaxComplete(jsonObj, pgkey, log) {
$(pgkey+\'nav.top\').set(\'html\', jsonObj.page_navigation);
$(pgkey+\'nav.bot\').set(\'html\', jsonObj.page_navigation);
log.hide(true);
for (var i=0; i < jsonObj.rows.length; i++) {
$(pgkey).getElements(\'tr\')[i].getElements(\'span\').each(function(item1, index1) {
if (item1.id.indexOf(pgkey+\'.\') >= 0) {
item1.set(\'html\', eval(\'jsonObj.rows[i].\'+item1.id.replace(pgkey+\'.\', \'\')) );
}
});
$(pgkey).getElements(\'tr\')[i].getElements(\'img\').each(function(item1, index1) {
if (item1.id.indexOf(pgkey+\'.\') >= 0) {
item1.set(\'src\', eval(\'jsonObj.rows[i].\'+item1.id.replace(pgkey+\'.\', \'\')) );
}
});
$(pgkey).getElements(\'tr\')[i].getElements(\'a\').each(function(item1, index1) {
if (item1.id.indexOf(pgkey+\'.\') >= 0) {
item1.set(\'href\', eval(\'jsonObj.rows[i].\'+item1.id.replace(pgkey+\'.\', \'\')) );
}
});
}
loct = jsonObj.location;
document.location = jsonObj.location;
}
</script>
');
define('AJAX_PG_ONCLICK', '
var log = new Spinner($(\'tbl1\'));
log.show(true);
var request = new Request.JSON({url: this.href + \'&option=getdata\',
onComplete: function(jsonObj) {
ajaxComplete(jsonObj, \'tbl1\', log)
}}).send();
return false;
');
?>