A php paging code (compatible with url routing)
- Class Page {
- Private $ total; // The total number of records in the data table
- Private $ listRows; // Number of Entries displayed on each page
- Private $ limit; // use the limit clause in SQL statements to limit the number of records to be retrieved
- Private $ uri; // automatically obtain the url request address
- Private $ pageNum; // The total number of pages.
- Private $ page; // current page
- Private $ config = array (
- 'Head' => 'records ',
- 'Prev' => 'previous page ',
- 'Next' => 'next page ',
- 'First' => 'homepage ',
- 'Last' => 'Last page'
- ); // Display the content in the page information. you can set it by using the set () method.
- Private $ listNum = 5; // Default number of display pages
-
- /**
- * Constructor: you can set the attributes of the paging class.
- * @ Param int $ total calculate the total number of records on the page
- * @ Param int $ listRows (optional) sets the number of records displayed on each page. the default value is 25.
- * @ Param mixed $ query (optional) it is an array or string format that passes parameters to the target page.
- * @ Param bool $ ord: The default value is true. the page is displayed from the first page, and false is displayed from the last page.
- */Bbs.it-home.org
- Public function _ construct ($ total, $ listRows = 25, $ query = '', $ ord = true ){
- $ This-> total = $ total;
- $ This-> listRows = $ listRows;
- $ This-> uri = $ this-> getUri ($ query );
- $ This-> pageNum = ceil ($ this-> total/$ this-> listRows );
-
- /* The following judgment is used to set the current page */
- If (! Empty ($ _ GET ['Page']) {
- $ Page = $ _ GET ['Page'];
- } Else {
- If ($ ord ){
- $ Page = 1;
- } Else {
- $ Page = $ this-> pageNum;
- }
- }
-
- If ($ total> 0 ){
- If (preg_match ('/\ D/', $ page )){
- $ This-> page = 1;
- } Else {
- $ This-> page = $ page;
- }
- }
-
- // $ This-> limit = 'limit'. $ this-> setLimit ();
- $ This-> limit = $ this-> setLimit ();
- }
-
- /**
- * Sets the display page information for consistent operations.
- * @ Param string $ the following table lists the attributes of the param member array config.
- * @ Param string $ value is used to set the element value corresponding to the config subscript.
- * @ Return object returns the current object $ this for consistent operations.
- */
- Function _ set ($ param, $ value ){
- If (array_key_exists ($ param, $ this-> config )){
- $ This-> config [$ param] = $ value;
- }
- // Return $ this;
- }
-
- /* You can directly obtain the limit and page values of private member attributes outside the object */
- Function _ get ($ args ){
- If ($ args = 'limit' | $ args = 'page '){
- Return $ this-> $ args;
- } Else {
- Return null;
- }
- }
-
- /**
- * Output pagination in the specified format
- * @ Param int 0-7 numbers are used as parameters to customize the output pagination structure and adjust the structure order. by default, all structures are output.
- * @ Return string paging information content
- */
- Function fpage (){
- $ Arr = func_get_args ();
-
- $ Html [0] = "total{$ This-> total}{$ This-> config ['head']} ";
- $ Html [1] = "this page". $ This-> disnum ()."";
- $ Html [2] = "this page is from{$ This-> start ()}-{$ this-> end ()}";
- $ Html [3] ="{$ This-> page}/{$ this-> pageNum}Page ";
- $ Html [4] = $ this-> firstprev ();
- $ Html [5] = $ this-> pageList ();
- $ Html [6] = $ this-> nextlast ();
- $ Html [7] = $ this-> goPage ();
-
- $ Fpage ='
';
-
- If (count ($ arr) <1 ){
- $ Arr = array (0, 1, 2, 3, 4, 5, 6, 7 );
- }
-
- For ($ I = 0; $ I $ Fpage. = $ html [$ arr [$ I];
- }
-
- $ Fpage. ='';
- Return $ fpage;
- }
-
- /* Used inside the object to automatically obtain the current url for access */
- Private function getUri ($ query ){
- $ Request_uri = $ _ SERVER ['request _ URI '];
- $ Url = strstr ($ request_uri ,'? ')? $ Request_uri: $ request_uri .'? ';
-
- If (is_array ($ query )){
- $ Url. = http_build_query ($ query );
- } Else if ($ query! = ''){
- $ Url. = '&'. trim ($ query ,'? &');
- }
-
- $ Arr = parse_url ($ url );
-
- If (isset ($ arr ['query']) {
- Parse_str ($ arr ['query'], $ arrs );
- Unset ($ arrs ['Page']);
- $ Url = $ arr ['path']. '? '. Http_build_query ($ arrs );
- }
-
- If (strstr ($ url ,'? ')){
- If (substr ($ url,-1 )! = '? '){
- $ Url = $ url .'&';
- }
- } Else {
- $ Url. = '? ';
- }
- Return $ url;
- }
-
- /* Private method, set limit */
- Private function setLimit (){
- If ($ this-> page> 0 ){
- Return ($ this-> page-1) * $ this-> listRows. ", {$ this-> listRows }";
- } Else {
- Return 0;
- }
- }
-
- /* Private method used inside the object to obtain the number of records starting from the current page */
- Private function start (){
- If ($ this-> total = 0 ){
- Return 0;
- } Else {
- Return ($ this-> page-1) * $ this-> listRows + 1;
- }
- }
-
- /* Used to obtain the number of records at the end of the current page */
- Private function end (){
- Return min ($ this-> page * $ this-> listRows, $ this-> total );
- }
-
- /* Used to obtain the number of records displayed on this page */
- Private function disnum (){
- If ($ this-> total> 0 ){
- Return $ this-> end ()-$ this-> start () + 1;
- } Else {
- Return 0;
- }
- }
-
- /* Used to obtain operation information of the previous page and homepage */
- Private function firstprev (){
- If ($ this-> page> 1 ){
- $ Str = "uri} page = 1' >{$ this-> config ['first']}";
- $ Str. = "uri} page =". ($ this-> page-1). "'>{$ this-> config ['prev']}";
- Return $ str;
- }
- }
-
- Private function pageList (){
- $ LinkPage ='';
- $ PageSub = $ this-> page % $ this-> listNum;
- If ($ pageSub = 0 & $ this-> page> 0 ){
- $ PageSub = $ this-> listNum;
- }
- /* List at the front of the current page */
- For ($ I = $ pageSub-1; $ I >=1; $ I --){
- $ Page = $ this-> page-$ I;
- If ($ page> = 1 ){
- $ LinkPage. = "uri} page = {$ page} '> {$ page }";
- }
- }
-
- /* Information on the current page */
- If ($ this-> pageNum> 1 ){
- $ LinkPage. = "{$ this-> page }";
- }
-
- /* List after the current page */
- For ($ I = 1; $ I <= $ this-> listNum-$ pageSub; $ I ++ ){
- $ Page = $ this-> page + $ I;
- If ($ page <= $ this-> pageNum ){
- $ LinkPage. = "uri} page = {$ page} '> {$ page }";
- } Else {
- Break;
- }
- }
-
- $ LinkPage. ='';
- Return $ linkPage;
- }
-
- /* Used to obtain page number list information */
- Private function pageListBak (){
- $ LinkPage ='';
-
- $ Inum = floor ($ this-> listNum/2 );
- /* List at the front of the current page */
- For ($ I = $ inum; $ I >=1; $ I --){
- $ Page = $ this-> page-$ I;
- If ($ page> = 1 ){
- $ LinkPage. = "uri} page = {$ page} '> {$ page }";
- }
- }
-
- /* Information on the current page */
- If ($ this-> pageNum> 1 ){
- $ LinkPage. = "{$ this-> page }";
- }
-
- /* List after the current page */
- For ($ I = 1; $ I <= $ inum; $ I ++ ){
- $ Page = $ this-> page + $ I;
- If ($ page <= $ this-> pageNum ){
- $ LinkPage. = "uri} page = {$ page} '> {$ page }";
- } Else {
- Break;
- }
- }
-
- $ LinkPage. ='';
- Return $ linkPage;
- }
-
- /* Obtain the operation information of the next and last pages */
- Private function nextlast (){
- If ($ this-> page! = $ This-> pageNum ){
- $ Str = "uri} page =". ($ this-> page + 1). "'>{$ this-> config ['Next']}";
- $ Str. = "uri} page = {$ this-> pageNum} '> {$ this-> config ['last']}";
- Return $ str;
- }
- }
-
- /* Display and process the form jump page */
- Private function goPage (){
- If ($ this-> pageNum> 1 ){
- Return'';
- }
- }
- }
|