我在自己的网站上有一个哈希数据库,我想分页(因为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
请不要介意我制作它的糟糕方式,我只是想让它起作用,而不是看起来漂亮:)
答案 0 :(得分:1)
php中的分页
按页数限制每页的项目数
if(isset($_GET['page']))
$page=$_GET['page'];
else
$page=1;
$max_results=6;
$from=( ($page * $max_results) - $max_results);
在此我限制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'> << 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>";
}
}
答案 1 :(得分:0)
考虑在sql查询中使用LIMIT子句 - 它将是高效的,并将解决您的问题
答案 2 :(得分:0)
这段代码使它适用于我:)('5'部分仍然需要编辑$max_pages
$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