【问题标题】:Expanding page navigation splitted Query result扩展页面导航拆分查询结果
【发布时间】:2013-03-25 05:55:34
【问题描述】:

我的查询结果被拆分为多个页面。这是使用以下代码完成的:

    <?php 
    if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
    $start_from = ($page-1) * 10; 
    $sql = "SELECT * FROM profiles where hoofdrubriek = '".$rubriekpage2."'  ORDER BY bedrijfsnaam LIMIT $start_from, 10"; 
    $rs_result = mysql_query($sql);

while ($row = mysql_fetch_assoc($rs_result)) { 

echo a result....

}

?>

接下来是为单独的页面创建导航:

<?php

if(mysql_num_rows($rs_result)!=0){

?>

<br /><div style="margin-left:200px;width:300px;text-align:center;background-color:">Pagina's:<br clear="left"> 


<?php 
$sql = "SELECT COUNT(bedrijfsnaam) FROM profiles where hoofdrubriek = '".$rubriekpage2."'"; 
$rs_result = mysql_query($sql); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 
$total_pages = ceil($total_records / 10); 

if ($total_pages>10) $total_pages=10;
for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a style=\"float:none;display:inline-block;color:white;width:22px;text-align:center;text-decoration:none;font-size:20px;background-color:#483435;margin-left:4px\" href='".$plaatsnaam7.".php?page=".$i."'>".$i."</a> "; 

}; 
?>

到目前为止一切都很好,没有问题。但是:我仅限于 10 页,仅此而已。当然,我可以将数字更改为 25,但我真正想要的是“数字”导航旁边的上一个和下一个按钮。如果超过 10 页,则在右侧放置一个下一步按钮,将显示接下来的 10 个数字,依此类推...

我整个晚上都在谷歌上搜索,但没有找到一个像样的结果、教程或类似的东西。有没有人愿意帮我一把?谢谢!

【问题讨论】:

  • 因此,为了弄清楚这一点,您是在询问如何在 google 中搜索“php mysql pagination tutorial”,还是询问如何阅读该搜索返回的大量优秀教程?
  • @Captain Payalytic Well Captain Sarcastic,该搜索实际上找到了一些不错的教程,谢谢,我的搜索显然不够“切中要害”。
  • 显然你需要前者,帮助如何使用谷歌。

标签: php sql navigation


【解决方案1】:

检查当前页是否为非第一页,如果后10页有效,则显示上一页、下一页、页面链接。

 function page_link($i) {
      echo "<a style=\"float:none;display:inline-block;color:white;width:22px;text-align:center;text-decoration:none;font-size:20px;background-color:#483435;margin-left:4px\" href='".$plaatsnaam7.".php?page=".$i."'>".$i."</a> "; 
  }

  function page_next($i){
       // link to page $i with arrow right
  }
  function page_prev($i){
      // link to page $i with arrow left
  }
 $s = $_GET['page'];
 if ($s > 1) page_prev($s - 1);
 for  ($i = $s; $i * 10 < $total_count && $i < $s + 10; $i++) { 
      page_link($i);
 }
 if (10 + $i * 10 < $total_count) page_next($i+1);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-20
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多