Specifications

In our case, we will return this array back all the way to index.php, where we pass it to the
display_categories() function from output_fns.php. This function displays each category
as a link to the page containing the books in that category. The code for this function is shown
in Listing 25.5.
LISTING 25.5 display_categories() Function from output_fns.phpFunction That Displays
an Array of Categories as a List of Links to Those Categories
function display_categories($cat_array)
{
if (!is_array($cat_array))
{
echo “No categories currently available<br>”;
return;
}
echo “<ul>”;
foreach ($cat_array as $row)
{
$url = “show_cat.php?catid=”.($row[“catid”]);
$title = $row[“catname”];
echo “<li>”;
do_html_url($url, $title);
}
echo “</ul>”;
echo “<hr>”;
}
This function converts each category from the database into a link. Each link goes to the next
scriptshow_cat.phpbut each has a different parameter, the category ID or catid. (This is
a unique number, generated by MySQL, and used to identify the category.)
This parameter to the next script will determine which category we end up looking at.
Listing Books in a Category
The process for listing books in a category is similar. The script that does this is called
show_cat.php. It is shown in Listing 25.6.
LISTING 25.6 show_cat.phpThis Script Shows the Books in a Particular Category
<?
include (‘book_sc_fns.php’);
// The shopping cart needs sessions, so start one
session_start();
Building a Shopping Cart
C
HAPTER 25
25
B
UILDING A
SHOPPING CART
553
31 7842 CH25 3/6/01 3:38 PM Page 553