两段适合初学者的php+mysql分页代码
<?php
$DBSERVER = "localhost";
$DBUSER = "root";
$DBPASSWORD = "root";
$DBNAME = "test";
$pagesize = 10;
$server = mysql_connect($DBSERVER, $DBUSER, $DBPASSWORD);
$db = mysql_select_db($DBNAME);
mysql_query("set names sjis");
$sql = " select count(*) as total from test1 where a='11' and b='22' ";
$rst = mysql_query($sql);
$row = mysql_fetch_array($rst);
$rowcount = $row["total"];
mysql_free_result($rst);
$pagecount = bcdiv($rowcount+$pagesize-1,$pagesize,0);
if(!isset($pageno)) $pageno = 1;
if($pageno<1) $pageno = 1;
if($pageno>$pagecount)
$pageno = $pagecount;
if($pageno>0){
$href = eregi_replace("%2f","/",urlencode($PHP_SELF));
echo "データ検索結果は:<br />";
if($pageno>1){
echo '<a href="' . $href . '?pageno=' . ($pageno-1) . '">前ページ</a> ';
}
else{
echo '前ページ';
}
for($i=1;$i<$pageno;$i++){
echo '<a href="' . $href . '?pageno=' . $i . '">' . $i . '</a> ';
}
echo $pageno . ' ';
for($i++;$i<=$pagecount;$i++){
echo '<a href="' . $href . '?pageno=' . $i . '">' . $i . '</a> ';
}
if($pageno<$pagecount){
echo '<a href="' . $href . '?pageno=' . ($pageno+1) . '">次ページ</a> ';
}
else{
echo '次ページ ';
}
$offset = ($pageno-1) * $pagesize;
$sql = "select * from entry where a='$_POST[a]' and b='$_POST[b]' LIMIT $offset,$pagesize";
$rst = mysql_query($sql);
$num_fields = mysql_num_fields($rst);
$i = 0;
while($i<$num_fields){
$fields[$i] = mysql_field_name($rst,$i);
$i++;
}
echo '<table border="1" cellspacing="0" cellpadding="0">';
echo '<tr>';
reset($fields);
while(list(,$field_name)=each($fields)){
echo "<th>$field_name</th>";
}
echo '</tr>';
while($row=mysql_fetch_array($rst)){
echo '<tr>';
reset($fields);
while(list(,$field_name)=each($fields)){
$field_value = $row[$field_name];
if($field_value==""){
echo '<td> </td>';
}
else{
echo "<td>$field_value</td>";
}
}
echo '</tr>';
}
echo '</table>';
mysql_free_result($rst);
}
else{
echo "データない" ;
}
mysql_close($server);
?>
=======================================================================
<?php
//连接数据库
$db=mysql_connect("localhost","root","");
mysql_select_db("hy",$db);
//设定每一页显示的记录数
$pagesize=25;
//取得记录总数,计算总页数用
$res=mysql_query("select count(*) from main " ,$db);
$myrow = mysql_fetch_array($res);
$numrows=$myrow[0];
//计算总页数
$pages=intval($numrows/$pagesize);
if ($numrows%$pagesize)
$pages++;
//判断页数设置与否,如无则定义为首页
if (!isset($page))
$page=1;
//判断转到页数
if (isset($ys))
if ($ys>$pages)
$page=$pages;
else
$page=$ys;
//计算记录偏移量
$offset=$pagesize*($page-1);
//取记录
$res=mysql_query("select id,title from main order by id desc limit $offset,$pagesize" ,$db);
//循环显示记录
if ($myrow = mysql_fetch_array($res))
{
$i=0;
?>
<table width="101%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="5%" bgcolor="#E1E9FB"></td>
<td width="95%" bgcolor="#E1E9FB"><font color="#FF6666" size="2">内容</font></td>
</tr>
<?php
do {
$i++;
?>
<tr>
<td width="5%" bgcolor="#E6F2FF"><?php echo $i;?></td>
<td width="95%" bgcolor="#E6F2FF"><font size="2">
<a href="javascript:popwin('view.php?id=<?php echo $myrow[0];?>')" ><?php echo $myrow[1];?></a></font></td>
</tr>
<?php
}
while ($myrow = mysql_fetch_array($res));
echo "</table>" ;
}
//显示总页数
echo "<div align='center'>共有".$pages."页(".$page."/".$pages.")<br>";
//显示分页数
for ($i=1;$i<$page;$i++)
echo "<a href='fenye.php?page=".$i."'>第".$i ."页</a> ";
echo "第".$page."页 ";
for ($i=$page+1;$i<=$pages;$i++)
echo "<a href='fenye.php?page=".$i."'>第".$i ."页</a> ";
echo "<br>";
//显示转到页数
echo "<form action='fenye.php' method='post'> ";
//计算首页、上一页、下一页、尾页的页数值
$first=1;
$prev=$page-1;
$next=$page+1;
$last=$pages;
if ($page>1)
{
echo "<a href='fenye.php?page=".$first."'>首页</a> ";
echo "<a href='fenye.php?page=".$prev."'>上一页</a> ";
}
if ($page<$pages)
{
echo "<a href='fenye.php?page=".$next."'>下一页</a> ";
echo "<a href='fenye.php?page=".$last."'>尾页</a> ";
}
echo "转到<input type=text name='ys' size='2' value=".$page.">页";
echo "<input type=submit name='Submit' value='Go'>";
echo "</form>";
echo "</div>";
?>