section.k.index % 2 == 0}
{/if}
{if $obj->mProducts[k].thumbnail neq ""}
alt="{$obj->mProducts[k].name}" />
{/if} {$obj->mProducts[k].description}
CHAPTER 5 ?– CREATING THE PRODUCT CATALOG: PART 2 158
Price: {if $obj->mProducts[k].discounted_price != 0} {$obj->mProducts[k].price} {$obj->mProducts[k].discounted_price} {else} {$obj->mProducts[k].price} {/if}
|
{if $smarty.section.k.index % 2 != 0 && !$smarty.section.k.first ||
$smarty.section.k.last}
{/if}
{/section}
{/if}
4. Now, you must create the presentation object file for the products_list.tpl template. Create a new file
named products_list.php in the presentation folder, and add the following code to it:
class ProductsList
{
// Public variables to be read from Smarty template
public $mPage = 1;
public $mrTotalPages;
public $mLinkToNextPage;
public $mLinkToPreviousPage;
public $mProducts;
// Private members
private $_mDepartmentId;
private $_mCategoryId;
// Class constructor
public function __construct()
{
// Get DepartmentId from query string casting it to int
if (isset ($_GET['DepartmentId']))
$this->_mDepartmentId = (int)$_GET['DepartmentId'];
// Get CategoryId from query string casting it to int
if (isset ($_GET['CategoryId']))
$this->_mCategoryId = (int)$_GET['CategoryId'];
// Get Page number from query string casting it to int
CHAPTER 5 ?– CREATING THE PRODUCT CATALOG: PART 2 159
if (isset ($_GET['Page']))
$this->mPage = (int)$_GET['Page'];
if ($this->mPage < 1)
trigger_error('Incorrect Page value');
}
public function init()
{
/* If browsing a category, get the list of products by calling
the GetProductsInCategory() business tier method */
if (isset ($this->_mCategoryId))
$this->mProducts = Catalog::GetProductsInCategory(
$this->_mCategoryId, $this->mPage, $this->mrTotalPages);
/* If browsing a department, get the list of products by calling
the GetProductsOnDepartment() business tier method */
elseif (isset ($this->_mDepartmentId))
$this->mProducts = Catalog::GetProductsOnDepartment(
$this->_mDepartmentId, $this->mPage, $this->mrTotalPages);
/* If there are subpages of products, display navigation
controls */
if ($this->mrTotalPages > 1)
{
// Build the Next link
if ($this->mPage < $this->mrTotalPages)
{
if (isset($this->_mCategoryId))
$this->mLinkToNextPage =
Link::ToCategory($this->_mDepartmentId, $this->_mCategoryId,
$this->mPage + 1);
elseif (isset($this->_mDepartmentId))
$this->mLinkToNextPage =
Link::ToDepartment($this->_mDepartmentId, $this->mPage + 1);
}
// Build the Previous link
if ($this->mPage > 1)
{
if (isset($this->_mCategoryId))
$this->mLinkToPreviousPage =
Link::ToCategory($this->_mDepartmentId, $this->_mCategoryId,
$this->mPage - 1);
elseif (isset($this->_mDepartmentId))
$this->mLinkToPreviousPage =
Link::ToDepartment($this->_mDepartmentId, $this->mPage - 1);
}
}
CHAPTER 5 ?– CREATING THE PRODUCT CATALOG: PART 2 160
// Build links for product details pages
for ($i = 0; $i < count($this->mProducts); $i++)
{
$this->mProducts[$i]['link_to_product'] =
Link::ToProduct($this->mProducts[$i]['product_id']);
if ($this->mProducts[$i]['thumbnail'])
$this->mProducts[$i]['thumbnail'] =
Link::Build('product_images/' .
Pages:
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261