我最近自己做了一個paginator,但以一種完全不同的方式接近它。我使用PHP來生成數字,每個數字(頁面)都有一個標籤,其href是mywebsite.php?page = x。這樣,您可以使用get方法,從url中獲取頁碼,並根據需要循環顯示頁數。
正如他們所說,有不止一種方法去皮膚貓。我更喜歡使用url傳遞方法,因爲我可以遠離表單和ID的全部內容,這樣一來,分頁器就可以到達我決定拍攝(無論多次)的位置。
下面是我如何生成它的快照。希望它給你一些想法!
/*PAGE NUMBERS*/
// ceil rounds a decimal up to the next integer
$pages=ceil(($totalrows-1)/$tablesize); //we subtract 1 from total rows because it counts 0
//(int) typecasts the $pages variable, so that it is divisible by an integer (ceil makes it a float)
$pages=(int)$pages;
//displays all the pages with their links
//if page count is less than 7 (the full paginator), display all pages
if($pages<=7){
for($i=1;$i<=$pages;$i++){
print "<a class='pages";
//add class current_page if necessary
if($page==$i){print " current_page";}
print "' href='index.php?page=";
print $i. "'>"." $i</a> "." "." ";
}
//if page count is more than 7
}else{
//if page # is less than 4, display pages up to 7, so that there are always 7 pages available (makes the buttons not jump around)
if($page<=4){
for($i=1;$i<=7;$i++){
print "<a class='pages";
//add class current_page if necessary
if($page==$i){print " current_page";}
print "' href='index.php?page=";
print $i. "'>"." $i</a> "." "." ";
}
//if page # is less than 4 away from the end, display pages $pages-7
}elseif($page>=$pages-3){
for($i=$pages-6;$i<=$pages;$i++){
//8,9,10,11,12,13,14,15
print "<a class='pages";
//add class current_page if necessary
if($page==$i){print " current_page";}
print "' href='index.php?page=";
print $i. "'>"." $i</a> "." "." ";
}
//if it's in between the ends, do this
}else{
for($i=1;$i<$pages+1;$i++){
//limit the number of visible pages to 7
if(($i>=$page-3)&&($i<=$page+3)){
print "<a class='pages";
//add class current_page if necessary
if($page==$i){print " current_page";}
print "' href='index.php?page=";
print $i. "'>"." $i</a> "." "." ";
}
}
}
}
當單擊該按鈕並將按鈕的表單作爲GET請求提交時,我是不是需要/需要使用該元素的ID來根據需要從元素中獲取值(來自存儲其他查詢字符串值的隱藏輸入,例如)? – 2011-06-10 15:47:54