PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Bole Chen   show page   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: An example
Class: show page
A class meant to be use to do pagination.
Author: By
Last change:
Date: 20 years ago
Size: 3,068 bytes
 

Contents

Class file image Download
<?php
/**
 * A Example to use show_page class
 *
 * @author Avenger <avenger@php.net>
 * @version $Id 2003-04-30 9:09:29 $
 */
/*
At first,assume we have a table like this:

table: user
Records:
Id Name Sex Age
====================================
1 Jim M 18
2 Bill M 26
3 Ted M 30
4 Mary F 16
5 Avenger M 19
6 Meow F 12

U can run the SQL below create the test database automatic

CREATE TABLE `user` (`Id` int(11) NOT NULL auto_increment,`Name` varchar(100) NOT NULL default '',`Sex` enum('M','F') default NULL,`Age` smallint(3) default NULL,PRIMARY KEY (`Id`)) TYPE=MyISAM;
INSERT INTO `user` VALUES("1", "Jim", "M", "18");
INSERT INTO `user` VALUES("2", "Bill", "M", "26");
INSERT INTO `user` VALUES("3", "Ted", "M", "30");
INSERT INTO `user` VALUES("4", "Mary", "F", "16");
INSERT INTO `user` VALUES("5", "Avenger", "M", "19");
INSERT INTO `user` VALUES("6", "Meow", "F", "12");

Note: Change the username and password to your's in the above script part of connect database.
*/

require_once 'page.inc.php';
$pg = new show_page;

$conn = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
mysql_select_db("test") or die(mysql_error());

// Recive the order variable
$_GET['order'] ? $order=$_GET['order'] : $order='Id';

// Count the total records
$result = mysql_query("SELECT COUNT(*) FROM user") or die("Invalid query: " . mysql_error());
$total_records = mysql_result($result,0);

// Page Set
$_GET['pagesize'] ? $pagesize=$_GET['pagesize'] : $pagesize = 2;
$pg->setvar(array('order'=>$order,'pagesize'=>$pagesize));
$pg->set($pagesize,$total_records);

/* u can used this script to limit too:
$limit = $pg->limit(1);
$SQL = "SELECT * FROM user ORDER BY $order LIMIT {$limit[1]},{$limit[2]}";
*/
$SQL = "SELECT * FROM user ORDER BY $order LIMIT ".$pg->limit();
$result = mysql_query($SQL) or die("Invalid query: " . mysql_error());

echo
"<h1>Show_page Class Example</h1>";

echo
"<table border='1' width='300'>\n";

echo
"<tr><th><a href='example.php?order=Id&pagesize=$pagesize'>Id</a></th><th><a href='example.php?order=Name&pagesize=$pagesize'>Name</a></th><th><a href='example.php?order=Sex&pagesize=$pagesize'>Sex</a></th><th><a href='example.php?order=Age&pagesize=$pagesize'>Age</a></th></tr>\n";

while (
$row = mysql_fetch_object($result)) {
   
printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td></tr>", $row->Id,$row->Name,$row->Sex,$row->Age);
}

echo
"</table><br>";

echo
"<b>Result:</b><br>\n";
echo
"<blockquote>
      TotalRecord:
{$total_records}<br>
      TotalPage:
{$pg->tpage}<br>
      PageSize:
{$pg->psize}<br>
      CurrerentPage:
{$pg->curr}<br>
      PassedParameter:
      "
;
print_r($pg->varstr);
echo
"</blockquote>";

echo
"<form action=''>Change pagesize:<input type='text' name='pagesize' size='2' value='{$pagesize}'> <input type='submit' value='change'></form>";

echo
"<hr>";
$pg->output();

mysql_close($conn);
?>