php³¬¼¶·ÖÒ³Àà(תÔØ×Ôphp100ÂÛ̳)
·ÖÒ³ÏÔʾÊÇÒ»Öַdz£³£¼ûµÄä¯ÀÀºÍÏÔʾ´óÁ¿Êý¾ÝµÄ·½·¨£¬ÊôÓÚweb±à³ÌÖÐ×î³£´¦ÀíµÄʼþÖ®Ò»¡£¶ÔÓÚweb±à³ÌµÄÀÏÊÖÀ´Ëµ£¬±àдÕâÖÖ´úÂëʵÔÚ ÊǺͺôÎüÒ»Ñù×ÔÈ»£¬µ«ÊǶÔÓÚ³õѧÕßÀ´Ëµ£¬³£³£¶ÔÕâ¸öÎÊÌâÃþ²»×ÅÍ·Ð÷£¬Òò´ËÌصØ׫д´ËÎĶÔÕâ¸öÎÊÌâ½øÐÐÏêϸµÄ½²½â£¬Á¦ÇóÈÿ´ÍêÕâƪÎÄÕµÄÅóÓÑÔÚ¿´ÍêÒÔºó¶ÔÓÚ ·ÖÒ³ÏÔʾµÄÔÀíºÍʵÏÖ·½·¨ÓÐËùÁ˽⡣±¾ÎÄÊʺϳõѧÕßÔĶÁ£¬ËùÓÐʾÀý´úÂë¾ùʹÓÃphp±àд¡£
2¡¢ÔÀí
Ëùν·ÖÒ³ÏÔʾ£¬Ò²¾ÍÊǽ«Êý¾Ý¿âÖеĽá¹û¼¯ÈËΪµÄ·Ö³ÉÒ»¶ÎÒ»¶ÎµÄÀ´ÏÔʾ£¬ÕâÀïÐèÒªÁ½¸ö³õʼµÄ²ÎÊý£º
ÿҳ¶àÉÙÌõ¼Ç¼£¨$PageSize£©£¿
µ±Ç°Êǵڼ¸Ò³£¨$CurrentPageID£©£¿
ÏÖÔÚÖ»ÒªÔÙ¸øÎÒÒ»¸ö½á¹û¼¯£¬ÎҾͿÉÒÔÏÔʾij¶ÎÌض¨µÄ½á¹û³öÀ´¡£
ÖÁÓÚÆäËûµÄ²ÎÊý£¬±ÈÈ磺ÉÏÒ»Ò³£¨$PReviousPageID£©¡¢ÏÂÒ»Ò³£¨$NextPageID£©¡¢×ÜÒ³Êý£¨$numPages£©µÈµÈ£¬¶¼¿ÉÒÔ¸ù¾ÝÇ°±ßÕ⼸¸ö¶«Î÷µÃµ½¡£
ÒÔMySQLÊý¾Ý¿âΪÀý£¬Èç¹ûÒª´Ó±íÄÚ½Øȡij¶ÎÄÚÈÝ£¬sqlÓï¾ä¿ÉÒÔÓãºselect * from table limit offset, rows¡£¿´¿´ÏÂÃæÒ»×ésqlÓï¾ä£¬³¢ÊÔһϷ¢ÏÖÆäÖеĹæÂÊ¡£
Ç°10Ìõ¼Ç¼£ºselect * from table limit 0,10
µÚ11ÖÁ20Ìõ¼Ç¼£ºselect * from table limit 10,10
µÚ21ÖÁ30Ìõ¼Ç¼£ºselect * from table limit 20,10
¡¡
ÕâÒ»×ésqlÓï¾äÆäʵ¾ÍÊǵ±$PageSize=10µÄʱºòÈ¡±íÄÚÿһҳÊý¾ÝµÄsqlÓï¾ä£¬ÎÒÃÇ¿ÉÒÔ×ܽá³öÕâÑùÒ»¸öÄ£°å£º
select * from table limit ($CurrentPageID - 1) * $PageSize, $PageSize
ÄÃÕâ¸öÄ£°å´úÈë¶ÔÓ¦µÄÖµºÍÉϱßÄÇÒ»×ésqlÓï¾ä¶ÔÕÕһϿ´¿´ÊDz»ÊÇÄÇô»ØÊ¡£¸ã¶¨ÁË×îÖØÒªµÄÈçºÎ»ñÈ¡Êý¾ÝµÄÎÊÌâÒÔºó£¬Ê£Ïµľͽö½öÊÇ´«µÝ²ÎÊý£¬¹¹ÔìºÏÊʵÄsqlÓï¾äÈ»ºóʹÓÃphp´ÓÊý¾Ý¿âÄÚ»ñÈ¡Êý¾Ý²¢ÏÔʾÁË¡£ÒÔÏÂÎÒ½«ÓþßÌå´úÂë¼ÓÒÔ˵Ã÷¡£
3¡¢¼òµ¥´úÂë
ÇëÏêϸÔĶÁÒÔÏ´úÂ룬×Ô¼ºµ÷ÊÔÔËÐÐÒ»´Î£¬×îºÃ°ÑËüÐÞ¸ÄÒ»´Î£¬¼ÓÉÏ×Ô¼ºµÄ¹¦ÄÜ£¬±ÈÈçËÑË÷µÈµÈ¡£
<?php // ½¨Á¢Êý¾Ý¿âÁ¬½Ó $link = mysql_connect("localhost", "mysql_user", "mysql_passWord") or die("Could not connect: " . mysql_error()); // »ñÈ¡µ±Ç°Ò³Êý if( isset($_GET['page']) ){ $page = intval( $_GET['page'] ); } else{ $page = 1; } // ÿҳÊýÁ¿ $PageSize = 10; // »ñÈ¡×ÜÊý¾ÝÁ¿ $sql = "select count(*) as amount from table"; $result = mysql_query($sql); $row = mysql_fetch_row($result); $amount = $row['amount']; // ¼ÇËã×ܹ²ÓжàÉÙÒ³ if( $amount ){ if( $amount < $page_size ){ $page_count = 1; } //Èç¹û×ÜÊý¾ÝÁ¿Ð¡ÓÚ$PageSize£¬ÄÇôֻÓÐÒ»Ò³ if( $amount % $page_size ){ //È¡×ÜÊý¾ÝÁ¿³ýÒÔÿҳÊýµÄÓàÊý $page_count = (int)($amount / $page_size) + 1; //Èç¹ûÓÐÓàÊý£¬ÔòÒ³ÊýµÈÓÚ×ÜÊý¾ÝÁ¿³ýÒÔÿҳÊýµÄ½á¹ûÈ¡ÕûÔÙ¼ÓÒ» }else{ $page_count = $amount / $page_size; //Èç¹ûûÓÐÓàÊý£¬ÔòÒ³ÊýµÈÓÚ×ÜÊý¾ÝÁ¿³ýÒÔÿҳÊýµÄ½á¹û } } else{ $page_count = 0; } // ·Ò³Á´½Ó $page_string = ''; if( $page == 1 ){ $page_string .= 'µÚÒ»Ò³|ÉÏÒ»Ò³|'; } else{ $page_string .= '<a href=?page=1>µÚÒ»Ò³</a>|<a href=?page='.($page-1).'>ÉÏÒ»Ò³</a>|'; } if( ($page == $page_count) || ($page_count == 0) ){ $page_string .= 'ÏÂÒ»Ò³|βҳ'; } else{ $page_string .= '<a href=?page='.($page+1).'>ÏÂÒ»Ò³</a>|<a href=?page='.$page_count.'>βҳ</a>'; } // »ñÈ¡Êý¾Ý£¬ÒÔ¶þάÊý×é¸ñʽ·µ»Ø½á¹û if( $amount ){ $sql = "select * from table order by id desc limit ". ($page-1)*$page_size .", $page_size"; $result = mysql_query($sql); while ( $row = mysql_fetch_row($result) ){ $rowset[] = $row; } }else{ $rowset = array(); } // ûÓаüº¬ÏÔʾ½á¹ûµÄ´úÂ룬ÄDz»ÔÚÌÖÂÛ·¶Î§£¬Ö»ÒªÓÃforeach¾Í¿ÉÒԺܼòµ¥µÄÓõõ½µÄ¶þάÊý×éÀ´ÏÔʾ½á¹û ?> 4¡¢OO·ç¸ñ´úÂë ÒÔÏ´úÂëÖеÄÊý¾Ý¿âÁ¬½ÓÊÇʹÓõÄpear dbÀà½øÐд¦Àí <?php // FileName: Pager.class.php // ·ÖÒ³À࣬Õâ¸öÀà½ö½öÓÃÓÚ´¦ÀíÊý¾Ý½á¹¹£¬²»¸ºÔð´¦ÀíÏÔʾµÄ¹¤×÷ Class Pager { var $PageSize; //ÿҳµÄÊýÁ¿ var $CurrentPageID; //µ±Ç°µÄÒ³Êý var $NextPageID; //ÏÂÒ»Ò³ var $PreviousPageID; //ÉÏÒ»Ò³ var $numPages; //×ÜÒ³Êý var $numItems; //×ܼǼÊý var $isFirstPage; //ÊÇ·ñµÚÒ»Ò³ var $isLastPage; //ÊÇ·ñ×îºóÒ»Ò³ var $sql; //sql²éѯÓï¾ä function Pager($option) { global $db; $this->_setOptions($option); // ×ÜÌõÊý if ( !isset($this->numItems) ) { $res = $db->query($this->sql); $this->numItems = $res->numRows(); } // ×ÜÒ³Êý if ( $this->numItems > 0 ) { if ( $this->numItems < $this->PageSize ){ $this->numPages = 1; } if ( $this->numItems % $this->PageSize ) { $this->numPages= (int)($this->numItems / $this->PageSize) + 1; } else { $this->numPages = $this->numItems / $this->PageSize; } } else { $this->numPages = 0; } switch ( $this->CurrentPageID ) { case $this->numPages == 1: $this->isFirstPage = true; $this->isLastPage = true; break; case 1: $this->isFirstPage = true; $this->isLastPage = false; break; case $this->numPages: $this->isFirstPage = false; $this->isLastPage = true; break; default: $this->isFirstPage = false; $this->isLastPage = false; } if ( $this->numPages > 1 ) { if ( !$this->isLastPage ) { $this->NextPageID = $this->CurrentPageID + 1; } if ( !$this->isFirstPage ) { $this->PreviousPageID = $this->CurrentPageID - 1; } } return true; } /*** * * ·µ»Ø½á¹û¼¯µÄÊý¾Ý¿âÁ¬½Ó * ÔÚ½á¹û¼¯±È½Ï´óµÄʱºò¿ÉÒÔÖ±½ÓʹÓÃÕâ¸ö·½·¨»ñµÃÊý¾Ý¿âÁ¬½Ó£¬È»ºóÔÚÀàÖ®Íâ±éÀú£¬ÕâÑù¿ªÏú½ÏС * Èç¹û½á¹û¼¯²»ÊǺܴ󣬿ÉÒÔÖ±½ÓʹÓÃgetPageDataµÄ·½Ê½»ñÈ¡¶þάÊý×é¸ñʽµÄ½á¹û * getPageData·½·¨Ò²Êǵ÷Óñ¾·½·¨À´»ñÈ¡½á¹ûµÄ * ***/ function getDataLink() { if ( $this->numItems ) { global $db; $PageID = $this->CurrentPageID; $from = ($PageID - 1)*$this->PageSize; $count = $this->PageSize; $link = $db->limitQuery($this->sql, $from, $count); //ʹÓÃPear DB::limitQuery·½·¨±£Ö¤Êý¾Ý¿â¼æÈÝÐÔ return $link; } else { return false; } } /*** * * ÒÔ¶þάÊý×éµÄ¸ñʽ·µ»Ø½á¹û¼¯ * ***/ function getPageData() { if ( $this->numItems ) { if ( $res = $this->getDataLink() ) { if ( $res->numRows() ) { while ( $row = $res->fetchRow() ) { $result[] = $row; } } else { $result = array(); } return $result; } else { return false; } } else { return false; } } function _setOptions($option) { $allow_options = array( 'PageSize', 'CurrentPageID', 'sql', 'numItems' ); foreach ( $option as $key => $value ) { if ( in_array($key, $allow_options) && ($value != null) ) { $this->$key = $value; } } return true; } } ?>²âÊÔ´úÂëÈçÏ£º
<?php // FileName: test_pager.php // ÕâÊÇÒ»¶Î¼òµ¥µÄʾÀý´úÂ룬ǰ±ßÊ¡ÂÔÁËʹÓÃpear dbÀཨÁ¢Êý¾Ý¿âÁ¬½ÓµÄ´úÂë require "Pager.class.php"; if ( isset($_GET['page']) ) { $page = (int)$_GET['page']; } else { $page = 1; } $sql = "select * from table order by id"; $pager_option = array( "sql" => $sql, "PageSize" => 10, "CurrentPageID" => $page ); if ( isset($_GET['numItems']) ) { $pager_option['numItems'] = (int)$_GET['numItems']; } $pager = @new Pager($pager_option); $data = $pager->getPageData(); if ( $pager->isFirstPage ) { $turnover = "Ê×Ò³|ÉÏÒ»Ò³|"; } else { $turnover = "<a href='?page=1&numItems=".$pager->numItems."'>Ê×Ò³</a>|< a href='?page=".$pager->PreviousPageID."&numItems=".$pager-> numItems."'>ÉÏÒ»Ò³</a>|"; } if ( $pager->isLastPage ) { $turnover .= "ÏÂÒ»Ò³|βҳ"; } else { $turnover .= "<a href='?page=".$pager->NextPageID."&numItems=".$pager-> numItems."'>ÏÂÒ»Ò³</a>|<a href='?page=".$pager->numPages."&numItems=".$pager-> numItems."'>βҳ</a>"; } ?>ÐèҪ˵Ã÷µÄµØ·½ÓÐÁ½¸ö£º
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