Home > Backend Development > PHP Tutorial > PHP paging function_PHP tutorial

PHP paging function_PHP tutorial

WBOY
Release: 2016-07-21 16:12:04
Original
979 people have browsed it

/*
需求,建立一个test数据库,在里边建一个test表,里面就
只要id字段,输入一下数据就可以啦。。
由于水平有限,难免出错。。
*/
$conn = mysql_connect("localhost","root","");

$maxnum = 2; //每页显示记录条数

mysql_select_db("test", $conn);
$query1 = "SELECT COUNT(*) AS totalrows FROM test ";
$result1 = mysql_query($query1, $conn) or die(mysql_error());
$row1 = mysql_fetch_assoc($result1);
$totalRows1 = $row1['totalrows']; //数据集数据总条数
$totalpages = ceil($totalRows1/$maxnum);//计算可分页总数,ceil()为上舍函数

if(!isset($_GET['page']) || !intval($_GET['page']) || $_GET['page'] > $totalpages) $page = 1;  //对3种出错进行默认处理
//在url参数page不存在时,page不为10进制数时,page大于可分页数时,默认为1
else $page = $_GET['page'];

$startnum = ($page - 1)*$maxnum; //从数据集第$startnum条开始取,注意数据集是从0开始的

$query = "SELECT * FROM test LIMIT $startnum,$maxnum";//选择出符合要求的数据 从$startnum条数据开始,选出$maxnum行
$result = mysql_query($query, $conn) or die(mysql_error());
$row = mysql_fetch_assoc($result);

?>



分页示例






  
    
  
   do {
?>
  
    


分页示例





  

echo "Total$totalRows1record"; 🎜>                                                                                                                 //Next page
$maxpages = 4;//When processing paging << < 1 2 3 4 > >>Display 4 pages
$pagepre = 1;//If the current page If it is 4, the previous $pagepre page should also be displayed, such as << < 3 /4/ 5 6 > >> Display page 3

if($page != 1) { echo "<< ";
echo "< ";}

if($maxpages>=$totalpages) //If the total records are not enough Display 4 pages
{$pgstart = 1;$pgend = $totalpages;}//Print processing of different pages
elseif(($page-$pagepre-1+$maxpages)>$totalpages) //Just like the total number of pages is 6 and the current number is 5, the previous 3 4 should be displayed, not just 4
{$pgstart = $totalpages - $maxpages + 1;$pgend = $totalpages; }
else{
$pgstart=(($page<=$pagepre)?1:($page-$pagepre));//When the current page is 1, it will only be 1 2 3 4 > >>Instead of 0 1 2 3 > >>
$pgend=(($pgstart==1)?$maxpages:($pgstart+$maxpages-1));


for($pg=$pgstart;$pg<=$pgend;$pg++){ //Jump menu
if($pg == $page) echo "$pg ";
else echo "< ;a href="".$_SERVER['PHP_SELF']."?page=$pg">$pg "; > {echo "> ";
echo ">> ";}
        ?>
          
          选择
                     echo "".$pg1."";
          }?>
      
    



    没有任何记录

  
  



mysql_free_result($result1);
mysql_free_result($result);
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/313750.htmlTechArticle?php /* 需求,建立一个test数据库,在里边建一个test表,里面就 只要id字段,输入一下数据就可以啦。。 由于水平有限,难免出错。。 */ $...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template