Chapter 4: Creating the Main Web Site
100
First, use the third URI segment (which represents a product ID) to extract information about that
product with the getProduct() model function.
If no product is retrieved (because it isn ’ t active or it doesn ’ t exist), then redirect the user back to the
home page. This is a very simple way to ensure that only active (or extant) products are shown. In
Chapter 9 , you learn how to further defend against various kinds of attacks involving user input
like URIs.
Otherwise, retrieve other products with the same group, and use $this - > load - > vars() to load the
$data array for use by all the views. Along the way, set the $data[ ’ main ’ ] variable to “ product, ” as
you ’ ll be using the product view in this context.
Creating the Product View
The view for products, called product.php, is very similar to the home page in form, function, and
output. It accepts two arrays, one called $products and the other called $grouplist , and outputs the
information in pleft and pright divs:
< div id=’pleft’ >
< ?php
echo “ < img src=’”.$product[‘image’].”’ border=’0’ align=’left’/ > \n”;
echo “ < h2 > ”.$product[‘name’].” < /h2 > \n”;
echo “ < p > ”.$product[‘longdesc’]. “ < br/ > \n”;
echo “Colors: < br/ > \n”;
echo “Sizes: < br/ > \n”;
echo anchor(‘welcome/cart/’.$product[‘id’],’buy now’). “ < /p > \n”;
? >
< /div >
< div id=’pright’ >
< ?php
foreach ($grouplist as $key = > $list){
echo “ < img src=’”.$list[‘thumbnail’].”’ border=’0’ align=’left’/ > \n”;
echo “ < h4 > ”.$list[‘name’].” < /h4 > \n”;
echo “ < p > ”;
echo anchor(‘welcome/product/’.$list[‘id’],’see details’). “ < br/ > \n”;
echo anchor(‘welcome/cart/’.$list[‘id’],’buy now’). “ < /p > \n”;
}
? >
< /div >
With this view built and uploaded, reload your browser to see the results. You should see something
similar to Figure 4 - 11. Again, it won ’ t win any awards, but it functions. Navigate the site to other
products within the same group (and even different groups) to check the validity of your code.