PHP翻页功能

本文参考: PHP翻页功能|Break易站  代码比较长,可以移步到这里看!

PHP的翻页主要是为了要展示产品和数据,如果有从数据库过来的数据如果太多的话,那么,制作翻页功能就是最好的展现数据的办法。

废话不多说啦,这个章节的代码比较多,需要大家仔细看,对于连接数据库,还有数据库的展现,这个章节里面的代码也包含了。

<html>
<head>
<style type="text/css">
li { 
float:left; /* 往左浮动 */
}
</style>
</head>
<body>
<?php

include_once("connector.php");              
//设定每一页显示的记录数
$pagesize=12;
// mysql_select_db("test",$link);
//取得记录总数$rs,计算总页数用
$allrs=mysql_query("SELECT COUNT(*) FROM test");
$allrow = mysql_fetch_array($allrs);
$numrows=$allrow[0];
//计算页面的总页数
$pages=intval($numrows/$pagesize);
if ($numrows%$pagesize)
$pages++;
//设置页数,会拿到点击上一页或者下一页传递过来的页数
if (isset($_GET['page'])){
$page=intval($_GET['page']);
}
else{
//设置为第一页 
$page=1;
}
//计算记录偏移量
$offset=$pagesize*($page - 1);
$rs=mysql_query("SELECT * FROM test");
if ($myrow = mysql_fetch_array($rs))
{
$i=0;
do {
$i++;

?>

<ul>
<li><?=$myrow["No"]?><li>
<li><?=$myrow["FirstName"]?><li>
<li><?=$myrow["LastName"]?><li>
</ul>


<?php
}
while ($myrow = mysql_fetch_array($rs));
}

$first=1;
$prev=$page-1;
$next=$page+1;
$last=$pages;
//输出HTML代码,编辑好html的前后按钮;
//"号在PHP的转移字符是\"
//这里要看懂最基本的html代码<a>,<img>以及标签的属性;
echo "<table><tr><th>";
if ($page > 1)
{
echo "<a href='CheckAll.php?page=".$prev."'><img src=\"images/back_button.png\" style=\"width:45px;height:45px;padding-left:100px;\"></a> ";
}else{
echo "<a><img src=\"images/back_button.png\" style=\"width:45px;height:45px;padding-left:100px;\"></a> ";
}
echo "</th><th>";
echo "<a align='center' style=\"border:1px solid green;background:white; width:100px;height:20px;\">(".$page."/".$pages.")";
echo "</a>";
echo "</th><th>";
if ($page < $pages)
{
echo "<a href='CheckAll.php?page=".$next."'><img src=\"images/forward_button.png\" style=\"width:45px;height:45px;pad;\"></</a>";
}else{
echo "<a><img src=\"images/forward_button.png\" style=\"width:45px;height:45px;pad;\"></</a>";
}
echo "</th><th>";
echo "<form id=\"PageNo_Form\"  method=\"post\" action=\"\" name=\"CheckAll\">";
echo "<input name=\"PageNo\" type=\"text\" id=\"PageNo\" value=\"\" class=\"keyword\" placeholder=\"页\" style=\"border:1px solid green;background:white; width:40px;\"/>";
echo "<input type=\"submit\" value=\"跳页\" onclick=\"submitinputvalue()\" style=\"border:1px solid green;background:green; width:40px;\"/>";
echo "</form>";
echo "</th> <tr>";
echo "</table>";
?>
</body>
</html>
更多PHP内容PHP翻页功能|Br

posted on 2017-08-20 23:29  MrAit  阅读(216)  评论(0)    收藏  举报

导航