Jak zauwazylem bardzo wiele osob ma problemy z porcjowaniem wynikow z bazy danych (ja tez z tym mialem problem).
Ale po dlugich poszukiwaniach znalazlem bardzo ciekawa klase do porcjowania (na
http://www.phpclasses.org/)
Sklada sie ona z 2 plikow:
page.inc.php (tego pliku nie trzeba nic modyfikowac)
example.php (ten plik wyswietla rezultaty z bazy i je porcjuje)
Mysle ze kazdy majac te pliki poradzi sobie z porcjowaniem wynikow !
page.inc.php
[php:1:29cdbc21f1]
<?php
/**
* A Class use to Mysql page
*
* @author Avenger <avenger@php.net>
* @version 1.02
* @update 2003-04-27 23:11:29
*
*
* Useage:
* $p = new show_page; // Create the Object
* $p->file="ttt.php"; // Set used the class name,default to the PHP_SELF
* $p->pvar="pagecount"; // Set the page parameter,default to "p"
* $p->setvar(array("a" => '1', "b" => '2')); // Set the parameter u wanta pass, Note:used it above the $p->set
* $p->set(20,2000,1); // Set the object parameter,total 3,1st is the pagesize,2nd is the total record count,3rd is the currerent page count,default is will auto read the GET variable
* $p->output(0); // Output,the parameter is true will return a string
* echo $p->limit(0); // Out put the limit.eg. "SELECT * FROM TABLE LIMIT {$p->limit()}"; set it true,will return a array
*
*/
class show_page {
/**
* output
*
* @var string
*/
var $output;
/**
* used the class's page
*
* @var string
*/
var $file;
/**
* page pass parameter
*
* @var string
*/
var $pvar = "p";
/**
* pagesize
*
* @var integer
*/
var $psize;
/**
* currerent page
*
* @var ingeger
*/
var $curr;
/**
* pass array
*
* @var array
*/
var $varstr;
/**
* total page count
*
* @var integer
*/
var $tpage;
/**
* page set
*
* @access public
* @param int $pagesize The pagesize
* @param int $total The toal records count
* @param int $current Current page,keep empty will auto read the get variable
* @return void
*/
function set($pagesize=20,$total,$current=false) {
global $HTTP_SERVER_VARS,$HTTP_GET_VARS;
$this->tpage = ceil($total/$pagesize);
if (!$current) {$current = $HTTP_GET_VARS[$this->pvar];}
if ($current>$this->tpage) {$current = $this->tpage;}
if ($current<1) {$current = 1;}
$this->curr = $current;
$this->psize = $pagesize;
if (!$this->file) {
$this->file = $HTTP_SERVER_VARS['PHP_SELF'];
}
strstr($this->file,'?') ? $middle = '&' : $middle = '?';
if ($this->tpage > 1) {
if ($current>10) {
$this->output.='<a class="en1" href='.$this->file.$middle.$this->pvar.'='.($current-10).($this->varstr).' title="Previous 10"><<<</a> ';
}
if ($current>1) {
$this->output.='<a class="en1" href='.$this->file.$middle.$this->pvar.'='.($current-1).($this->varstr).' title="Previous"><<</a> ';
}
$start = floor($current/10)*10;
$end = $start+9;
if ($start<1) $start=1;
if ($end>$this->tpage) $end=$this->tpage;
for ($i=$start; $i<=$end; $i++) {
if ($current==$i) {
$this->output.='<font color="red" class="en1">'.$i.'</font> ';
} else {
$this->output.='<a class="en1" href="'.$this->file.$middle.$this->pvar.'='.$i.$this->varstr.'">['.$i.']</a> ';
}
}
if ($current<$this->tpage) {
$this->output.='<a class="en1" href='.$this->file.$middle.$this->pvar.'='.($current+1).($this->varstr).' title="Next">>></a> ';
}
if ($this->tpage>10 && ($this->tpage-$current)>=10 ) {
$this->output.='<a class="en1" href='.$this->file.$middle.$this->pvar.'='.($current+10).($this->varstr).' title="Next 10">>>></a>';
}
}
}
/**
* passed variable set
*
* @access public
* @param array $data The parameter u wanta passed,see the example above.
* @return void
*/
function setvar($data) {
foreach ($data as $k=>$v) {
$this->varstr.='&'.$k.'='.urlencode($v);
}
}
/**
* Output
*
* @access public
* @param bool $return Set it true will return a string,otherwish will output automatic
* @return string
*/
function output($return = false) {
if ($return) {
return $this->output;
} else {
echo $this->output;
}
}
/**
* Make the limit
*
* @access public
* @return string
*/
function limit($arr=0) {
if ($arr) {
settype($arr,"array");
$arr[1] = ($this->curr-1)*$this->psize;
$arr[2] = $this->psize;
return $arr;
} else {
return (($this->curr-1)*$this->psize).','.$this->psize;
}
}
} //End Class
?>[/php:1:29cdbc21f1]
example.php
[php:1:29cdbc21f1]
<?php
/**
* A Example to use show_page class
*
* @author Avenger <avenger@php.net>
* @version $Id 2003-04-30 9:09:29 $
*/
/*
At first,assume we have a table like this:
table: user
Records:
Id Name Sex Age
====================================
1 Jim M 18
2 Bill M 26
3 Ted M 30
4 Mary F 16
5 Avenger M 19
6 Meow F 12
U can run the SQL below create the test database automatic
CREATE TABLE `user` (`Id` int(11) NOT NULL auto_increment,`Name` varchar(100) NOT NULL default '',`Sex` enum('M','F') default NULL,`Age` smallint(3) default NULL,PRIMARY KEY (`Id`)) TYPE=MyISAM;
INSERT INTO `user` VALUES("1", "Jim", "M", "18");
INSERT INTO `user` VALUES("2", "Bill", "M", "26");
INSERT INTO `user` VALUES("3", "Ted", "M", "30");
INSERT INTO `user` VALUES("4", "Mary", "F", "16");
INSERT INTO `user` VALUES("5", "Avenger", "M", "19");
INSERT INTO `user` VALUES("6", "Meow", "F", "12");
Note: Change the username and password to your's in the above script part of connect database.
*/
require_once 'page.inc.php';
$pg = new show_page;
$conn = mysql_connect("localhost", "pawel", "xx") or die("Could not connect: " . mysql_error());
mysql_select_db("xx") or die(mysql_error());
// Recive the order variable
$_GET['order'] ? $order=$_GET['order'] : $order='Id';
// Count the total records
$result = mysql_query("SELECT COUNT(*) FROM user") or die("Invalid query: " . mysql_error());
$total_records = mysql_result($result,0);
// Page Set
$_GET['pagesize'] ? $pagesize=$_GET['pagesize'] : $pagesize = 2;
$pg->setvar(array('order'=>$order,'pagesize'=>$pagesize));
$pg->set($pagesize,$total_records);
/* u can used this script to limit too:
$limit = $pg->limit(1);
$SQL = "SELECT * FROM user ORDER BY $order LIMIT {$limit[1]},{$limit[2]}";
*/
$SQL = "SELECT * FROM user ORDER BY $order LIMIT ".$pg->limit();
$result = mysql_query($SQL) or die("Invalid query: " . mysql_error());
echo "<h1>Galeria</h1>";
$pg->output();
echo "<table border='1' width='300'>n";
echo "<tr><th><a href='example.php?order=Id&pagesize=$pagesize'>Id</a></th><th><a href='example.php?order=Name&pagesize=$pagesize'>Name</a></th><th><a href='example.php?order=Sex&pagesize=$pagesize'>Sex</a></th><th><a href='example.php?order=Age&pagesize=$pagesize'>Age</a></th></tr>n";
while ($row = mysql_fetch_object($result)) {
printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td></tr>", $row->Id,$row->Name,$row->Sex,$row->Age);
}
echo "</table><br>";
echo "<b>Result:</b><br>n";
echo "<blockquote>
TotalRecord:{$total_records}<br>
TotalPage:{$pg->tpage}<br>
PageSize:{$pg->psize}<br>
CurrerentPage:{$pg->curr}<br>
PassedParameter:
";
print_r($pg->varstr);
echo "</blockquote>";
echo "<form action=''>Change pagesize:<input type='text' name='pagesize' size='2' value='{$pagesize}'> <input type='submit' value='change'></form>";
echo "<hr>";
$pg->output();
mysql_close($conn);
?>[/php:1:29cdbc21f1]