A "GENERIC" query paging class that can query all tables _php basics

Source: Internet
Author: User
Tags explode

A "GENERIC" query paging class that can query all tables recently had the whim to write a query pagination class that can be used for all tables. Because in the actual development, I am afraid that the query and the result set pagination is the most used code, and the structure of the table is diverse, I would like to improve the reuse rate and maintenance of the code as much as possible.
Here's what I wrote, so please give me some pointers and tests to see if you can make better improvements and more support.
Only a single table is currently supported, and federated queries are not supported. But the future can consider how to support it.

Code:
<?php
/****************************************************************
This class is mainly to solve a lot of tables for simple data queries, but also in the improvement. Hope that the broad masses of netizens to make more comments and suggestions.

My goal is to develop a class that can be applied to almost all MySQL tables and pagination the result set.
You can automatically identify the field values that you want to display in your query.

Here, special statement, thanks to Chinaunix's friends, especially the PHP version of the netizens for a long time to help me.
Especially to mention is Nightkids, all along, he has given me selfless help, even his source code.

This class can be freely referenced, 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 considering that for some complex federated queries, you can derive a class to reconstruct 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 a 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 paging 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. <br> ";
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= "table without query or number of rows or no query statement <br>";
}
$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.= The result set totals destination query failed, please check. <br> ";
Exit
}
$tempvar =mysql_fetch_array ($sult);
$this->total= $tempvar [0];
}

Query gets result 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 the data and depositing the 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 value of the field to display
$tempvar =explode ("", $this->c_query);
$fields =explode (",", $tempvar [1]); field values (array)

Show data to a table
$echo _content.= "<table width=100% border=1>";
while ($found = @mysql_fetch_array ($sult)) {
$echo _content.= "<tr><td>";
$echo _content.= "<a href=newsshow.php?id=". $found [0]. " > ". $found [1]." </a></td> ";
Displays the user-specified fields, which need to be looked at carefully
for ($i =2; $i <count ($fields); $i + +) {
$echo _content.= "<td>". $found [$i]. " </td> ";
}
$echo _content.= "</tr>";
}

Paging
if ($this->c_rows==0) {
$this->c_error.= "The number of displays per 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;
Show Previous Page
if ($pre _page>=0) {
$echo _content.= "<tr><td colspan=10><a href= $PHP _self?offset=". $pre _page. " > Prev </a>& ";
}else{
$echo _content.= "<tr><td colspan=10> 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.= "&<a href= $PHP _self?offset=". ($i-1) * $this->c_rows. " > ". $i." </a>& ";
}
}
Show Next Page
if ($nex _page!=0 and ($_get[offset]+ $this->c_rows) <= $this->total) {
$echo _content.= "&<a href= $PHP _self?offset=". $nex _page. " > next page </a></td></tr> ";
}else{
$echo _content.= "& Next page </td></tr>";
}
$echo _content.= "</table>";
return $echo _content;

}
}

/* Examples
Browser ("Table name", Number of display per 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 () is a two-dimensional array of query result sets, 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.