Displaying the Departments


Introduction.  Getting the departments to display is not likely as easy as you might think.  We are creating a site where things like the products, if not the departments, are constantly changing.  We do not want to create a page that lists the departments.  We need a page that will display the departments dynamically, that is as the information within our database changes.

We also have some other major things to consider.

  • make sure that the department selected is where the shopper goes next
  • make sure that the products in the department selected will display as they go
    • remembering that these products and their department assignments can be changing all the time

Now you need to copy the following code and name it departments.php.  After the code we give a brief discussion of its overall structure and content.


<!-- departments.php - Displays the departments in the store -->
<head><title>The Firefox Departments Page</title></head>
<font size=4 color="A66838"><b>Select from a department below:</b></font>
<table align = "center" border = 0>


// assign the table for database access
$table_name = "department";

// connecting to the database on battcave.com
$link = mysql_connect($host, $user, $password);
// constructing the query string
$query_string = "SELECT * FROM $table_name";
$result_set = mysql_db_query($db_name, $query_string, $link);

// looping through the resultset appending a new row in a table for each record
while ($row = mysql_fetch_array($result_set))

<td align=center valign=middle><font size=4> <a href="department_products.php?id_dept=<?php echo $row[id_department]; ?>"> <?php echo $row[department_name]; ?> </a></font></td>
<td align=center valign=middle><font size=4> <a href="department_products.php?id_dept=<?php echo $row[id_department]; ?>"><img src="images_dept/<?php echo $row[department_image]; ?>" border=0> </a></font></td>



Notice that I have included an HTML comment line in addition to the PHP comments.  You also want to make sure you distinguish our PHP scriptlets from the HTML.  We move back and forth quite a bit.
  • We start by including the header.php. 
  • Notice how HTML is used to give feedback to the user and start the table used for display.
  • Then we establish all the characteristics required to connect to our database.
  • We create the query string and execute it to create our resultset.
  • We then loop through this resultset while it has more entries appending a new row to the table for each department.
    • within this loop we
    • create links to department_products.php that will display all of the products carried within the department based on
      • department_name
      • department_image
    • but the actual department will be identified in the department_products.php by using the id_dept that is passed in the URL.
      • this id_dept is obtained from the departments table using the id_department field
    • notice how the path to the images is constructed
  • Then we close the link to the database.

More will be said about this after you upload this and get it to work.  Passing extra arguments in the URL is making use of a GET approach rather than the POST we have used so far to pass information to a subsequent PHP.

If we were clicking on department 2, the URL will look like the following on my computer.


The 2 is obtained from the departments table in your database and appended to the URL in the <a> anchor tag by echoing it within a PHP scriptlet.

You will get used to such trickery!