A "common" query page class for all Tables _php tutorial

Source: Internet
Author: User
A "common" query page class that can query all tables The most recent whim is to write a query that can be paged across all tables. Because in the actual development, I am afraid to query and display the result set paging is the most used code, and the structure of the table is diverse, I want to maximize the code reuse rate and maintainability.
Here is what I wrote, please give your guidance, test, see if you can make better improvements and more support.
Currently, only a single table is supported, and federated queries are not supported. But the future can be considered how to support.

Code:
/****************************************************************
This class is mainly to solve for a lot of tables of simple data query, is still in perfect. Hope that the majority of users more comments and suggestions.

My goal is to develop a class that can be used almost all of the MySQL table's queries and pagination the result set.
You can automatically identify the field values to display in the query.

In this, special statement, to thank Chinaunix's friends, especially the PHP version of the netizens have long given me the help.
Especially to mention is Nightkids, always, he has given me selfless help, even his source code.

This class can be freely quoted, used, and modified by anyone. But please keep this text.
All the losses caused by using this class have nothing to do with the author Tonera.

I'm also thinking that for some complex union queries, you can derive a class that reconstructs the SQL implementation.
This class does not consider more display styles, and you can construct them yourself.
*****************************************************************/
Class browser{
var $c _table; The name of the table to query
var $c _rows; Number of rows to display
var $c _lation; Criteria for the query
var $c _order; Criteria for sorting
var $c _result; Data connection handle for query
var $c _query; The final constructed query
var $c _found; Result set
var $c _error; Error collector
var $c _offset; Offset for pagination display
var $total; Total number of result sets

Connecting to a database
function Connect () {
Include '. /connect.inc.php ';
if ($connection ==false) {
$this->c_error.= "is not connected to the database.
";
Exit
}
$this->c_result= $connection;
}

constructors, initializing variables
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= "No query table or no batch shows how many rows or no query statements
";
}
$this->c_query= $sql;
if (!empty ($lation)) {
$this->c_query.= "". $lation;
}
if (!empty ($orderby)) {
$this->c_query.= "". $orderby;
}
}

Calculate Total Pages
function Tatolpage () {
$sult =mysql_query ("SELECT count (*) as ' total_rows ' from $this->c_table", $this->c_result);
if ($sult ==false) {
$this->c_error.= "Count the total number of result sets for the purpose of query failure, please check.
";
Exit
}
$tempvar =mysql_fetch_array ($sult);
$this->total= $tempvar [0];
}

Query results set, stored in 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;
}
}

Querying data and storing results in a variable
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 query to get the field values to display
$tempvar =explode ("", $this->c_query);
$fields =explode (",", $tempvar [1]); field value (Array)

Show data to a table
$echo _content.= "




$echo _content.= "



















";while ($found = @mysql_fetch_array ($sult)) {$echo _content.= " "; Displays the user-specified field, where you need to look closely for the for ($i =2; $i "; } $echo _content.= " "; }//Paging if ($this->c_rows==0) {$this->c_error.= "the number of pages per page cannot be 0"; Exit } $total _page=ceil ($this->total/$this->c_rows); $pre _page=$_get[offset]-$this->c_rows; Next $nex _page=$_get[offset]+ $this->c_rows; Display previous if ($pre _page>=0) {$echo _content.= " "; }else{$echo _content.= "& Next Page"; } $echo _content.= "
";
$echo _content.= "". $found [1]. "
". $found [$i]."
on page & ";
}else{
$echo _content.= "
on page & ";
}
Show page numbers
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." & ";
}
}
Show 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 of displays per page, "SQL", "query Criteria", "sort Criteria");
$GGGG =new Browser ("News", 5, "Select Auto_id,news_title from News", "", "Order by newstime Desc");

$temp = $gggg->showtable ();
Echo $temp;

$GGGG->getfound () is the existence of a two-dimensional array of query result sets, which is not used in this example.
*/
?>

http://www.bkjia.com/PHPjc/314651.html www.bkjia.com true http://www.bkjia.com/PHPjc/314651.html techarticle a "Common" query page class that can query all tables The most recent whim is to write a query that can be paged across all tables. Because in the actual development, I am afraid to query and will ...

  • 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.