only for RuBoard - do not distribute or recompile Previous Section Next Section

Implementing the Online Catalog

Three catalog scripts are in this application: the main page, the category page, and the book details page.

The front page of the site is produced by the script called index.php. The output of this script is shown in Figure 25.3.

Figure 25.3. The front page of the site lists the categories of books available for purchase.
graphics/25fig03.gif

You'll notice that, in addition to the list of categories on the site, there is a link to the shopping cart in the top-right corner of the screen and some summary information about what's in the cart. This will appear on every page while a user browses and shops.

If a user clicks one of the categories, she'll be taken to the category page, produced by the script show_cat.php. The category page for the Internet books section is shown in Figure 25.4.

Figure 25.4. Each book in the category is listed with a photo.
graphics/25fig04.gif

All the books in the Internet category are listed as links. If a user clicks one of these links, she will be taken to the book details page. The book details page for one book is shown in Figure 25.5.

Figure 25.5. Each book has a details page that shows more information including a long description.
graphics/25fig05.gif

On this page, as well as the View Cart link, we have an Add to Cart link in which the user can select an item. We'll return to that when we look at how to build the shopping cart later.

Let's look at each of these three scripts.

Listing Categories

The first script, index.php, lists all the categories in the database. It is shown in Listing 25.2.

Listing 25.2 index.php—Script to Produce the Front Page of the Site
<?
  include ('book_sc_fns.php');
  // The shopping cart needs sessions, so start one
  session_start();
  do_html_header("Welcome to Book-O-Rama");

  echo "<p>Please choose a category:</p>";

  // get categories out of database
  $cat_array = get_categories();

  // display as links to cat pages
  display_categories($cat_array);

  // if logged in as admin, show add, delete, edit cat links
  if(session_is_registered("admin_user"))
  {
    display_button("admin.php", "admin-menu", "Admin Menu");
  }
  
  do_html_footer();
?>

The script begins by including book_sc_fns.php, the file that includes all the function libraries for this application.

After that, we must begin a session. This will be required for the shopping cart functionality to work. Every page in the site will use the session.

There are some calls to HTML output functions such as do_html_header() and do_html_footer() (both contained in output_fns.php).

We also have some code that checks if the user is logged in as an administrator and gives her some different navigation options if she is—we'll return to this in the section on the administration functions.

The most important part of this script is

						
// get categories out of database
$cat_array = get_categories();

// display as links to cat pages
display_categories($cat_array);

					

The functions get_categories() and display_categories() are in the function libraries book_fns.php and output_fns.php, respectively. The function get_categories() returns an array of the categories in the system, which we then pass to display_categories(). Let's look at the code for get_categories(). It is shown in Listing 25.3.

Listing 25.3 get_categories() Function from output_fns.php—Function That Retrieves a Category List from the Database
function get_categories()
{
   // query database for a list of categories
   $conn = db_connect();
   $query = "select catid, catname
             from categories";
   $result = @mysql_query($query);
   if (!$result)
     return false;
   $num_cats = @mysql_num_rows($result);
   if ($num_cats ==0)
      return false;
   $result = db_result_to_array($result);
   return $result;
}

As you can see, this function connects to the database and retrieves a list of all the category IDs and names. We have written and used a function called db_result_to_array(), located in db_fns.php. This function is shown in Listing 25.4. It takes a MySQL result identifier and returns a numerically indexed array of rows, where each row is an associative array.

