Ajax software
Free javascripts
↑
Main Page
// 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();
270
Chapter 14: Case Study: Building an E-Commerce Store
c14.qxd:c14 10:46 270
Ajax software
Free javascripts
→