Php paging query, php Paging

Source: Internet
Author: User

Php paging query, php Paging
Paging query is much easier to write by referencing the page. class. php paging tool;1. First, you must have the following content:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<? Php include ("db. class. php "); $ db = new db (); $ SQL =" select * from chinastates "; $ arr = $ db-> Query ($ SQL ); // The default value is 1. foreach ($ arr as $ v) is not allowed) {echo "<tr> <td >{$ v [0] }</td> <td >{$ v [1] }</td> <td >{$ v [2] }</td> </tr> ";}?>

 

</table></body>

 

Figure:

2. reference the page. class. php paging tool to create an object and check the total number:
Include ("page. class. php "); // check the total number of items $ sz =" select count (*) from chinastates "; $ az = $ db-> Query ($ sz); // 1. create object $ page = new page ($ az [0] [0], 10); // Generally, only two parameters are written, the total number of items in the first data // take two indexes 0: First take index 0 two-dimensional array, get one array, then take index 0 // second parameter, display 10 items $ SQL = "select * from chinastates ". $ page-> limit; // 2. directly splice the limit in $ page (paging class) after the SQL statement to complete the paging $ arr = $ db-> Query ($ SQL ); // The default value is 1, which can be left empty.

 

3. Output:
<? Php // 3. output the page information echo $ page-> fpage (); // The fpage () method in the page Object?>

Total code:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 

 

The final completion diagram:

4. Paging Query

The query keyword is displayed on the page!

First, form:

 

<Form action = "fenyechaxun. php "method =" get "> // submit it to the current page and use get to pass <div> enter the region name for fuzzy search: <input type = "text" name = "area"/> <input type = "submit" value = ""/> </div> </form>

Paging query Conditions

 

$ Tj = "1 = 1"; // conditional constant variable if (! Empty ($ _ GET ["area"]) // checks whether the passed data is null {$ r = $ _ GET ["area"]; // to prevent the double quotation marks from being conflicted, the variable $ tj = "areaname like '% {$ r} %'"; // The condition is changed}

 

The query statement also needs to be changed:

 

$ Aall = "select count (*) from chinastates WHERE {$ tj}"; // query all data $ SQL = "select * from chinastates WHERE {$ tj }". $ page-> limit; // query by page. The page class calls limit.

Output:

 

<Div> <? Php echo $ page-> fpage (); // directly output the fpage () in the page class (output page information)?> </Div>

 

:

 

 

The queried data is also in the form of pagination:

 

Code:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> untitled document </title>
</Head>
<Body>
<H1> paging query <Form action = "fenyechaxun. php" method = "get">
<! -- // Submit it to the current page and pass it with get. Otherwise, the keyword cannot be obtained. -->
<Div>
Enter a region name for fuzzy search:
<Input type = "text" name = "area" value = ""/>
<Input type = "submit" value = ""/>
</Div>
</Form>
<Table border = "1" cellpadding = "0" cellspacing = "0">
<Tr>
<Td> Region Code </td>
<Td> region name </td>
<Td> parent Code </td>

</Tr>
<? Php
Include ("DBDA. class. php ");
// Reference encapsulation class
$ Db = new db ();
// Create object
Include "page. class. php ";
// Reference the page Tool

$ Tj = "1 = 1 ";
// Conditional constant variable
If (! Empty ($ _ GET ["area"])
// Determine whether the passed data is empty
{
$ R = $ _ GET ["area"];
// To prevent the double quotation marks from being conflicted, give the variable
$ Tj = "areaname like '% {$ r} % '";
// Condition change
}

 

$ Aall = "select count (*) from chinastates WHERE {$ tj }";
// Query all data
$ Arr = $ db-> Query ($ aall );
// Call SQL and return an array,
$ Page = new page ($ arr [0] [0], 10 );
// Page Object, 10 entries per page
$ SQL = "select * from chinastates WHERE {$ tj}". $ page-> limit;
// Query by page. The page class calls limit.
$ Ar = $ db-> Query ($ SQL );
// Execute and return an array (data)
Foreach ($ ar as $ v)
// Traverse
{
Echo "<tr>
<Td >{$ v [0]} </td>
<Td >{$ v [1]} </td>
<Td >{$ v [2]} </td>

</Tr>
";
}

// Display

?>

</Table>
<Div>
<? Php
Echo $ page-> fpage ();
// Directly output the fpage () in the page class (output page Information)
?>
</Div>
</Body>
</Html>

 

 

 

Page. class. php paging Tool

<? Php
/**
File: page. class. php
Perfect paging Page
*/
Class Page {
Private $ total; // The total number of records in the data table
Private $ listRows; // number of lines displayed per page
Private $ limit; // use the limit clause in SQL statements to limit the number of records to be retrieved and check which records to query. Use limit (0th) to skip records and retrieve 10 records.
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"
);
// Modifiable Information

// Display the content in the page information. You can set it by using the set () method.
Private $ listNum = 10; // The number of entries displayed on the default page list. The number of entries to be displayed in the following list is 10.

/**
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 to be displayed on each page. The default value is 25.
@ Param mixed $ query: Optional. It can be an array or a query string format to pass parameters to the target page.
@ Param bool $ ord: Optional. The default value is true. The page is displayed from the first page, and false indicates the last page.
*/
Public function _ construct ($ total, $ listRows = 25, $ query = "", $ ord = true ){
$ This-> total = $ total;
$ This-> listRows = $ listRows;
$ This-> uri = $ this-> getUri ($ query );
// Automatically obtain the browser address
$ This-> pageNum = ceil ($ this-> total/$ this-> listRows); // (total number of data records/several entries per page) ceil: rounded up
// Get the member variable, $ ord = ture Homepage
/* The following judgment is used to set the current plane */
// Go to 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;
}
} Else {
$ This-> page = 0;
}

