在PhP中限制分页页面 [英] Limit Pagination pages in PhP
问题描述
我在自己的网站上有一个哈希数据库,因此我想进行分页(因为210.000哈希值不降低网站速度就不会轻易加载)
I have a hash database on my own site, and I wanted to paginate (because 210.000hashes won't load easily without slowing down the site)
现在我已经对我的书进行了分页,但是我得到了大约21.000页 如何将其限制为大约100页??
now I have paginated my stuff, but I get about 21.000 pages how can I limit this to about 100pages???
$sql = "SELECT * FROM hashes";
$rs_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='?page=".$i."'>".$i."</a> ";
};
echo "<a href='?page=$total_pages'>".'>|'."</a> "; // Goto last page
请不要介意我制作的糟糕方法,我只是想让它工作,而不是看起来很漂亮:)
Please don't mind the crappy way I made it, I just want it to work, not look pretty :)
推荐答案
在PHP中分页
将每页的项目数限制为
Limit the number of items per page by
if(isset($_GET['page']))
$page=$_GET['page'];
else
$page=1;
$max_results=6;
$from=( ($page * $max_results) - $max_results);
在此情况下,每页限制6(
$max_results=6;
)个项目您可以更改 根据您的需要
in this i limit 6(
$max_results=6;
) items per page you can change according to your need
使用查询限制结果
$search=mysql_query("SELECT * FROM `hashes` LIMIT $from,$max_results");
$total_results=mysql_result(mysql_query("select count(*) as num from `hashes`"),0);
while($r=mysql_fetch_object($search))
{//your code}
提供页面链接的分页概念
Pagination concept to provide links for pages
$total_pages=ceil($total_results/$max_results);
if($total_results>$max_results)
{
if($page>1)
{
$prev=($page-1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\" style='text-decoration:none'> << Previous </a>";
}
for($i=1;$i<=$total_pages;$i++)
{
if($page==$i)
{
echo $i."";
}
else
{
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\" style='text-decoration:none'> $i </a>";
}
}
if($page<$total_pages)
{
$next=($page+1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\" style='text-decoration:none'> Next>></a>";
}
}
这篇关于在PhP中限制分页页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!