Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write a simple and easy to use php paging class

Shulou Source: shulou.com Published: 2022-06-03 07:38:56 10月02日 Update

This article mainly introduces "how to write a simple and easy-to-use php paging class". In daily operation, I believe many people have doubts about how to write a simple and easy-to-use php paging class. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to write a simple and easy-to-use php paging class". Next, please follow the editor to study!

The copy code is as follows:

Class Page {

/ / the number of pages displayed per page in the paging column

Public $rollPage = 6

/ / parameters to be taken when the number of pages jumps

Public $parameter

/ / the default list shows the number of rows per page

Public $listRows = 20

/ / number of starting lines

Public $firstRow

/ / Total number of pages paged

Protected $totalPages

/ / Total number of rows

Protected $totalRows

/ / current number of pages

Protected $nowPage

/ / the total number of pages in paged columns

Protected $coolPages

/ / customization of paging display

Protected $config = array (

'redirect'= > false

'header'= >' record'

'prev'= > 'previous page'

'next'= >' next page'

'first'= >'1'

'last'= >' Last Page'

'theme'= >' upPage% first% prePage% linkPage% nextPage% downPage% of all totalPage% pages')

/ / default paging variable name

Protected $varPage

/ * *

+

* Architecture function

+

* @ access public

+

* @ param array $totalRows total number of records

* @ param array $listRows displays the number of records per page

* @ param array $parameter paging jump parameters

+

, /

Public function _ _ construct ($totalRows,$listRows='',$parameter='') {

$this- > totalRows = $totalRows

$this- > parameter = $parameter

$this- > varPage = C ('VAR_PAGE')? C ('VAR_PAGE'):' p'

If (! empty ($listRows)) {

$this- > listRows = intval ($listRows)

}

$this- > totalPages = ceil ($this- > totalRows/$this- > listRows); / / Total number of pages

$this- > coolPages = ceil ($this- > totalPages/$this- > rollPage)

/ / $_ GET verification

$this- > nowPage = intval ($_ GET [$this- > varPage])

$this- > nowPage = $this- > nowPage > 0? $this- > nowPage: 1

If (! empty ($this- > totalPages) & & $this- > nowPage > $this- > totalPages) {

$this- > nowPage = $this- > totalPages

}

$this- > firstRow = $this- > listRows* ($this- > nowPage-1)

}

Public function setConfig ($name,$value) {

If (isset ($this- > config [$name])) {

$this- > config [$name] = $value

}

}

/ * *

+

* display output in paging

+

* @ access public

+

, /

Public function show () {

If (0 = $this- > totalRows) return''

/ / processing parameters

$p = $this- > varPage

$url = $_ SERVER ['REQUEST_URI']. (strpos ($_ SERVER [' REQUEST_URI'],'?'): "?"). $this- > parameter

$parse = parse_url ($url)

If (isset ($parse ['query'])) {

Parse_str ($parse ['query'], $params)

Unset ($params [$p])

$url = $parse ['path'].'?. Http_build_query ($params)

}

/ * paging logic * /

/ / when the total is less than the number of pages displayed

If ($this- > totalPages rollPage) {

$start = 1

$end = $this- > totalPages

}

Else {

/ /

If ($this- > nowPage rollPage-1) {

$start = 1

$end = $this- > rollPage

$islast = true

}

Else if ($this- > nowPage > $this- > totalPages-$this- > rollPage + 1) {

$start = $this- > totalPages-($this- > rollPage-1)

$end = $this- > totalPages

$isfirst = true

}

Else {

/ / floating number

$size = floor ($this- > rollPage / 2)

$start = $this- > nowPage-$size

$end = $this- > nowPage + $size

$isfirst = true

$islast = true

}

}

/ / turn the page string up and down

$upRow = $this- > nowPage-1

$downRow = $this- > nowPage + 1

/ * assemble HTML * /

/ /

< 1... ...last >

If ($isfirst) {

$theFirst = ". $this- > config ['first']."

}

If ($islast) {

$theEnd = ". $this- > config ['last']."

}

If ($upRow > 0) {

$upPage = ". $this- > config ['prev']."

}

If ($downRow totalPages) {

$downPage = ". $this- > config ['next']."

}

If ($start==3) {

$linkPage. = "2"

}

If ($start > = 4) {

$linkPage. = "2..."

}

/ / 1 2 3 4 5

For ($itemized startplace inowPage) {

$linkPage. = ". $I."

} else {

$linkPage. = ". $I."

}

If ($i==$end) {

If ($itotalRows) {

$linkPage. = "..."

}

}

}

$pageStr = str_replace (

Array ('% header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%first%','%prePage%','%linkPage%','%nextPage%','%downPage%','%end%')

Array ($this- > config ['header'], $this- > nowPage,$this- > totalRows,$this- > totalPages,$upPage,$theFirst,$prePage,$linkPage,$nextPage,$downPage,$theEnd), $this- > config [' theme'])

/ / display mode ordinary false with jump ture

If (! empty ($this- > config ['redirect'])) {

$html = $pageStr

} else {

/ / pass parameters

If ($this- > totalPages > 1) {

$redirect = "to page"

If ($params) {

Foreach ($params as $k = > $v) {

$string. = "

}

$redirect = $redirect. $string. ''

} else {

$redirect = $redirect. ''

}

}

/ / generate Html string

$html = $pageStr. $redirect

}

Return $html

}

}

At this point, the study on "how to write a simple and easy-to-use php paging class" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Easy to use number of pages parameters learning code characters strings more help practical ordinary next up and down functions variables total total lines articles methods architecture Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information vpn NVidia Huawei