Ajax software
Free javascripts
↑
Main Page
// return the connection handler
return $db_link;
}
// close the database connection
function closeConnection($db_handler)
{
mysql_close($db_handler);
}
}
?>
11.
Save the following code in
include/catalog.inc.php
. This file contains three classes:
Brands
,
Categories
, and
Products
, which contain the functionality to read brand, category, and prod-
uct data from the database:
<?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;
}
269
Chapter 14: Case Study: Building an E-Commerce Store
c14.qxd:c14 10:46 269
Ajax software
Free javascripts
→