Location: PHPKode > projects > SQLite PHP Admin > spa/tbrowse.php
<?php
/***********************************************************************

		Copyright (c) 2011, Andrew V. Pleshakov	(hide@address.com)
		All rights reserved.

		This file is part of SPA (SQLite PHP Admin)
		See file LICENSE.txt for details

************************************************************************/

require_once('include.php');
if((!isset($_REQUEST['object'])) || (!$pla_db)) {
	header('Location: main.php');
	die;
}
$object = $_REQUEST['object'];
$tar['table'] = $object;
$total = $tar['total'] = $pla_db->numRows($object);

if(isset($_REQUEST['page'])) $_SESSION['page'] = intval($_REQUEST['page']);
if(isset($_POST['lim'])) {
	$_SESSION['lim'] = intval($_REQUEST['lim']);
//	$_SESSION['page'] = 1;
}
	$lim = $_SESSION['lim'];
	$page = $_SESSION['page'];
	if($lim < 1) $lim = 50;
	if($page < 1) $page = 1;
	$pages = ceil($total / $lim);
	 if(!$pages) $pages = 1;
	if($page > $pages) $page = $pages;
	$from = $lim * ($page - 1);

	$buf = "<table>\n";
	$sql = "SELECT _ROWID_,* FROM '$object' LIMIT $from,$lim";
	if(!preg_match('/redit/',$_SERVER['HTTP_REFERER']))
		$_SESSION['lquery'] = "SELECT * FROM '$object' LIMIT $from,$lim";// _ROWID_ off
	$pla_db->query($sql);

	$buf .=  "<tr>\n<th></th>\n<th></th>\n";

	$nf = $pla_db->numFields();
	for ($i=1; $i<$nf; $i++) // omit first column (_rowid_)
		$buf .= "<th>".$pla_db->fieldName($i)."</th>\n";
	$buf .= "</tr>\n";

$tar['numr'] = ConstructLim($lim);	// rows on a page

$object = $tar['utable'] = urlencode($object);
$url = "tbrowse.php?object=$object&amp;page=";

	$navbar = array();
	if($pages > 1) {
		$hp = round($pages / 2);		// half of pages
		$qp = round($hp / 4);	// quarter of pages
		$lq = $fq = false;
		if($page < ($hp - 1)) $lq = true;
		elseif ($page <= ($hp + 1)) { /*$fq = $lq = true*/; }
		else $fq = true;
		$navpages = array();
		if($fq) fillpages($navpages,$qp-1,$qp+1);
		fillpages($navpages,$hp-1,$hp+1);
		if($lq) fillpages($navpages,$qp+$hp-1,$qp+$hp+1);
		fillpages($navpages,$page-5,$page+5);
		fillpages($navpages,$page+10,$page+12);
		fillpages($navpages,$page-12,$page-10);
		reset($navpages);
		foreach($navpages as $i) {
			if($i < 1) { unset($navpages[$i]);$navpages[1] = 1; }
			elseif($i > $pages) { unset($navpages[$i]);$navpages[$pages] = $pages; }
		}
		sort($navpages);
		reset($navpages);
		$pe = current($navpages) - 1;
		if($pe > 0) $navbar[] = '..';
		foreach($navpages as $i) {
			if($pe + 1 < $i) {$navbar[] = '..';}
			$pe = $i;
			$navbar[] = "{$i}";
		}
		if($i < $pages) $navbar[] = '..';
	}
	$s = '';
	foreach($navbar as $val) {
		$s .= "<option value=\"{$val}\"";
		if(intval($val) == $page) $s .= ' selected="selected"';
		if($val == '..') $s.= ' disabled="disabled"';
		$s .= ">{$val}</option>";
	}
	$navbar = design_render('misc/gennav',array('opts'=>$s,'url'=>$url));
	if($pages > 1) $tar['navbar'] = ShowNavbar($navbar,$page,$pages,$url);
	else	$tar['navbar'] = '';
	
	$rows = 0;
	while($row = $pla_db->fetchArray()) {
		$rows++;
		$buf .= "<tr>\n";
		$tar['row'] = $row[0];
		$buf .= design_render('extra/rowact',$tar);
		for ($i=1; $i<$nf; $i++)
			$buf .= '<td>'.htmlentities(iconv($encoding,DEF_CHARSET,$row[$i]),ENT_QUOTES,DEF_CHARSET)."</td>\n";
		$buf .= "</tr>\n";
	}
	$buf .= "</table>\n";
$tar['rows'] = $rows;
if($rows) $tar['buf'] = $buf;
//	else $tar['buf'] = '';

die(design_render('table/tbrowse',$tar));
////////////////////////////////////////////////////////////////////////////////
function ShowNavbar($navbar,$page,$pages,$url) {
//function ShowNavbar($navbar,$pra,$prv,$nxt,$nxa) {
	$rr = array();
	$rr['navbar'] = $navbar;
	$rr['page'] = $page;
	$rr['pages'] = $pages;
	$rr['url'] = $url;
//	$rr['nxa'] = $nxa;
	return design_render('misc/navbar',$rr);
}

function fillpages(&$navpages,$fr,$to) {
	for($i = $fr; $i <= $to; $i++)
		$navpages[$i] = $i;
}

function ConstructLim($lim) {
	$s = "<select class=\"limit\" name=\"lim\" onchange=\"this.form.submit();\">\n";
	$trk = array(10,20,30,50,100,250,500,1000);
	foreach($trk as $val) {
		$s .= "\t<option value=\"{$val}\"";
		if($lim == $val) $s .= ' selected="selected"';
		$s .= ">{$val}</option>\n";
	}
	$s .= "</select>\n";
	return $s;
}
Return current item: SQLite PHP Admin