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"
". $ This-> strFieldName [$ m]." | ";}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"
". $ This-> ShowData ($ array, $ I, $ j )." | ";}Echo"
";}Echo"
";
}
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 );
?>