Forum Moderators: coopster
<?php
// load configuration file
require_once('config.inc.php');
// load database tools
require_once('database_tools.inc.php');
// Database class for handling brands
class Brands
{
// retrieve cloaking data filtered by the supplied parameters
function get($id = 0, $name = '', $order_by = '', $order_dir = '')
{
// by default, retrieve all records
$q = " SELECT brands.* FROM brands WHERE TRUE ";
// filter by brand ID
if ($id) {
$id = (int) $id;
$q .= " AND id = $id ";
}
// filter by brand name
if ($name) {
$name = mysql_escape_string($name);
$q .= " AND name = '$name' ";
}
// add sorting options
if ($order_by) {
if ($order_dir !== '' && !$order_dir) {
$order_q = ' DESC ';
} else {
$order_q = ' ';
}
$q .= " ORDER BY " . db_identifier($order) . $order_q;
}
// get a database connection
$db_link = DatabaseTools::getConnection();
// execute the query
$query_results = mysql_query($q);
// close database connection
DatabaseTools::closeConnection($db_link);
// return the results as an associative array
$rows = array();
while ($result = mysql_fetch_assoc($query_results)) {
$rows[] = $result;
}
return $rows;
}
}
// Database class for handling categories
class Categories
{
// retrieves categories data filtered by the supplied parameters
function get($id = 0, $name = '', $order_by = '', $order_dir = '')
{
// by default, retrieve all records
$q = " SELECT categories.* FROM categories WHERE TRUE ";
// filter by category id
if ($id) {
$id = (int) $id;
$q .= " AND id = $id ";
}
// filter by category name
if ($name) {
$name = mysql_escape_string($name);
$q .= " AND name = '$name' ";
}
// add sorting options
if ($order_by) {
if ($order_dir !== '' && !$order_dir) {
$order_q = ' DESC ';
} else {
$order_q = ' ';
}
$q .= " ORDER BY " . DatabaseTools::dbIdentifier($order_by) . $order_q;
}
// get a database connection
$db_link = DatabaseTools::getConnection();
// execute the query
$query_results = mysql_query($q);
// close database connection
DatabaseTools::closeConnection($db_link);
// return the results as an associative array
$rows = array();
while ($result = mysql_fetch_assoc($query_results)) {
$rows[] = $result;
}
return $rows;
}
}
// Database class for handling products
class Products
{
// retrieves products data filtered by the supplied parameters
function get($id = 0, $category_id = 0, $brand_id = 0, $name = '')
{
// by default, retrieve all records
$q = "SELECT products.* FROM products WHERE TRUE ";
// filter by ID if the $id parameter was provided
if ($id) {
$id = (int) $id;
$q .= " AND id = $id ";
}
// filter by product name if the $name parameter was provided
if ($name) {
$name = mysql_escape_string($name);
$q .= " AND name = '$name' ";
}
// filter by category ID if the category_id parameter was provided
if ($category_id) {
$category_id = (int) $category_id;
$q .= " AND (SELECT COUNT(*) FROM product_categories " .
" WHERE product_categories.product_id = products.id " .
" AND product_categories.category_id = $category_id) ";
}
// filter by brand ID if the $brand_id parameter was provided
if ($brand_id) {
$brand_id = (int) $brand_id;
$q .= " AND brand_id = $brand_id ";
}
// get a database connection
$db_link = DatabaseTools::getConnection();
// execute the query
$query_results = mysql_query($q);
// close database connection
DatabaseTools::closeConnection($db_link);
// return the results as an associative array
$rows = array();
while ($result = mysql_fetch_assoc($query_results)) {
$rows[] = $result;
}
return $rows;
}
}
?>