Ò»¸ö·ÖÒ³ÏÔʾÀà,ÀàËÆ°Ù¶ÈËÑË÷½á¹ûµÄÒ³ºÅÐÐ
ǰһ¶Îʱ¼äдÁËÒ»¸ö¿ÉÒÔ×Ô¶¯Æ«ÒƵ¼º½Ò³ºÅµÄ·ÖÒ³ÏÔʾÀà,Ч¹ûÓеãÀàËÆ°Ù¶ÈµÄ½á¹ûÒ³ºÅÐÐ("ÉÏÒ»Ò³","ÏÂÒ»Ò³"Ö®¼äµÄÒ»ÐÐÊý×Ö),ÓÉÓÚÊÇÐÂÊÖ,ÎÊÌâÓÐһЩ,ÏÖÔÚ°ÑËü·ÅÉÏÀ´,Çë¸÷λÅóÓÑÃǰïæ¸ÄÁ¼Ò»ÏÂ.Èç¹ûÄÄλÅóÓѲ»ÏÓÆú±¾ÈËˮƽÓÐÏ޵ϰ,Ò²¿ÉÒÔÖ±½ÓʹÓÃ,´úÂëÎÒ×÷Á˼òµ¥µÄ²âÊÔ,»¹¿ÉÒÔ.Áí¸öÄÄλÅóÓÑÖªµÀÔÚPEARÀïÓÐÀàËÆµÄ°üµÄ»°,Çë°ÑÃû³Æ·¢ÉÏÀ´,ÄÜÓÃPEARµ±È»¾Í²»±ØÔÙÕâÑùÂé·³ÁË.<?php
class ShowByPage
{
private $conn;
private $sql;
private $pageSize;
private $groupPages;
private $total;
private $pages;
private $pageNum;
private $startResult;
private $endResult;
private $startPage;
private $endPage;
private $groupStyle;
private $currentPageStyle;
private $GETVarsString;
private $prev;
private $next;
private function setPageNum()
{
if ( isset($_GET["pageNum"]) )
{
$this->pageNum = ceil(0 + $_GET["pageNum"]);
if ( $this->pageNum < 1 || $this->pageNum > $this->pages )
$this->pageNum = 1;
}
else
{
$this->pageNum = 1;
}
}
public function __construct($conn,$sql,$pageSize,$groupPages)
{
$this->conn = $conn;
$this->sql = $sql;
$this->pageSize = $pageSize;
$this->groupPages = $groupPages;
$this->total = mysql_num_rows( mysql_query($sql,$conn) );
$this->pages = ceil( $this->total / $this->pageSize );
$this->setPageNum();
$this->startResult = ( $this->pageNum - 1 ) * $this->pageSize;
$this->endResult = $this->startResult + $this->pageSize - 1;
$this->endResult > $this->total ? $this->endResult = $this->total : true;
if ( $this->pages < $this->groupPages || $this->pageNum <= ceil($this->groupPages/2) )
{
$this->startPage = 1;
}
elseif ( $this->pageNum > $this->pages - ceil($this->groupPages/2) )
{
$this->startPage = $this->pages - $this->groupPages + 1;
}
else
{
$this->startPage = $this->pageNum - ceil( $this->groupPages / 2 ) + 1;
}
$this->endPage = $this->startPage + $this->groupPages - 1;
$this->endPage > $this->pages ? $this->endPage = $this->pages : true;
$this->groupStyle = false;
$this->currentPageStyle = "color:#FF0000;font-size:larger;";
$this->GETVarsString = false;
$this->prev = "ǰһҳ";
$this->next = "ºóÒ»Ò³";
}
public function getTotal()
{
return $this->total;
}
public function getPages()
{
return $this->pages;
}
public function getPageNum()
{
return $this->pageNum;
}
public function getStartResult()
{
return $this->startResult + 1;
}
public function getEndResult()
{
return $this->endResult + 1;
}
public function getResult()
{
return mysql_query( $this->sql." limit ".$this->startResult.",".$this->pageSize, $this->conn );
}
public function setGroupStyle($style)
{
$this->groupStyle = $style;
}
public function setCurrentPageStyle($style)
{
$this->currentPageStyle = $style;
}
public function setGETVarsString($varsString)
{
$this->GETVarsString = $varsString;
}
public function displayGroup()
{
if ( $this->groupStyle )
echo "<div style=\"{$this->groupStyle}\"> ";
else
echo "<div> ";
for ( $i=$this->startPage; $i<=$this->endPage; $i++ )
{
if ( $i == $this->pageNum )
echo "<span style=\"{$this->currentPageStyle}\">{$i}</span> ";
else
{
echo "<a href=\"{$_SERVER['PHP_SELF']}?pageNum={$i}";
if ( $this->GETVarsString )
echo "&{$this->GETVarsString}";
echo "\">{$i}</a> ";
}
}
echo "</div>";
}
public function setPrevString($prevString)
{
$this->prev = $prevString;
}
public function setNextString($nextString)
{
$this->next = $nextString;
}
public function setPrevImg($src)
{
$this->prev = "<img style=\"border-width:0px;\" src=\"".$src."\">";
}
public function setNextImg($src)
{
$this->next = "<img style=\"border-width:0px;\" src=\"".$src."\">";
}
public function displayPrev()
{
if ( $this->pageNum > 1 )
{
echo "<a href=\"{$_SERVER['PHP_SELF']}?pageNum=".($this->pageNum-1);
if ( $this->GETVarsString )
echo "&{$this->GETVarsString}";
echo "\">{$this->prev}</a>";
}
}
public function displayNext()
{
if ( $this->pageNum < $this->pages )
{
echo "<a href=\"{$_SERVER['PHP_SELF']}?pageNum=".($this->pageNum+1);
if ( $this->GETVarsString )
echo "&{$this->GETVarsString}";
echo "\">{$this->next}</a>";
}
}
}
?>
<?php
//ShowByPageÀà˵Ã÷Îĵµ
//
//__construct( $conn, $sql, $pageSize, $groupPages ) ¹¹Ô캯Êý
//$conn Êý¾Ý¿âÓÐЧÁ¬½Ó
//$sql ¿ÉÒԵõ½ËùÓнá¹û(²»·ÖÒ³)µÄSQLÓï¾ä
//$pageSize ÿҳ×î¶àÏÔʾ½á¹ûÊýÄ¿
//$groupPages ÓÃÀ´µ¼º½µÄÒ³±àºÅÊýÄ¿
//getTotal() ·µ»ØËùÓнá¹ûµÄ×ÜÊý
//getPages() ·µ»Ø½á¹ûµÄ×î´óÒ³Êý
//getPageNum() ·µ»Øµ±Ç°ËùÔÚÒ³µÄ½á¹ûÒ³Êý
//getStartResult() ·µ»Øµ±Ç°Ò³µÚÒ»Ìõ½á¹ûµÄ±àºÅ(´Ó1¿ªÊ¼)
//getEndResult() ·µ»Øµ±Ç°Ò³×îºóÒ»Ìõ½á¹ûµÄ±àºÅ
//getResult() ·µ»Øµ±Ç°Ò³µÄÊý¾Ý¿â½á¹û¼¯(¾ÍÊÇmysql²éѯ·µ»ØµÄ½á¹û¼¯)
//setGroupStyle( $style ) ÉèÖÃÓÃÀ´µ¼º½µÄ½á¹ûÒ³ºÅµÄcssÑùʽ(µ±Ç°ËùÔÚÒ³µÄÒ³ºÅÀýÍâ)
//$style µ¼º½µÄ½á¹ûÒ³ºÅÑùʽ(html±ê¼ÇµÄstyleÊôÐÔÖµ)µÄÑùʽ
//displayGroup() ÏÔʾµ¼º½Ò³ºÅÁбí
//setCurrentPageStyle( $style ) ÉèÖõ¼º½Ò³ºÅÁбíÖеĵ±Ç°ËùÔÚÒ³µÄcssÑùʽ
//$style µ¼º½Ò³ºÅÁбíÖеĵ±Ç°ËùÔÚÒ³µÄÑùʽ( html±ê¼ÇµÄstyleÊôÐÔÖµ)
//setGETVarsString( $varsString ) ÉèÖÃÓ¦ÔÚ¸÷Ò³µ¼º½Ê±´«µÝµÄ¸½¼ÓGET±äÁ¿×Ö·û´®
//$varsString ¸÷Ò³µ¼º½Ê±´«µÝµÄGET±äÁ¿×Ö·û´®( htmlµÄa±ê¼ÇhrefÊôÐÔÖµÄÚ?Ö®ºóµÄ±äÁ¿ÃûºÍÖµ×é³ÉµÄ×Ö·û´®)
//setPrevString( $prevString ) ÉèÖõ¼º½µ½Ç°Ò»Ò³µÄ×Ö·û´®(²»Ó¦ÓësetPreImg()Ò»ÆðʹÓÃ)
//$prevString µ¼º½µ½Ç°Ò»Ò³µÄ×Ö·û´®±êʶ
//setNextString( $nextString ) ÉèÖõ¼º½µ½ºóÒ»Ò³µÄ×Ö·û´®
//$nextString µ¼º½µ½ºóÒ»Ò³µÄ×Ö·û´®±êʶ
//setPrevImg( $src ) ÉèÖõ¼º½µ½Ç°Ò»Ò³µÄͼƬURL(²»Ó¦ÓësetPrevString()Ò»ÆðʹÓÃ)
//$src µ¼º½µ½Ç°Ò»Ò³µÄͼƬµÄURLÖµ
//setNextImg( $src ) ÉèÖõ¼º½µ½ºóÒ»ÃæµÄͼƬURL(²»Ó¦ÓësetNextString()Ò»ÆðʹÓÃ)
//$src µ¼º½µ½ºóÒ»Ò³µÄͼƬµÄURLÖµ
//displayPrev() ÏÔʾµ¼º½µ½Ç°Ò»Ò³µÄÁ´½Ó(µ±Ç°ÎªµÚһҳʱ²»ÏÔʾ)
//displayNext() ÏÔʾµ¼º½µ½ºóÒ»Ò³µÄÁ´½Ó(µ±Ç°Îª×îºóһҳʱ²»ÏÔʾ)
?>
ÎÒ¸öÈ˵ĸоõÊDZ¾À´Ó¦¸Ã·ÖΪ¼¸¸öÀà,²»¹ýµ±Ê±Í¼¸ö·½±ã,¾ÍÈ«·ÅÒ»¸öÀàÀïÁË,»¹ÓоÍÊÇûÓÐÏÔʾ²éѯ½á¹ûº¯Êý,ÕâÖ÷ÒªÊÇ¿¼Âǵ½Ê¹ÓÃÄ£°å.
[color=#333333][font=Tahoma,Verdana][size=12px][/size][/font][/color]
Ò³:
[1]