簡體   English   中英

分頁號?

[英]Pagination with Page numbers?

我有一個頁面,該頁面是從Mysql數據庫動態填充的。

為了防止一次顯示大量信息,我在頁面上添加了分頁。

使用此代碼。

$currentPage = $_SERVER["PHP_SELF"];
$maxRows_atoz = 10; $page= 0;
if (isset($_GET['page'])) {$page= $_GET['page'];}
$startRow_atoz = $page* $maxRows_atoz;

mysql_select_db($database_main, $main);
$query_atoz = "SELECT * FROM main WHERE title LIKE 'A%'"; 
$query_limit_atoz = sprintf("%s LIMIT %d, %d", $query_atoz, $startRow_atoz, $maxRows_atoz);
$atoz = mysql_query($query_limit_atoz, $main) or die(mysql_error());
$row_atoz = mysql_fetch_assoc($atoz);

if (isset($_GET['totalRows_atoz'])) {$totalRows_atoz = $_GET['totalRows_atoz'];} else {$all_atoz = mysql_query($query_atoz); $totalRows_atoz = mysql_num_rows($all_atoz);}
$totalPages_atoz = ceil($totalRows_atoz/$maxRows_atoz)-1;
$queryString_atoz = ""; if (!empty($_SERVER['QUERY_STRING'])) {$params = explode("&", $_SERVER['QUERY_STRING']); $newParams = array(); foreach ($params as $param) {
if (stristr($param, "pageNum_atoz") == false && stristr($param, "totalRows_atoz") == false) {array_push($newParams, $param);}}
if (count($newParams) != 0) {$queryString_atoz = "&" . htmlentities(implode("&", $newParams));}}    
$queryString_atoz = sprintf("&totalRows_atoz=%d%s", $totalRows_atoz, $queryString_atoz);

?>

<ul>
<li class="previous"> <?php if ($page > 0) { // Show if not first page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, max(0, $page - 1), $queryString_atoz); ?>">
<img src="/files/previous.png" /></a>
<?php } // Show if not first page ?></li>
<li class="next">  <?php if ($page < $totalPages_atoz) { // Show if not last page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, min($totalPages_atoz, $page + 1), $queryString_atoz); ?>">
<img src="/files/next.png" /></a>
<?php } // Show if not last page ?></li>
</ul>

該代碼可用於分頁結果,但有局限性。 當前僅顯示“下一個”和“上一個”按鈕。

我想添加頁碼。 這樣,用戶可以跳至結果集中的第4頁或第5頁,而不必按下下一個按鈕4或5次。

所以,有些人可以告訴我,我將如何做到這一點?

謝謝

我必須說,我沒有完成你的整個代碼。 您必須縮進代碼以幫助其他人。 如果我的問題是對的,以下內容可能會對您有所幫助。 我沒有太多關注細節。 這只是為了給你一個想法。 :-)

<ul>
<li class="previous">
<?php if ($page > 0) { // Show if not first page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, max(0, $page - 1),$queryString_atoz); ?>">
<img src="/files/previous.png" /></a>
<?php } // Show if not first page ?></li>
<!--MODIFY_BEGIN-->
<li>
<?php for ($i=0;$i<$page; $i++){
    if ($currentPage==$i)
        echo ($currentPage+1);
    else
        echo "<a title='Page $i' href='?page=$i'>Page $i</a>";
} ?>

</li>
<!--MODIFY_END-->
<li class="next">  <?php if ($page < $totalPages_atoz) { // Show if not last page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, min($totalPages_atoz, $page + 1), $queryString_atoz); ?>">
<img src="/files/next.png" /></a>
<?php } // Show if not last page ?></li>
</ul>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM