Mysql paging Query

Source: Internet
Author: User
Tags mysql tutorial

Select * from table limit 0, 10


// If the input page number is greater than the total page number, the error message is displayed.

If ($ page> $ pagenum | $ page = 0 ){
Echo "error: can not found the page .";
Exit;
}

$ Offset = ($ page-1) * $ num; // obtain the value of the first parameter of limit. If the first page is (1-1) * 10 = 0, the second page is (2-1) * 10 = 10.

$ Info = mysql tutorial _ query ("select name from table limit $ offset, $ num"); // obtain the data to be displayed for the corresponding page number. name is a field in the data.
While ($ it = mysql_fetch_array ($ info )){
Echo $ it ['name']. "<br/> ";
} // Display data

For ($ I = 1; $ I <= $ pagenum; $ I ++ ){

$ Show = ($ I! = $ Page )? "<A href = 'index. php tutorial? Page = ". $ I." '> $ I </a> ":" <B> $ I </B> ";
Echo $ show ."";
}

/* Display the page information. If the page is displayed, the numbers in bold are displayed, and the remaining pages are hyperconnections. If the page is currently the third page, the following information is displayed:
1 2 3 4 5 6


Let's look at a complete instance.

<?
// To avoid errors caused by repeated file inclusion, the following conditions are added to determine whether a function exists:
If (! Function_exists (pageft )){
// Define the pageft () function. The three parameters are described as follows:
// $ Totle: total information;
// $ Displaypg: Number of information displayed on each page. The default value is 20;
// $ Url: the link in the paging navigation. Except for adding different Query Information "page", the link is the same as the url.
// The default value should be the url of the current page (that is, $ _ server ["request_uri"]), but the default value can only be a constant on the right, so this default value is set to an empty string, set this page url in the function.
Function pageft ($ totle, $ displaypg = 20, $ url = ''){

// Define several global variables:
// $ Page: Current page number;
// $ Firstcount: (Database tutorial) Start item of the query;
// $ Pagenav: page navigation bar code, which is not output in the function;
// $ _ Server: required to read the url "$ _ server [" request_uri "]" on this page.
Global $ page, $ firstcount, $ pagenav, $ _ server;

// To enable external function access to "$ displaypg", set it as a global variable. Note that after a variable is redefined as a global variable, the original value is overwritten, so the value is assigned again here.
$ Globals ["displaypg"] = $ displaypg;

If (! $ Page) $ page = 1;

// If $ url uses the default value, that is, null value, the value is assigned to the url of the current page:
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 ){
// Because the url may contain page number information, we need to remove it to add new page number information.
// Here the regular expression is used, please refer to the Regular Expression in php (http://www.bKjia. c0m)
$ Url_query = ereg_replace ("(^ | &) page = $ page", "", $ url_query );

// Replace the query string of the processed url with the query string of the original url:
$ Url = str_replace ($ parse_url ["query"], $ url_query, $ url );

// Add page after the url to query the information, but the value to be assigned is:

If ($ url_query) $ url. = "& page"; else $ url. = "page ";
} Else {
$ Url. = "? Page ";
}

// Page number calculation:
$ 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 = "show the <B>". ($ totle? ($ Firstcount + 1): 0 ). "</B>-<B> ". min ($ firstcount + $ displaypg, $ totle ). "</B> records, total $ totle records <br> ";

// If there is only one page, the function will jump out:
If ($ lastpg <= 1) return false;

$ Pagenav. = "<a href = '$ url = 1'> homepage </a> ";
If ($ prepg) $ pagenav. = "<a href = '$ url = $ prepg'> previous page </a>"; else $ pagenav. = "Previous Page ";
If ($ nextpg) $ pagenav. = "<a href = '$ url = $ nextpg'> next page </a>"; else $ pagenav. = "next page ";
$ Pagenav. = "<a href = '$ url = $ lastpg'> last page </a> ";

// Pull-down Jump list, listing all page numbers cyclically:
$ Pagenav. = "to the <select name = 'topage' size = '1' onchange = 'window. location =" $ url = "+ this. value'> n ";
For ($ I = 1; $ I <= $ lastpg; $ I ++ ){
If ($ I = $ page) $ pagenav. = "<option value = '$ I' selected> $ I </option> n ";
Else $ pagenav. = "<option value = '$ I'> $ I </option> n ";
}
$ Pagenav. = "</select> page, total $ lastpg page ";
}
}

?>

Call description:
?
$ SQL = "select * from guestbook ";
$ Result = mysql_query ($ SQL );
$ Total = mysql_num_rows ($ result );
Pageft ($ total, 10 );
$ SQL = mysql_query ("select * from guestbook limit $ firstcount, $ displaypg ");
While ($ row = mysql_fetch_object ($ SQL )){
?>
Call page display:

<? Echo $ pagenav;?>

Related Article

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.