Listing 25.4 db_result_to_array() Function from db_fns.php—Function That Converts a MySQL Result Identifier into an Array of Results
function db_result_to_array($result)
{
   $res_array = array();

   for ($count=0; $row = @mysql_fetch_array($result); $count++)
     $res_array[$count] = $row;

   return $res_array;
}

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.php—Function 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 script—show_cat.php—but 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.php—This Script Shows the Books in a Particular Category
<?
  include ('book_sc_fns.php');
  // The shopping cart needs sessions, so start one
  session_start();
  $name = get_category_name($catid);

  do_html_header($name);

  // get the book info out from db
  $book_array = get_books($catid);

  display_books($book_array);


  // if logged in as admin, show add, delete book links
  if(session_is_registered("admin_user"))
  {
    display_button("index.php", "continue", "Continue Shopping");
    display_button("admin.php", "admin-menu", "Admin Menu");
    display_button("edit_category_form.php?catid=$catid", "edit-category",
                   "Edit Category");
  }
  else
    display_button("index.php", "continue-shopping", "Continue Shopping");

  do_html_footer();
?>

This script is very similar in structure to the index page, with the difference being that we are retrieving books instead of categories.

We start with session_start() as usual, and then convert the category ID we have been passed into a category name using the get_category_name() function as follows:

						
$name = get_category_name($catid);

					

This function looks up the category name in the database. It is shown in Listing 25.7.

Listing 25.7 get_category_name() Function from book_fns.php—This Function Converts a Category ID to a Category Name
function get_category_name($catid)
{
   // query database for the name for a category id
   $conn = db_connect();
   $query = "select catname
             from categories
             where catid = $catid";
   $result = @mysql_query($query);
   if (!$result)
     return false;
   $num_cats = @mysql_num_rows($result);
   if ($num_cats ==0)
      return false;
   $result = mysql_result($result, 0, "catname");
   return $result;
}

After we have retrieved the category name, we can render an HTML header and proceed to retrieve and list the books from the database that fall into our chosen category, as follows:

						
$book_array = get_books($catid);
display_books($book_array);

					

The functions get_books() and display_books() are extremely similar to the get_categories() and display_categories() functions, so we will not go into them here. The only difference is that we are retrieving information from the books table rather than the categories table.

The display_books() function provides a link to each book in the category via the show_book.php script. Again, each link is suffixed with a parameter. This time around, it's the ISBN for the book in question.

At the bottom of the show_cat.php script, you will see that there is some code to display some additional functions if an administrator is logged in. We will look at these in the section on administrative functions.

Showing Book Details

The show_book.php script takes an ISBN as a parameter and retrieves and displays the details of that book. The code for this script is shown in Listing 25.8.

Listing 25.8 show_book.php—This Script Shows the Details of a Particular Book
<?
  include ('book_sc_fns.php');
  // The shopping cart needs sessions, so start one
  session_start();

  // get this book out of database
  $book = get_book_details($isbn);
  do_html_header($book["title"]);
  display_book_details($book);

  // set url for "continue button"
  $target = "index.php";
  if($book["catid"])
  {
    $target = "show_cat.php?catid=".$book["catid"];
  }
  // if logged in as admin, show edit book links
  if( check_admin_user() )
  {
    display_button("edit_book_form.php?isbn=$isbn", "edit-item", "Edit Item");
    display_button("admin.php", "admin-menu", "Admin Menu");
    display_button($target, "continue", "Continue");
  }
  else
  {
    display_button("show_cart.php?new=$isbn", "add-to-cart",
                  "Add ".$book["title"]." To My Shopping Cart");
    display_button($target, "continue-shopping", "Continue Shopping");
  }
  
  do_html_footer();
?>

Again with this script, we are doing very similar things as in the previous two pages. We begin by starting the session, and then use

						
$book = get_book_details($isbn);

					

to get the book information out of the database, and

						
display_book_details($book);

					

to output the data in HTML.

One thing to note here is that display_book_details() looks for an image file for the book as images/$isbn.jpg. If this file does not exist, no image will be displayed.

The remainder of the script sets up navigation. A normal user will have the choices Continue Shopping, which will take her back to the category page, and Add to Cart, which will add the book to her shopping cart. If a user is logged in as an administer, she will get some different options, which we'll look at in the section on administration.

That completes the basics of the catalog system. Let's go ahead and look at the code for the shopping cart functionality.

only for RuBoard - do not distribute or recompile Previous Section Next Section