$ This-> limit = "LIMIT". $ this-> setLimit ();
// LIMIT 10, 10
}

/**
Used to set display page information for consistent operations
@ Param string $ param is the subscript of the member attribute array config.
@ Param string $ value is used to set the element value corresponding to the config subscript.
@ Return object returns the current object $ this, which is used for connection operations.
*/
Function set ($ param, $ value ){
If (array_key_exists ($ param, $ this-> config )){
$ This-> config [$ param] = $ value;
}
Return $ this;
}

/* This method is not called directly. You can use this method to 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 pagination content
*/
Function fpage (){
$ Arr = func_get_args (); // USER Parameters

$ Html [0] = "<span class = 'p1'> & nbsp; <B >{$ this-> total }</B >{$ this-> config ["head"]} & nbsp; </span> ";
$ Html [1] = "& nbsp; <B>". $ this-> disnum (). "</B> & nbsp ;";

$ Html [2] = "& nbsp; this page is from <B >{$ this-> start ()}-{$ this-> end ()} </B> & nbsp ;";

$ Html [3] = "& nbsp; <B >{$ this-> page}/{$ this-> pageNum} </B> page & nbsp ;";

$ Html [4] = $ this-> firstprev ();

$ Html [5] = $ this-> pageList ();

$ Html [6] = $ this-> nextlast ();
$ Html [7] = $ this-> goPage ();

$ Fpage = '<div style = "font: 12px \' \ 5B8B \ 4F53 \ ', san-serif;"> ';
If (count ($ arr) <1)
$ Arr = array (0, 1, 2, 3, 4, 5, 6, 7 );

For ($ I = 0; $ I <count ($ arr); $ I ++)
$ Fpage. = $ html [$ arr [$ I];

$ Fpage. = '</div> ';
Return $ fpage;
}

/* Private methods used inside the object ,*/
Private function setLimit (){
If ($ this-> page> 0)
Return ($ this-> page-1) * $ this-> listRows. ", {$ this-> listRows }";
Else
Return 0;
}

/* Private method used inside the object, used 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 = $ url .'? ';
}

Return $ url;
}

/* 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;
}

/* Private method used inside the object to obtain the number of records ending on the current page */
Private function end (){
Return min ($ this-> page * $ this-> listRows, $ this-> total );
}

/* Private method used inside the object to obtain operation information of the previous page and homepage */
Private function firstprev (){
If ($ this-> page> 1 ){
$ Str = "& nbsp; <a href = '{$ this-> uri} page = 1' >{$ this-> config ["first"]} </a> & nbsp ;";
$ Str. = "<a href = '{$ this-> uri} page = ". ($ this-> page-1 ). "'>{$ this-> config [" prev "]} </a> & nbsp ;";
Return $ str;
}

}

/* Private method used inside the object to obtain page list information */
Private function pageList (){
$ LinkPage = "& nbsp; <B> ";

$ 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. = "<a href = '{$ this-> uri} page = {$ page}'> {$ page} </a> & nbsp ;";
}
/* Information on the current page */
If ($ this-> pageNum> 1)
$ LinkPage. = "<span style = 'padding: 1px 2px; background: # BBB; color: white' >{$ this-> page} </span> & nbsp ;";

/* List after the current page */
For ($ I = 1; $ I <= $ inum; $ I ++ ){
$ Page = $ this-> page + $ I;
If ($ page <= $ this-> pageNum)
$ LinkPage. = "<a href = '{$ this-> uri} page = {$ page}'> {$ page} </a> & nbsp ;";
Else
Break;
}
$ LinkPage. = '</B> ';
Return $ linkPage;
}

/* Private methods used inside the object to obtain operation information on the next and last pages */
Private function nextlast (){
If ($ this-> page! = $ This-> pageNum ){
$ Str = "& nbsp; <a href = '{$ this-> uri} page = ". ($ this-> page + 1 ). "'>{$ this-> config [" next "]} </a> & nbsp ;";
$ Str. = "& nbsp; <a href = '{$ this-> uri} page = ". ($ this-> pageNum ). "'>{$ this-> config [" last "]} </a> & nbsp ;";
Return $ str;
}
}

/* Private method used inside the object for displaying and processing the form jump page */
Private function goPage (){
If ($ this-> pageNum> 1 ){
Return '& nbsp; <input style = "width: 20px; height: 17px! Important; height: 18px; border: 1px solid # CCCCCC; "type =" text "onkeydown =" javascript: if (event. keyCode = 13) {var page = (this. value> '. $ this-> pageNum. ')? '. $ This-> pageNum. ': this. value; location = \''. $ this-> uri. 'page = \ '+ page + \'} "value = "'. $ this-> page. '"> <input style =" cursor: pointer; width: 25px; height: 18px; border: 1px solid # CCCCCC; "type =" button "value =" GO "onclick =" javascript: var page = (this. previussibling. value> '. $ this-> pageNum. ')? '. $ This-> pageNum. ': this. previussibling. value; location = \''. $ this-> uri. 'page = \ '+ page + \' "> & nbsp ;';
}
}

/* Private method used inside the object 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;
}
}
}

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.