php实现读取mysql数据库全部文章或者产品并分页
读取数据库内容,并分页展示,这个php程序简单的实现了,适合刚入门的PHP新手,如果做网站遇到同样问题的朋友,可以参考一下:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>php分页</title>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″>
<?php
//连接数据库
$db=mysql_connect(“localhost”,”uj8wa6d_f”,”a090922″);
mysql_select_db(“uj8wa6d”,$db);
//设置UTF8,防止中文乱码
mysql_query(“set names utf8”);
//设定每一页显示的记录数
$pagesize=8;
//取得记录总数,计算总页数用
$res=mysql_query(“select count(*) from king__product” ,$db);
$myrow = mysql_fetch_array($res);
$numrows=$myrow[0];
//计算总页数
$pages=intval($numrows/$pagesize);
if ($numrows%$pagesize)
$pages++;
//获取底部分页传达的值,网上部分教程没有这个导致分页无法浏览
$page=$_GET[‘page’];
//判断页数设置与否,如无则定义为首页
if (!isset($page))
$page=1;
//判断转到页数
if (isset($ys))
if ($ys>$pages)
$page=$pages;
else
$page=$ys;
//计算记录偏移量
$offset=$pagesize*($page-1);
//取记录
$res=mysql_query(“select kpath,kimage,ktitle from king__product order by kid desc limit $offset,$pagesize” ,$db);
//循环显示记录
if ($myrow = mysql_fetch_array($res))
{
$i=0;
?>
<div style=”width:100%; margin-top:5px; padding:0px;”>
<?php
do {
$i++;
?>
<li style=”list-style:none; width:180px; float:left; height:220px; text-align:center;”>
<a href=”/<?php echo $myrow[0];?>” target=”blank”><img src=”<?php echo $myrow[1];?>” width=”180″ height=”150″ />
<br/> <?php echo mb_substr(“$myrow[2]”,0,25,”utf-8″); ?></a>
</li>
<?php
}
while ($myrow = mysql_fetch_array($res));
echo “</div><div style=‘clear:both; width:100%;’></div>” ;
}
//显示总页数
echo “<div align=’center’ style=‘width:100%;clear:both;’>共有”.$pages.”页(“.$page.”/”.$pages.”) ”;
//显示分页数
for ($i=1;$i<$page;$i++)
echo “<a href=’page_pro.php?page=”.$i.”‘>第”.$i .”页</a> “;
echo “第”.$page.”页 “;
for ($i=$page+1;$i<=$pages;$i++)
echo “<a href=’page_pro.php?page=”.$i.”‘>第”.$i .”页</a> “;
echo ” ”;
//显示转到页数
echo “<form action=’page_pro.php’ method=’post’> “;
//计算首页、上一页、下一页、尾页的页数值
$first=1;
$prev=$page-1;
$next=$page+1;
$last=$pages;
if ($page>1)
{
echo “<a href=’page_pro.php?page=”.$first.”‘>首页</a> “;
echo “<a href=’page_pro.php?page=”.$prev.”‘>上一页</a> “;
}
if ($page<$pages)
{
echo “<a href=’page_pro.php?page=”.$next.”‘>下一页</a> “;
echo “<a href=’page_pro.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>”;
?>
</body>
</html>