A "common" query paging class that can be used to query all tables

Source: Internet
Author: User
A "general" query paging class that can query all tables has a recent whim. I hope to write a paging class that can query all tables. In actual development, I am afraid that the most frequently used code is to query and display the result set by page, while the table structure is diverse, I want to improve the code reuse rate and maintainability as much as possible. The following is what I wrote. please give me some advice and test it. a query of all the tables is a "common" query, you want to write a paging class that can be used to query all tables. In actual development, I am afraid that the most frequently used code is to query and display the result set by page, while the table structure is diverse, I want to improve the code reuse rate and maintainability as much as possible.
The following is my description. please give us some advice and test it to see if it can be improved or supported.
Currently, only a single table is supported, and joint query is not supported. However, we may consider how to support it in the future.

Code:
/*************************************** *************************
This class is mainly used to solve simple data queries targeting many tables and is still being improved. I hope the majority of users will give more comments and suggestions.

My goal is to develop a class that can be used to query almost all mysql tables and pagination the result set.
You can automatically identify the field values to be displayed in the query.

Here, I would like to express my special thanks to chinaunix friends, especially PHP netizens, for their long-term help.
He has always given me selfless help, even his source code.

This class can be freely referenced, used, modified by anyone. However, keep this text.
All the losses caused by using this class are irrelevant to the author tonera.

I am still considering that for some complex union queries, a class can be derived to reconstruct the SQL implementation.
This class does not consider more display styles. you can construct it yourself.
**************************************** *************************/
Class browser {
Var $ c_table; // The name of the table to be queried.
Var $ c_rows; // number of rows to be displayed
Var $ c_lation; // query condition
Var $ c_order; // sorting condition
Var $ c_result; // The queried data connection handle.
Var $ c_query; // The final query constructed
Var $ c_found; // result set
Var $ c_error; // error collector
Var $ c_offset; // The offset displayed by page
Var $ total; // The total number of result sets.

// Connect to the database
Function connect (){
Include '../connect. inc. php ';
If ($ connection = false ){
$ This-> c_error. = "The database is not connected.
";
Exit;
}
$ This-> c_result = $ connection;
}

// Constructor, initialization variable
Function browser ($ tablename, $ row, $ SQL, $ lation, $ orderby ){
$ This-> c_table = $ tablename;
$ This-> c_rows = $ row;
If (empty ($ this-> c_offset )){
$ This-> c_offset = 0;
}
If (empty ($ tablename) or empty ($ row) or empty ($ SQL )){
$ This-> c_error = "tables not queried, number of rows not displayed in batches, or no query statement are displayed.
";
}
$ This-> c_query = $ SQL;
If (! Empty ($ lation )){
$ This-> c_query. = "". $ lation;
}
If (! Empty ($ orderby )){
$ This-> c_query. = "". $ orderby;
}
}

// Calculate the total number of pages
Function TatolPage (){
$ Sult = mysql_query ("select count (*) as 'total _ rows 'from $ this-> c_table", $ this-> c_result );
If ($ sult = false ){
$ This-> c_error. = "failed to query the total number of computing result sets. Please check.
";
Exit;
}
$ Tempvar = mysql_fetch_array ($ sult );
$ This-> total = $ tempvar [0];
}

// Query the result set and save it to the array c_found [] []
Function GetFound (){
$ Sult = mysql_query ($ this-> c_query, $ this-> c_result) or die (mysql_error ());
While ($ found = mysql_fetch_array ($ sult )){
$ This-> c_found [] = $ found;
}
}

// Query data and save the results to a variable by page
Function ShowTable (){

$ This-> connect ();
$ This-> TatolPage ();

If (empty ($ _ GET [offset]) {
$ _ GET [offset] = 0;
}
$ This-> c_query. = "limit". $ _ GET [offset]. ",". $ this-> c_rows;
$ Sult = mysql_query ($ this-> c_query, $ this-> c_result) or die (mysql_error ());

// Parse the query to obtain the field value to be displayed.
$ Tempvar = explode ("", $ this-> c_query );
$ Fields = explode (",", $ tempvar [1]); // field value (array)

// Display data to a table
$ Echo_content. ="



$ Echo_content. ="



















";While ($ found = @ mysql_fetch_array ($ sult )){$ Echo_content. =" ";// Display the fields specified by the user.For ($ I = 2; $ I ";}$ Echo_content. =" ";}// PaginationIf ($ this-> c_rows = 0 ){$ This-> c_error. = "The number of entries displayed on each page cannot be 0 ";Exit;}$ Total_page = ceil ($ this-> total/$ this-> c_rows );$ Pre_page = $ _ GET [offset]-$ this-> c_rows;// Next page$ Nex_page = $ _ GET [offset] + $ this-> c_rows;// Display the previous pageIf ($ pre_page> = 0 ){$ Echo_content. =" ";} Else {$ Echo_content. = "& Next page";}$ Echo_content. ="
";
$ Echo_content. = "". $ found [1]."
". $ Found [$ I]."
Previous Page &";
} Else {
$ Echo_content. ="
Previous Page &";
}
// Display the page number
For ($ I = 1; $ I <= $ total_page; $ I ++ ){
If ($ _ GET [offset]/$ this-> c_rows = ($ i-1 )){
$ Echo_content. = "& ". $ I. "Page &";
} Else {
$ Echo_content. = "& c_rows."> ". $ I ."&";
}
}
// Display the next page
If ($ nex_page! = 0 and ($ _ GET [offset] + $ this-> c_rows) <= $ this-> total ){
$ Echo_content. = "& Next page
";
Return $ echo_content;

}
}

/* Example
// Browser ("table name", number displayed on each page, "SQL", "query condition", "sorting condition ");
$ Gggg = new browser ("news", 5, "select auto_id, news_title from news", "", "order by newstime desc ");

$ Temp = $ gggg-> ShowTable ();
Echo $ temp;

// $ Gggg-> GetFound () stores the query result set in a two-dimensional array, which is not used in this example.
*/
?>

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.