php简略数字分页显示代码

WBOY
发布: 2016-06-13 13:11:53
原创
1229 人浏览过

php简单数字分页显示代码

<?php // database connection info
/*
$conn = mysql_connect('localhost','dbusername','dbpass') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('dbname',$conn) or trigger_error("SQL", E_USER_ERROR);
*/
// find out how many rows are in the table 
/*
$sql = "SELECT COUNT(*) FROM numbers";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
*/
$numrows = 1000;
// number of rows to show per page
$rowsperpage = 10;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage ";
// while there are rows to be fetched...
/*
while ($list = mysql_fetch_assoc($result)) {
   // echo data
   echo $list['id'] . " : " . $list['number'] . "<br>";
} // end while
*/
/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show  ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show  ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x  0) && ($x $x] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href="%7B%24_SERVER%5B" php_self>$x</a> ";
      } // end else
   } // end if 
} // end for
                 
// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href="%7B%24_SERVER%5B" php_self>></a> ";
   // echo forward link for lastpage
   echo " <a href="%7B%24_SERVER%5B" php_self>>></a> ";
} // end if
/****** end build pagination links ******/
?>



<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>分页</title>
<style type="text/css">
body{font:12px/1.5em "Century Gothic";}
</style>



	<p>摘自:<a href="http://www.phpfreaks.com/tutorial/basic-pagination">http://www.phpfreaks.com/tutorial/basic-pagination</a></p>

登录后复制
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!