Displaying the Products in each Department
Introduction.
Getting the products in each department to display is also 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 products for each department. We need a page that will
display the products in a department 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 department_products.php. This code is somewhat more involved because we need to make it depend on the department the client selected and display all of the products within the department. After this code we give a brief discussion of its overall structure and content. |
<HTML> <!-- department_products.php - Displays the products in each department --> <head><title>The Firefox Products within a Department Page</title></head> <?php include("includes/header.php"); include("includes/connection_config.php"); ?> <br><br> <table align = "center" border = 0 cellpadding="5"> <?php // obtaining the id of the department // that was selected on departments.php $id_department = $_REQUEST["id_dept"]; // 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 to retrieve information about the department $query_dept_string = "SELECT * FROM $table_name WHERE id_department = $id_department"; $result_set = mysql_db_query($db_name, $query_dept_string, $link); // obtaining the name of the department while ($row = mysql_fetch_array($result_set)) { ?> <tr > <td align=center colspan=2><font size=6 color="A66838"><i><b><?php echo $row[department_name]; ?></i></b></font><br></td> </tr> <tr> <td align=center colspan=2><font size=4 color="c0c0c0"><?php echo $row[department_description]; ?></font><br></td> </tr> <?php } // constructing the query string to retrieve // the products within this department $query_prod_string = "SELECT * FROM products, department_products WHERE products.id_product = department_products.id_product AND department_products.id_department = $id_department"; $result_set = mysql_db_query($db_name, $query_prod_string, $link); // looping through the resultset appending a new row for each record while ($row = mysql_fetch_array($result_set)) { ?> <tr> <td align=right valign=middle><font size=4> <a href="product.php?id_prod=<?php echo $row[id_product]; ?>"><?php echo $row[product_name]; ?> </a></font></td> <td align=center valign=middle><font size=4> <a href="product.php?id_prod=<?php echo $row[id_product]; ?>"><img src="images_prod/sm_<?php echo $row[product_image]; ?>" border=0> </a></font></td> </tr> <?php } mysql_close($link); ?> </table> </BODY> </HTML> |
As usual 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 product numbered 4 in department 2, the URL will look like the following on my computer. http://gesaighu.net/firefox/product.php?id_prod=4 The 4 is obtained from the products 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! |