分頁類 PageModel的手法

2021-08-30 19:57:20 字數 4382 閱讀 5072

* 封裝分頁邏輯

* @author administrator

public class pagemodel{

//結果集

private listlist;

//記錄數

private int totalrecords;

//每頁多少條資料

private int pagesize;

//第幾頁

private int pageno;

* 返回總頁數

* @return

public int gettotalpages() {

return (totalrecords + pagesize - 1) / pagesize;

* 首頁

* @return

public int gettoppageno() {

return 1;

* @return

public int getpreviouspageno() {

if (this.pageno <= 1) {

return 1;

return this.pageno - 1;

* @return

public int getnextpageno() {

if (this.pageno >= getbuttompageno()) {

return getbuttompageno();

return this.pageno + 1;

* 尾頁

* @return

public int getbuttompageno() {

return gettotalpages();

public listgetlist() {

return list;

public void setlist(listlist) {

this.list = list;

public int gettotalrecords() {

return totalrecords;

public void settotalrecords(int totalrecords) {

this.totalrecords = totalrecords;

public int getpagesize() {

return pagesize;

public void setpagesize(int pagesize) {

this.pagesize = pagesize;

public int getpageno() {

return pageno;

public void setpageno(int pageno) {

this.pageno = pageno;

另一種寫法:

public class pageutils {

* 預設第一頁頁碼

public static final int index_page_num = 1;

public final static int page_size = 15; // 預設分頁大小

* 計算頁數

public static int pagecount(int recordcount, int perpage) {

int pc = (int) math.ceil(recordcount / (double) perpage);

if (pc == 0)

pc = 1;

return pc;

* 計算查詢時,資料當前位置(fromindex)

public static int fromidx(int page, int fetchcount) {

return (page - index_page_num) * fetchcount;

* 分頁條

* @param currentpage

*            當前頁數

* @param fetchcount 

*            每頁顯示數

* @param recordcount

*            資料總數

* @param pagepath

*            頁面路徑

* @return

public static string getpagestr(int currentpage, int fetchcount,

int recordcount, string filename,string suffix) {

// string filename = pagepath.split("\\.")[0];

// string suffix = "." + pagepath.split("\\.")[1];

stringbuffer result = new stringbuffer();

string blankstr = " ";

int maxlength = 4;

int pagecount = pagecount(recordcount, fetchcount);

if (pagecount > index_page_num) {

if (currentpage > index_page_num) {

if (pagecount > index_page_num) {

for (int cou = ((currentpage - maxlength) > 0 ? maxlength

: currentpage - 1); cou >= 1; cou--) {

for (int cou = 1; cou <= ((currentpage + maxlength) > pagecount ? pagecount

- currentpage

: maxlength); cou++) {

+ (currentpage + cou) + "");

if (pagecount > index_page_num) {

if (currentpage < pagecount) {

return result.tostring();

* 動態分頁條(自動帶"page=?"的引數)

* @param currentpage

*            當前頁數

* @param fetchcount

*            每頁顯示數

* @param recordcount

*            資料總數

* @param pagepath

*            頁面路徑

* @param reqparam

*            頁面引數

* @return

public static string getparampagestr(int currentpage, int fetchcount,

int recordcount, string pagepath, string reqparam) {

string filename = pagepath.split("\\.")[0];

string suffix = "." + pagepath.split("\\.")[1];

stringbuffer result = new stringbuffer();

string blankstr = " ";

int maxlength = 4;

int pagecount = pagecount(recordcount, fetchcount);

if (pagecount > index_page_num) {

if (currentpage > index_page_num) {

if (pagecount > index_page_num) {

for (int cou = ((currentpage - maxlength) > 0 ? maxlength

: currentpage - 1); cou >= 1; cou--) {

for (int cou = 1; cou <= ((currentpage + maxlength) > pagecount ? pagecount

- currentpage

: maxlength); cou++) {

if (pagecount > index_page_num) {

if (currentpage < pagecount) {

return result.tostring();

public static void main(string args){

system.out.println(getparampagestr(2,1,3,"555.shtml",".shtml"));

才子分頁類

取文章總數及每頁重複顯示條數,準備分頁 wzcount 文章總數 wzrep 重複顯示條數 wzpage 分頁引數id wzpagecount 總頁數 dim wzcount,wzrep,wzpage,wzpagecount,wzpagerep,boardstr wzrep 30 rssql sel...

簡易分頁類

這是乙個簡單易用的分頁類。只需在你原有的程式中加兩句 改一句就可以了 先貼 paging.php php code phpclass paging static function bar tpl echo tpl 通常你都有類似這樣的語句 sql rs mysql query sql 或 rs my...

簡易分頁類

phpclass paging static function bar tpl echo tpl 通常你都有類似這樣的語句 sql rs mysql query sql 或 rs mysql query select 你只需改作 include paging.php rs paging prepar...