2013-10-15 15 views
2

我有一个分页问题,​​如何显示只有10个项目?在谷歌可以做这样的事情 - 数字增加了+5?分页php只有10个项目

enter image description here

<?php 
// Make the links to other pages, if necessary. 
if ($pages > 1) { 

    echo '<br /><p>'; 
    $current_page = ($start/$display) + 1; 

    // If it's not the first page, make a Previous button: 
    if ($current_page != 1) { 
     echo '<a href="list_photos_4.php?s=' . ($start - $display) . '&p=' . $pages . '&sort=' . $sort . '">Previous</a> '; 
    } 

    // Make all the numbered pages: 
    for ($i = 1; $i <= $pages; $i++) { 
     if ($i != $current_page) { 
      echo '<a href="list_photos_4.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&sort=' . $sort . '">' . $i . '</a> '; 
     } else { 
      echo $i . ' '; 
     } 
    } // End of FOR loop. 

    // If it's not the last page, make a Next button: 
    if ($current_page != $pages) { 
     echo '<a href="list_photos_4.php?s=' . ($start + $display) . '&p=' . $pages . '&sort=' . $sort . '">Next</a>'; 
    } 

    echo '</p>'; // Close the paragraph. 

} // End of links section. 
?> 

回答

2

试图改变部分(让所有的偶数页面),以这样的:

// Make all the numbered pages: 
    for ($i = 1; $i <= $pages; $i++) {  
     if ($i != $current_page) { 
      $distance = $current_page - $i; 
      if (abs($distance) < 5){ 
       echo '<a href="list_photos_4.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&sort=' . $sort . '">' . $i . '</a> '; 
      } 
     } else { 
      echo $i . ' '; 
     } 
    } // End of FOR loop 

我希望这项工作适合您。

+0

你好,谢谢你的回复我现在有类似的内容SCREAM:错误抑制忽略注意:Undefined variable:i in C :\ wamp \ www \ dwwithphp \ public \ admin \ list_photos_4.php on line 186 - line 186“if($ i!= $ current_page){” –

+0

@ Mantykora7刚把我的解答放到for循环中:for($ i = 1 ; $ i <= $ pages; $ i ++){// MY ANSWER HERE} 我刚编辑我的答案。应该工作 – Adam

+0

是的,你是对的,粘贴错误的东西,效果很好,谢谢你的时间和回答:) –

0

更改此:

// Make all the numbered pages: 
for ($i = 1; $i <= $pages; $i++) { 

这样:

if ($current_page <= 5) $first_page = 1; 
else if ($pages - $current_page < 10) $first_page = $pages - 10; 
else $first_page = $current_page - 5; 

// Make up to 10 numbered pages: 
for ($i = $first_page; $i < ($first_page + 10); $i++) { 
+0

Hello - 在$ display = 2时正常工作; - 项目25,当$显示= 10项目10它显示了这样的事情上一个-5 -4 -3 -2 -1 0 1 2 3 4下一个 –

+0

我不确定$显示的目的是什么。也就是说,'$ current_page =($ start/$ display)+ 1;'看起来很奇怪...... $ current_page可能只是'(int)$ _GET ['s']',虽然老实说我很难一次了解你的链接结构。您只应传递排序顺序和页码,页数应该由服务器决定,而不是由浏览器发送。你应该清理你的整体代码/逻辑来弄清细节。 –

+0

谢谢你的时间和答案 - 已经解决了这个问题 –

0
<?php 
// Make the links to other pages, if necessary. 
if ($pages > 1) { 

    // (...) 

    $first_disp_page = $cutrrent_page - 5 < 1 ? 1 : $current_page - 5; 
    $last_disp_page = $current_page + 5 > $pages ? $pages : $current_page + 5; 

    // Make all the numbered pages: 
    for ($i = $first_disp_page; $i <= $last_disp_page; $i++) { 
     if ($i != $current_page) { 
      echo '<a href="list_photos_4.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&sort=' . $sort . '">' . $i . '</a> '; 
     } else { 
      echo $i . ' '; 
     } 
    } // End of FOR loop. 

    // (...) 

} // End of links section. 
?> 
+0

谢谢你的时间和回答:) –

相关问题