简体   繁体   English

在PhP中限制分页页面

[英]Limit Pagination pages in PhP

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) 我在自己的网站上有一个哈希数据库,所以我想分页(因为210.000哈希在不降低网站速度的情况下无法轻松加载)

now I have paginated my stuff, but I get about 21.000 pages how can I limit this to about 100pages??? 现在我已经分页了,但是我得到了约21.000页,如何将其限制为约100页呢???

$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 :) 请不要介意我制作的糟糕方法,我只是想让它工作,而不是看起来很漂亮:)

Pagination in php 在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);     

in this i limit 6( $max_results=6; ) items per page you can change according to your need 在此我$max_results=6;限制6( $max_results=6; )个项目, 您可以根据需要进行更改

Use the query to limit the results 使用查询限制结果

$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'>&nbsp;<< Previous&nbsp;</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'>&nbsp;$i&nbsp;</a>";
    }
}
if($page<$total_pages)
{
    $next=($page+1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\" style='text-decoration:none'>&nbsp;Next>></a>";
}
}   

考虑在sql查询中使用LIMIT子句-这将是有效的并且可以解决您的问题

This piece of code made it work for me :) (the '5' pieces still gotta be editted with the $max_pages 这段代码使它对我$max_pages :)(仍然需要使用$max_pages “ 5”部分

$max_pages = 10;
$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 = max(1, $page - 5); $i <= min($page + 5, $total_pages); $i++) { 
            echo "<a href='?page=".$i."'>".$i."</a> "; 
}; 
echo "<a href='?page=$total_pages'>".'>|'."</a> "; // Goto last page

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM