簡體   English   中英

在PhP中限制分頁頁面

[英]Limit Pagination pages in PhP

我在自己的網站上有一個哈希數據庫,所以我想分頁(因為210.000哈希在不降低網站速度的情況下無法輕松加載)

現在我已經分頁了,但是我得到了約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

請不要介意我制作的糟糕方法,我只是想讓它工作,而不是看起來很漂亮:)

在PHP中分頁

將每頁的項目數限制為

if(isset($_GET['page']))
    $page=$_GET['page'];
else 
    $page=1;

$max_results=6;
$from=( ($page * $max_results) - $max_results);     

在此我$max_results=6;限制6( $max_results=6; )個項目, 您可以根據需要進行更改

使用查詢限制結果

$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}

分頁概念提供頁面鏈接

$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子句-這將是有效的並且可以解決您的問題

這段代碼使它對我$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