Ajax software
Free javascripts
↑
Main Page
Figure 14-5
When it comes to the code, you’ll find most of it familiar from the previous chapters. This section focuses
only on the new details.
The functions you’re using to display catalog data are located in the
catalog.inc.php
file. There you
can find the
Brands
,
Categories
, and
Products
classes — each of these classes have a method called
get()
. The various parameters of
get()
allow you to either retrieve the complete list of brands, cate-
gories, and products, or to filter the results according to various criteria.
The
get()
methods return an associate array with the query results. If you’re expecting a single result,
then you need to read the first element of the returned array. Take the following example, which uses
Products::get()
to retrieve the details of a single product:
// retrieve the product details
$product_id = $_GET[‘product_id’];
$products = Products::get($product_id);
$product = $products[0];
You can see the same function called in
category.php
to retrieve all the products in a category, like this:
// retrieve the products in the category
$products = Products::get(0, $category_id);
The other new material in this chapter is the implementation of the pager library. This library, located in
simple_pager.inc.php
, contains the basic functionality for displaying a pager. You use this function-
ality in the category pages, where you want to display a limited number of products per page. You can
set the number of products by editing the
PRODUCTS_PER_PAGE
constant in
config.inc.php
. For the
purposes of this test this value is set to 1, but feel free to change it to any value:
// defines the number of products for paging
define(‘PRODUCTS_PER_PAGE’, 1);
A number of settings related to the pager are hard-coded in the constructor of the
SimplePager
class —
depending on how flexible you want this functionality to be, you may want to make these configurable.
Also, in a professional implementation you would need to add support for including CSS styles in the
pager output.
products
PK id
brand_id
name
price
desc
primary_category_id
brands
id
name
categories
PK id
name
product_categories
PK
PK
product_id
category_id
PK
280
Chapter 14: Case Study: Building an E-Commerce Store
c14.qxd:c14 10:46 280
Ajax software
Free javascripts
→