Questions about PHP paging display

Source: Internet
Author: User
For PHP paging display problems, I will introduce the paging principle in the PHP100 video. According to the provided code, after running, it is found that the page change cannot be performed, and the url is still displayed? Page = 2 page = 3 page = 4. the reason is that the ereg_replace function is no longer used in version 5.3. if it is changed to preg_replace, the problem cannot be solved. Thank you.

Code:

Function _ PAGEFT ($ totle, $ displaypg = 20, $ url = ''){
Global $ page, $ firstcount, $ pagenav, $ _ SERVER;
$ GLOBALS ["displaypg"] = $ displaypg;
If (! $ Page)
$ Page = 1;
If (! $ Url ){
$ Url = $ _ SERVER ["REQUEST_URI"];
}
// URL analysis:
$ Parse_url = parse_url ($ url );
$ Url_query = $ parse_url ["query"]; // Retrieve the query string of the URL separately.
If ($ url_query ){
$ Url_query = preg_replace ("/(^ | &) page = $ page/", "", $ url_query );

$ Url = str_replace ($ parse_url ["query"], $ url_query, $ url );
If ($ url_query)
$ Url. = "& page ";
Else
$ Url. = "page ";
} Else {
$ Url. = "? Page ";
}
$ Lastpg = ceil ($ totle/$ displaypg); // The last page, also the total number of pages
$ Page = min ($ lastpg, $ page );
$ Prepg = $ page-1; // Previous page
$ Nextpg = ($ page = $ lastpg? 0: $ page + 1); // Next page
$ Firstcount = ($ page-1) * $ displaypg;
// Start the paging navigation bar code:
$ Pagenav = "display ". ($ Totle? ($ Firstcount + 1): 0 )."- ". Min ($ firstcount + $ displaypg, $ totle )."A total of $ totle records ";
// If there is only one page, the function will jump out:
If ($ lastpg <= 1)
Return false;
$ Pagenav. = "homepage ";
If ($ prepg)
$ Pagenav. = "front page ";
Else
$ Pagenav. = "front page ";
If ($ nextpg)
$ Pagenav. = "";
Else
$ Pagenav. = "";
$ Pagenav. = "Last page ";
// Pull-down Jump List, listing all page numbers cyclically:
$ Pagenav. =" \ N ";For ($ I = 1; $ I <= $ lastpg; $ I ++ ){If ($ I = $ page)$ Pagenav. ="$ I\ N ";Else$ Pagenav. ="$ I\ N ";}$ Pagenav. ="Page, total $ lastpg page ";
}
Include ("conn. php ");
$ Result = mysql_query ("SELECT * FROM 'p _ newsbase "');
$ Total = mysql_num_rows ($ result );
// Call pageft () to display 10 messages per page (this parameter can be omitted when the default value is 20). use the URL on this page (which is omitted by default ).
_ PAGEFT ($ total, 1 );
Echo $ pagenav;
$ Result = mysql_query ("SELECT * FROM 'p _ newsbase 'limit $ firstcount, $ displaypg ");
While ($ row = mysql_fetch_array ($ result )){
Echo" ". $ Row ['author']." | ". $ row ['title'];
}

?


Reply to discussion (solution)

You have to watch more php100 videos.

1. add return $ pagenav to the last line of function _ PAGEFT;
2. write the code like this during the call.

$ Page = isset ($ _ GET ['Page'])? $ _ GET ['Page']: 1; echo _ PAGEFT (100); // The total number of records you plan to process

You can also perform tests without databases.

Thanks to the moderator, the problem has been solved, but I still cannot study it myself. thanks to the moderator, I feel the concern of the organization. thank you! More questions will be asked in the future!

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.