Write a pagination class by yourself

Source: Internet
Author: User
Class. php :? * Member name: TurnPage function: Entrance (): Class entry. the parameter is the parameter SelectData ($ connection, $ query) of all member functions ): select the data function and return to class. php:
/*
Bytes -----------------------------------------------------------------------------------------------
Name: TurnPage
Purpose: Display by page
Member functions:
Entrance (): Class entry. the parameter is the parameter of all member functions.
SelectData ($ connection, $ query): Select the data function and return an array.
ShowData ($ array, $ I, $ j): a function that displays the values of each field. a value is returned.
MakeTable ($ array, $ intPageBegin, $ intPageEnd): generates table functions based on the number of fields to be displayed. the parameters are arrays, information sequence number at the beginning of each page and information sequence number at the end of each page
MakeLink ($ parameter = null): the link for displaying pages. optional.
GetFileName (): Get the name of the current execution file and return the file name.

Relationship between functions:
Bytes -----------------------------------------------------------------------------------------------
*/
Class TurnPage
{
Var $ strArray;
Var $ cols;
Var $ rows;
Var $ strFieldName;
Var $ intPageBegin;
Var $ intPageEnd;

Function Entrance ($ connection, $ query)
{
$ MyArray = $ this-> SelectData ($ connection, $ query );

$ This-> GetFileName ();

$ This-> MakeLink ($ parameter = null );


$ This-> MakeTable ($ myArray, $ this-> intPageBegin, $ this-> intPageEnd );

}

Function SelectData ($ connection, $ query)
{
$ Result = @ mysql_query ($ query, $ connection) or die ("unable to select the data! ");

$ IntFieldsNum = mysql_num_fields ($ result );

For ($ a = 0; $ a <$ intFieldsNum; $ a ++) // obtain the names of selected fields
{
$ This-> strFieldName [$ a] = mysql_field_name ($ result, $ );
}

$ This-> cols = $ intFieldsNum;

$ Count = 0;

While ($ rows = mysql_fetch_row ($ result ))
{
For ($ I = 0; $ I <$ intFieldsNum; $ I ++)
{
$ Data [$ count] [$ I] = trim ($ rows [$ I]);
}
$ Count ++;
}
$ This-> rows = count ($ data );
$ This-> strArray = $ data;
Return $ this-> strArray;
}

Function ShowData ($ array, $ I, $ j)
{
Return $ array [$ I] [$ j];
}

Function MakeTable ($ array, $ intPageBegin, $ intPageEnd)
{
Echo"
























";Echo" ";For ($ m = 0; $ m <$ this-> cols; $ m ++){Echo" ";}Echo" ";For ($ I = $ intPageBegin; $ I <$ intPageEnd; $ I ++){If ($ I % 2 = 0){$ BgColor = "# D2F0FF ";}Else{$ BgColor = "# ffffff ";}Echo" ";For ($ j = 0; $ j <$ this-> cols; $ j ++){Echo" ";}Echo" ";}Echo"
". $ This-> strFieldName [$ m]."
". $ This-> ShowData ($ array, $ I, $ j )."
";
}

Function GetFileName ()
{
$ StrPath = $ _ SERVER ['php _ SELF '];
$ StrFileName = trim (substr ($ strPath, (strrpos ($ strPath, "/") + 1 )));
Return $ strFileName;
}

Function MakeLink ($ parameter = null)
{
If ($ parameter = null)
{
$ StrParam = null;
}
Else
{
$ StrParam = $ parameter;
}

If ($ _ GET ["intPage"] = null)
{
$ IntPage = 1;
}
Else
{
$ IntPage = trim ($ _ GET ['intpage']);
}

$ IntPerPage = 10;

$ Total = $ this-> rows;

$ StrFile = $ this-> GetFileName ();

$ IntPageBegin = $ intPerPage * $ intPage-$ intPerPage;

$ This-> intPageBegin = $ intPageBegin;



If ($ intPage * $ intPerPage <$ total)
{
$ IntPageEnd = $ intPageBegin + $ intPerPage;
}
Else
{
$ IntPageEnd = $ total;
}

$ This-> intPageEnd = $ intPageEnd;

Echo"
Total Information: ". $ this-> rows ." ";
If ($ intPage> 1)
{
Echo "previous page ";
}
Echo"
";
If ($ intPage * $ intPerPage <$ total)
{
Echo "next page ";
}
Echo"
";

}

}
?>
Test. php:
Include ("../common/connection. php"); // database connection operations
Include ("../common/class. php"); // include the preceding file

$ Query = "select * from uphoto order by intUID asc ";

$ Test = new TurnPage;

$ Test-> Entrance ($ connection, $ query );

?>

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.