Home  >  Article  >  Backend Development  >  php简单的分页程序_php实例

php简单的分页程序_php实例

PHP中文网
PHP中文网Original
2016-05-17 09:37:54646browse

php简单的分页程序_php实例

 代码如下:

<html>  
<head>  
<meta http-equiv="Content-Type" c>  
<title>用户信息总汇</title>  
<style type="text/css">  
<!--  
body,td,th {  
font-size: 14px;  
}  
.STYLE1 {  
font-size: 16px;  
color: #666666;  
}  
-->  
</style>  
</head>  
<body leftMargin=0 topMargin=0 rightmargin=0 >  
<p>  
<?php  
#############################################################  
###########        连接数据库                ################  
#############################################################  
$db=mysql_connect("192.168.0.2","root","goalwe608");  //数据库,用户名,密码  
mysql_select_db("86pos",$db);   //数据库  
############# 分页 ############  
//设定每一页显示的记录数  
$pagesize=5;  
//取得记录总数  
$res=mysql_query("select count(id) from buyer " ,$db);  
$myrow = mysql_fetch_array($res);  
$numrows=$myrow[0];  
//计算总页数  
$pages=intval($numrows/$pagesize);  
if ($numrows%$pagesize)  
  $pages++;  
//判断页数设置与否,如无则定义为首页  
if (!isset($page))  
  $page=1;  
//防止恶意访问  
if ($_GET>$pages)  
  $page=$pages;  
if ($_GET<1)  
  $page=1;  
if (!ereg("^[0-9]+$",$_GET))  
  $page=1;  
if ($_GET=="")  
  $page=1;  
    
//计算记录偏移量  
$offset=$pagesize*($page-1);  
//取记录  
$res=mysql_query("select username,pass,mail,add_time from buyer order by add_time 
limit $offset,$pagesize" ,$db);  
//循环显示记录  
  if ($myrow = mysql_fetch_array($res))  
  {  
   $i=0;  
?>  
</p>  
<p>  </p>  
<table width="100%" border="0" align="center">  
  <tr>  
    <td width="40%" height="30"> </td>  
    <td width="20%" align="center"><span class="STYLE1">用户信息管理<br>  
          <br>  
    </span></td>  
    <td width="15%"> </td>  
    <td><? $time=date ("Y-l-F H:i:s");echo $time;?>  
       </td>  
  </tr>  
</table>  
<p align="center">  
  -------------------------------------------------------------  
</p>  
<table width="90%" border="1" align="center" cellpadding="0" cellspacing="0">  
  <tr>  
    <td width="10%" height="24" bgcolor="#cccccc"><p align="center">序号</p></td>  
    <td width="20%" bgcolor="#cccccc"><p align="center">用户名</p></td>  
    <td width="20%" bgcolor="#cccccc"><p align="center">密码</p></td>  
    <td width="30%" bgcolor="#cccccc"><p align="center">电子邮箱</p></td>  
    <td bgcolor="#cccccc"><p align="center">添加时间</p></td>  
  </tr>  
  <?php  
do   
{  
  $i++;  
?>  
  <tr>  
    <td width="10%"><p align="center"><?php echo $i;?></p></td>  
    <td><p align="center"><font size="2"> <?php echo $myrow[&#39;username&#39;];?></font></p></td>  
    <td><p align="center"><font size="2"><?php echo $myrow[&#39;pass&#39;];?></font></p></td>  
    <td><p align="center"><font size="2"><?php echo $myrow[&#39;mail&#39;];?></font></p></td>  
    <td><p align="center"><font size="2"><?php echo $myrow[&#39;add_time&#39;];?></font></p></td>  
  </tr>  
  <?php  
}  
while ($myrow = mysql_fetch_array($res));  
  echo "</table>" ;  
}  
//显示总页数  
echo "<p align=&#39;center&#39;>共有".$pages."页(".$page."/".$pages.")<br>";  
//显示分页数  
for ($i=1;$i<$page;$i++)  
  echo "<a href=&#39;$SELF_PHP?page=".$i."&#39;>第".$i ."页</a>  ";  
  echo "第".$page."页  ";  
for ($i=$page+1;$i<=$pages;$i++)  
  echo "<a href=&#39;$SELF_PHP?page=".$i."&#39;>第".$i ."页</a>  ";  
  echo "<br>";  
//显示转到页数  
  echo "<form action=&#39;$SELF_PHP&#39; method=&#39;post&#39;> ";  
//计算首页、上一页、下一页、尾页的页数值  
$first=1;  
$prev=$page-1;  
$next=$page+1;  
$last=$pages;  
if ($page>1)  
{  
  echo "<a href=&#39;$SELF_PHP?page=".$first."&#39;>首页</a>  ";  
  echo "<a href=&#39;$SELF_PHP?page=".$prev."&#39;>上一页</a>  ";  
}  
if ($page<$pages)  
{  
  echo "<a href=&#39;$SELF_PHP?page=".$next."&#39;>下一页</a>  ";  
  echo "<a href=&#39;$SELF_PHP?page=".$last."&#39;>尾页</a>  ";  
}  
echo "</form>";  
echo "</p>";  
?>  
</table>  
<p> </p>  
</body>  
</html>

以上就是php简单的分页程序_php实例的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
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