kevinc
10-02-2003, 08:57 AM
Hi, some comments will be cool :)
<?PHP
/**
Here's a generic table layout thing. -- You can populate it yourself!
CREATE TABLE products (
id int(6) NOT NULL auto_increment,
Product_Name varchar(255) default '0',
Product_Description text,
Product_Price varchar(7) default '0',
Product_Postage varchar(7) default '0',
Product_Thumbnail varchar(200) default '0',
Product_InStock varchar(7) default '0',
PRIMARY KEY (id)
) TYPE=MyISAM;
* class Products
*
* This class is used for listing and paging purposes. It's capable of handling many records as well as singular.
* It can be used for anything, as you specify which field values to use.
* It returns the values you specify, so you can use them in your PHP scripts.
*
*
* Please see the attached LICENSE and COPYING files for copyright information.
*
* Kevin Cadman - 2003.
*
* Last Updated - 31/07/2003
*/
class Products {
/**
* Products::$_DB
*
* Private: Database Object Variable
*
*/
var $_DB = FALSE;
/**
* Products::$_Results_Per_Page
*
* Private: How many results to show on one page.
* This must be greater than 2. If this is FALSE all results will be shown.
* If this is FALSE, do not use the Pagination() function.
*/
var $_Results_Per_Page = 6;
/**
* Products::$_Products_Table
*
* Private: The name of the table where the products are kept.
*
*/
var $_Products_Table = "products";
/**
* Products::$_IDField
*
* Private: ID Field in the $_Products_Table.
* This is the Primary Key field which is essential. This is used to uniquely reference each record.
*
*/
var $_IDField = "id";
/**
* Products::$ProductFields
*
* Public: The list of fields which you want returned when querying the products table.
* These must be initialized to false.
*
*/
var $ProductFields = array(
"id" =>FALSE,
"Product_Name" =>FALSE,
"Product_Description" =>FALSE,
"Product_Price" =>FALSE,
"Product_Thumbnail" =>FALSE,
"Product_InStock" =>FALSE,
"Product_Postage" =>FALSE,
"Product_PicLink" =>FALSE
);
/**
* Products::$_UseStock
*
* Private: Specifies whether or not we're going to use the STOCK system.
* This will only return items that are "In Stock" (where the $StockField field is > 0.)
*
*/
var $_UseStock = FALSE;
/**
* Products::$StockField
*
* Private: The field name of the "In Stock" field.
*
*/
var $StockField = "Product_InStock";
/**
* Products::$_OrderByField
*
* Private: The field we are going to order all the results return by.
*
*/
var $_OrderByField = "Product_Name";
/**
* Products::$_OrderHow
*
* Private: The method in which we are going to order the results returned.
* Valid options are asc and desc.
*
*/
var $_OrderHow = "asc";
/**
* Products::$_LimitPosition
*
* Private: Used to determine which page we are currently on.
*
*/
var $_LimitPosition = 0;
/**
* Products::$Affected_Rows
*
* Public: Used to store the amount of total records returned.
* Used in conjunction with $_LimitPosition to determine which page we are currently on.
*
*/
var $Affected_Rows = 0;
/**
* Products::$_Pagination_Seperator
*
* Private: The seperator character to seperate either the numbers(advanced) for the pagination, or the words(simple).
*
*/
var $_Pagination_Seperator = " ";
/**
* Products::$_Pagination_Selected_Style
*
* Private: The style used to show the "selected" page.
* B = Bold, I = Italic, U = Underline (You may use any corresponding HTML.
*
*/
var $_Pagination_Selected_Style = "B";
/**
* Products::Products()
*
* Constructor Function. Initializes the $_DB variable, and the $_LimitPosition variable.
* If the Session is set for LimitPage, use it, otherwise, set it to 0. (Which it already is.)
*
*/
function Products(&$DB,$TableName="") {
$this->_DB = &$DB;
if($TableName != "") {
$this->_Products_Table = $TableName;
}
$this->_LimitPosition = (isset($_SESSION['LimitPage']) ? $_SESSION['LimitPage'] : 0);
}
/**
* Products::Product_Listing()
*
*
*
* Used to return valid Product Listings.
* If no paramter is passed, will return all the records according to the amount per page specified.
* If $ID is passed, it will only return the corresponding record.
* Clause if any additional WHERE statements you want to pass.
* @param integer $ID
* @param string $Clause
*/
function Product_Listing($ID="",$Clause="") {
$query = "SELECT *
FROM ".$this->_Products_Table."
";
if($this->_UseStock) {
$query .= "WHERE
".$this->StockField." > 0
";
if($ID != "") {
$query .= "
&& ".$this->_IDField."='".$ID."'
";
}
} elseif($ID != "") {
$query .= "WHERE
".$this->_IDField."='".$ID."'
";
if($Clause != "") {
$query .= " && ".$Clause."";
}
} elseif($Clause != "" && $ID == "") {
$query .= "WHERE ".$Clause."";
}
$result = $this->_DB->query($query);
$this->Affected_Rows = $result->numRows();
$query .= "
ORDER BY ".$this->_OrderByField." ".$this->_OrderHow."
";
if($this->_Results_Per_Page != FALSE && $ID == "") {
$query .= " LIMIT ".$this->_LimitPosition.",".$this->_Results_Per_Page."
";
}
$result = $this->_DB->query($query);
if(!DB::isError($result)) {
if($ID != "") {
$row = $result->fetchrow();
foreach($this->ProductFields as $Wanted_Field=>$Name) {
$this->ProductFields[$Wanted_Field] = stripslashes($row->$Wanted_Field);
}
} else {
while($row = $result->fetchrow()) {
foreach($this->ProductFields as $Wanted_Field=>$Name) {
$this->ProductFields[$Wanted_Field][] = stripslashes($row->$Wanted_Field);
}
}
}
}
}
/**
* Products::Pagination()
*
* Used to display page seperation control.
* Valid Methods: ADVANCED & SIMPLE
* Advanced will use the numbered method (1,2,3,4)
* Simple will use Previous Page - Next Page
*
* @param string $Pagination_Method
*/
function Pagination($Pagination_Method="ADVANCED") {
$Output = "";
switch(strtoupper($Pagination_Method)) {
case "ADVANCED" :
case "ADV" :
case "1" :
default : {
$this->_LimitPosition = ($this->_LimitPosition == 0 ? $this->_LimitPosition = 1 : $this->_LimitPosition);
for($Counter=0;$Counter<(ceil($this->Affected_Rows / $this->_Results_Per_Page));++$Counter) {
if(round(($this->_LimitPosition / $this->_Results_Per_Page)) == ($Counter)) {
$Output .= $this->_Pagination_Seperator.
"<".$this->_Pagination_Selected_Style.">".($Counter+1)."</".$this->_Pagination_Selected_Style.">"
.$this->_Pagination_Seperator;
} else {
$Output .= '<a href="'.$_SERVER['PHP_SELF'].'?LimitPage='.round(($Counter*$this->_Results_Per_Page)).'">
'.($Counter+1).'</a>'
.$this->_Pagination_Seperator;
}
}
BREAK;
}
case "SIMPLE" :
case "SIM" :
case "2" : {
if($this->_LimitPosition != 0) {
$Output .= '
<a href="'.$_SERVER['PHP_SELF'].'?LimitPage='.($this->_LimitPosition-$this->_Results_Per_Page).'">
Previous Page</a>
'.$this->_Pagination_Seperator;
} else {
$Output .= '
Previous Page
'.$this->_Pagination_Seperator;
}
if(($this->_LimitPosition+$this->_Results_Per_Page) <= $this->Affected_Rows) {
$Output .= '
<a href="'.$_SERVER['PHP_SELF'].'?LimitPage='.($this->_LimitPosition+$this->_Results_Per_Page).'">
Next Page</a>
'.$this->_Pagination_Seperator;
} else {
$Output .= '
Next Page
';
}
BREAK;
}
}
return $Output;
}
}
?>
<?PHP
/**
Here's a generic table layout thing. -- You can populate it yourself!
CREATE TABLE products (
id int(6) NOT NULL auto_increment,
Product_Name varchar(255) default '0',
Product_Description text,
Product_Price varchar(7) default '0',
Product_Postage varchar(7) default '0',
Product_Thumbnail varchar(200) default '0',
Product_InStock varchar(7) default '0',
PRIMARY KEY (id)
) TYPE=MyISAM;
* class Products
*
* This class is used for listing and paging purposes. It's capable of handling many records as well as singular.
* It can be used for anything, as you specify which field values to use.
* It returns the values you specify, so you can use them in your PHP scripts.
*
*
* Please see the attached LICENSE and COPYING files for copyright information.
*
* Kevin Cadman - 2003.
*
* Last Updated - 31/07/2003
*/
class Products {
/**
* Products::$_DB
*
* Private: Database Object Variable
*
*/
var $_DB = FALSE;
/**
* Products::$_Results_Per_Page
*
* Private: How many results to show on one page.
* This must be greater than 2. If this is FALSE all results will be shown.
* If this is FALSE, do not use the Pagination() function.
*/
var $_Results_Per_Page = 6;
/**
* Products::$_Products_Table
*
* Private: The name of the table where the products are kept.
*
*/
var $_Products_Table = "products";
/**
* Products::$_IDField
*
* Private: ID Field in the $_Products_Table.
* This is the Primary Key field which is essential. This is used to uniquely reference each record.
*
*/
var $_IDField = "id";
/**
* Products::$ProductFields
*
* Public: The list of fields which you want returned when querying the products table.
* These must be initialized to false.
*
*/
var $ProductFields = array(
"id" =>FALSE,
"Product_Name" =>FALSE,
"Product_Description" =>FALSE,
"Product_Price" =>FALSE,
"Product_Thumbnail" =>FALSE,
"Product_InStock" =>FALSE,
"Product_Postage" =>FALSE,
"Product_PicLink" =>FALSE
);
/**
* Products::$_UseStock
*
* Private: Specifies whether or not we're going to use the STOCK system.
* This will only return items that are "In Stock" (where the $StockField field is > 0.)
*
*/
var $_UseStock = FALSE;
/**
* Products::$StockField
*
* Private: The field name of the "In Stock" field.
*
*/
var $StockField = "Product_InStock";
/**
* Products::$_OrderByField
*
* Private: The field we are going to order all the results return by.
*
*/
var $_OrderByField = "Product_Name";
/**
* Products::$_OrderHow
*
* Private: The method in which we are going to order the results returned.
* Valid options are asc and desc.
*
*/
var $_OrderHow = "asc";
/**
* Products::$_LimitPosition
*
* Private: Used to determine which page we are currently on.
*
*/
var $_LimitPosition = 0;
/**
* Products::$Affected_Rows
*
* Public: Used to store the amount of total records returned.
* Used in conjunction with $_LimitPosition to determine which page we are currently on.
*
*/
var $Affected_Rows = 0;
/**
* Products::$_Pagination_Seperator
*
* Private: The seperator character to seperate either the numbers(advanced) for the pagination, or the words(simple).
*
*/
var $_Pagination_Seperator = " ";
/**
* Products::$_Pagination_Selected_Style
*
* Private: The style used to show the "selected" page.
* B = Bold, I = Italic, U = Underline (You may use any corresponding HTML.
*
*/
var $_Pagination_Selected_Style = "B";
/**
* Products::Products()
*
* Constructor Function. Initializes the $_DB variable, and the $_LimitPosition variable.
* If the Session is set for LimitPage, use it, otherwise, set it to 0. (Which it already is.)
*
*/
function Products(&$DB,$TableName="") {
$this->_DB = &$DB;
if($TableName != "") {
$this->_Products_Table = $TableName;
}
$this->_LimitPosition = (isset($_SESSION['LimitPage']) ? $_SESSION['LimitPage'] : 0);
}
/**
* Products::Product_Listing()
*
*
*
* Used to return valid Product Listings.
* If no paramter is passed, will return all the records according to the amount per page specified.
* If $ID is passed, it will only return the corresponding record.
* Clause if any additional WHERE statements you want to pass.
* @param integer $ID
* @param string $Clause
*/
function Product_Listing($ID="",$Clause="") {
$query = "SELECT *
FROM ".$this->_Products_Table."
";
if($this->_UseStock) {
$query .= "WHERE
".$this->StockField." > 0
";
if($ID != "") {
$query .= "
&& ".$this->_IDField."='".$ID."'
";
}
} elseif($ID != "") {
$query .= "WHERE
".$this->_IDField."='".$ID."'
";
if($Clause != "") {
$query .= " && ".$Clause."";
}
} elseif($Clause != "" && $ID == "") {
$query .= "WHERE ".$Clause."";
}
$result = $this->_DB->query($query);
$this->Affected_Rows = $result->numRows();
$query .= "
ORDER BY ".$this->_OrderByField." ".$this->_OrderHow."
";
if($this->_Results_Per_Page != FALSE && $ID == "") {
$query .= " LIMIT ".$this->_LimitPosition.",".$this->_Results_Per_Page."
";
}
$result = $this->_DB->query($query);
if(!DB::isError($result)) {
if($ID != "") {
$row = $result->fetchrow();
foreach($this->ProductFields as $Wanted_Field=>$Name) {
$this->ProductFields[$Wanted_Field] = stripslashes($row->$Wanted_Field);
}
} else {
while($row = $result->fetchrow()) {
foreach($this->ProductFields as $Wanted_Field=>$Name) {
$this->ProductFields[$Wanted_Field][] = stripslashes($row->$Wanted_Field);
}
}
}
}
}
/**
* Products::Pagination()
*
* Used to display page seperation control.
* Valid Methods: ADVANCED & SIMPLE
* Advanced will use the numbered method (1,2,3,4)
* Simple will use Previous Page - Next Page
*
* @param string $Pagination_Method
*/
function Pagination($Pagination_Method="ADVANCED") {
$Output = "";
switch(strtoupper($Pagination_Method)) {
case "ADVANCED" :
case "ADV" :
case "1" :
default : {
$this->_LimitPosition = ($this->_LimitPosition == 0 ? $this->_LimitPosition = 1 : $this->_LimitPosition);
for($Counter=0;$Counter<(ceil($this->Affected_Rows / $this->_Results_Per_Page));++$Counter) {
if(round(($this->_LimitPosition / $this->_Results_Per_Page)) == ($Counter)) {
$Output .= $this->_Pagination_Seperator.
"<".$this->_Pagination_Selected_Style.">".($Counter+1)."</".$this->_Pagination_Selected_Style.">"
.$this->_Pagination_Seperator;
} else {
$Output .= '<a href="'.$_SERVER['PHP_SELF'].'?LimitPage='.round(($Counter*$this->_Results_Per_Page)).'">
'.($Counter+1).'</a>'
.$this->_Pagination_Seperator;
}
}
BREAK;
}
case "SIMPLE" :
case "SIM" :
case "2" : {
if($this->_LimitPosition != 0) {
$Output .= '
<a href="'.$_SERVER['PHP_SELF'].'?LimitPage='.($this->_LimitPosition-$this->_Results_Per_Page).'">
Previous Page</a>
'.$this->_Pagination_Seperator;
} else {
$Output .= '
Previous Page
'.$this->_Pagination_Seperator;
}
if(($this->_LimitPosition+$this->_Results_Per_Page) <= $this->Affected_Rows) {
$Output .= '
<a href="'.$_SERVER['PHP_SELF'].'?LimitPage='.($this->_LimitPosition+$this->_Results_Per_Page).'">
Next Page</a>
'.$this->_Pagination_Seperator;
} else {
$Output .= '
Next Page
';
}
BREAK;
}
}
return $Output;
}
}
?>