Displaying the Departments
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.
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.
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!