Home > Article > Backend Development > PHP paging display function source code_PHP tutorial
php分页显示函数 源代码
require('web_page.php'); //包含分页程序
//数据库配置
$mysql_host = 'localhost'; //数据库服务器
$mysql_user = 'root'; //数据库用户名
$mysql_pass = '123456'; //数据库密码
$mysql_db = 'test'; //数据库名
//连接mysql数据库
$link = mysql_connect($mysql_host,$mysql_user,$mysql_pass) or die ('连接MYSQL服务器出错');
mysql_select_db($mysql_db,$link) or die ('连接MYSQL数据库出错');
//分页开始
$sql_page = "select news_id,news_title from cms_news order by news_id desc";
$sql = mysql_query($sql_page);
$num = mysql_num_rows($sql); //总条数
$max = 1; //每页条数
$pagenum = ceil($num/$max); //可分页数
if(!isset($_GET['page']) or !intval($_GET['page']) or !is_numeric($_GET['page']) or $_GET['page'] > $pagenum){
$page = 1; //当页数不存在 不为十进制数 不是数字 大于可分页数 为1
}else{
$page = $_GET['page']; //当前页数
}
$min = ($page-1)*$max; //当前页从$min条开始
$sql = "{$sql_page} limit $min,$max";
echo '
//mysql_free_result($sql); //释放资源
?>
//End of pagination
mysql_close();
?>
The above is the calling method and the bottom is the paging program
function web_page($pageurl="", $pageselect = true) {
global $page,$num,$pagenum; //The current number of pages, the total number of pages that can be divided into pages
echo "A total of $num records,";
$uppage = $page - 1; //Previous page
$downpage = $page + 1; //Next page
$lr = 5; //Show how many pages to connect
$ left = floor(($lr-1)/2); //How many page connections are displayed on the left
$right = floor($lr/2); //How many page connections are displayed on the right
//Find the start page and end page below
if($page <= $left){ //If the left side of the current page is not enough to display the page number
$leftpage = 1;
$rightpage = (($lr<$pagenum)?$lr:$pagenum);
}elseif(($pagenum-$page) < $right){ //If the current page right is not enough to display the page number
$ leftpage = (($pagenum<$lr)?1:($pagenum-$lr+1));
$rightpage = $pagenum;
}else{ //The number of pages can be displayed on the left and right
$ leftpage = $page - $left;
$rightpage = $page + $right;
}
//The first $lr page and the next $lr page
$qianpage = (($ page-$lr) < 1?1:($page-$lr));
$houpage = (($page+$lr) > $pagenum?$pagenum:($page+$lr));
//Output paging
if($page != 1){
echo "<< << ;/a> ";
}else{
echo "<<<< /span> ";
}
for($pages = $leftpage; $pages <= $rightpage; $pages++){
if($pages == $page){
echo "$pages ";
}else{
echo "$pages< ;/a> ";
}
}
if($page != $pagenum){
echo "> >>";
}else{
echo ">< span class='disabled'> >> ";
}
//Jump
$javapage = <<
function web_page(targ,selObj,restore){
eval("self"+".location='"+selObj.options[selObj.selectedIndex].value+"' ");
if (restore) selObj.selectedIndex=0;
}
EOM;
echo $javapage;
if ($pageselect){
echo "Jump to page";
}
}
?>