JavaScript Editor Ajax software     Free javascripts 



Main Page

// 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();
271
Chapter 14: Case Study: Building an E-Commerce Store
c14.qxd:c14 10:46 271


JavaScript Editor Ajax software     Free javascripts