Php paging code _ PHP Tutorial

Source: Internet
Author: User
Php paging code. [Php] * idea 1. get the URL in the address bar 2. analyze the query section in the URL-that is? 3. analyze the query part into an array 4. Combine the page unit in the array with + 1 and-1 to form two [php]

/*

Ideas

1. get the URL in the address bar

2. analyze the query part in the URL -- that is? Parameters

3. analyze the query part into an array

4. add the page unit in the array, + 1,-1, to form two new arrays.

5. splice the new array into the query part and combine the previous page and Next Page connection address.

*/

// Paging class

Class Page {

Public $ total; // The total number of items, which are retrieved from the database.

Public $ prePage = 10; // number of entries per page

Protected $ curr = 1; // Default current page number

Public function _ construct ($ total, $ prePage = ''){

$ This-> total = $ total; // place the total entry information in the total attribute.

If ($ prePage> 0 ){

$ This-> prePage = $ prePage; // set the page size to the perPage attribute.

}

// Calculate the current page number

If (isset ($ _ GET ['Page']) & ($ _ GET ['Page'] + 0)> 0 ){

$ This-> curr =$ _ GET ['Page'] + 0;

}

}

// Subject function

Public function showPage (){

If ($ this-> total <= 0 ){

Return ''; // if the total number of entries <= 0, an empty string is directly returned.

}

$ Cnt = ceil ($ this-> total/$ this-> prePage); // calculates the total number of pages and returns an integer.

// Calculate the previous page and next page based on the current page

/*

What are the situations for url analysis?

Xx. php

Xx. php? Id = 5

Xx. php? Page = 3

Xx. php? Id = 5 & page = 3

*/

// The final generated URL must contain page = N

$ Url = $ _ SERVER ['request _ URI '];

$ Parse = parse_url ($ url); // put the URL analysis result in the array

// Print_r ($ parse );

// Ensure that the parameter contains page

If (! Isset ($ parse ['query']) {

$ Parse ['query'] = 'page = '. $ this-> curr;

}

// Analyze the query string into an array and make sure that the page option is available again.

Parse_str ($ parse ['query'], $ parms );

If (! Array_key_exists ('page', $ parms )){

$ Parms ['Page'] = $ this-> curr;

}

// Test all the above four cases, and the page parameter can be generated.

// Print_r ($ parms );

// Determine whether there are other parameters besides page

If (count ($ parms) = 1 ){

$ Url = $ parse ['path']. '? ';

} Else {

Unset ($ parms ['Page']);

$ Url = $ parse ['path']. '? '. Http_build_query ($ parms ).'&';

}

// Echo $ url

$ Prev = $ this-> curr-1;

$ Next = $ this-> curr + 1;

// Homepage

$ IndexLink = 'homepage ';

// Previous Page

If ($ prev <1 ){

$ PrevLink = '';

} Else {

$ PrevLink = 'preput ';

}

// Next page

If ($ next> $ cnt ){

$ NextLink = '';

} Else {

$ NextLink = 'next page ';

}

// Last Page

$ LastLink = 'end page ';

// Echo $ indexLink. ''. $ prevLink.''. $ nextLink. ''. $ lastLink;

// Previous page, 1 2 3 4 5 Next Page

$ Start = $ this-> curr-(5-1)/2; // calculate the start page number on the left.

$ End = $ this-> curr + (5-1)/2; // calculate the start page number on the right.

// If the page on the left is already smaller than 1, add the part smaller than 1 to the right.

If ($ start <1 ){

$ End + = (1-$ start );

$ Start = 1; // modify start = 1

If ($ end> $ cnt ){

$ End = $ cnt;

}

}

// Add the excess part on the right to the left

If ($ end> $ cnt ){

$ Start-= ($ end-$ cnt );

$ End = $ cnt;

If ($ start <1 ){

$ Start = 1;

}

}

// Number of outgoing page numbers

$ PageStr = '';

For ($ I = $ start; $ I <= $ end; $ I ++ ){

If ($ I ==$ this-> curr ){

$ PageStr. = $ I;

Continue;

}

$ PageStr. = ''. $ I .'';

}

Return $ indexLink. $ prevLink. $ pageStr. $ nextLink. $ lastLink;

}

}

$ Page = new Page (30,3 );

Echo $ page-> showPage ();

Idea 1. get the URL in the address bar 2. analyze the query section in the URL-that is? 3. analyze the query part into an array 4. Combine the page unit in the array with + 1 and-1 to form two...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.