Managing Categories
When you compare the roles of authors and joke categories in thedatabase, they are really very similar. They both reside in tables of their
own, and they both serve to group jokes together in some way. As a result,
categories can be handled with almost the exact same code as we've developed
for authors, with one important exception.When we delete a category, we can't simultaneously delete any jokes
that belong to that category, because those jokes may also belong to other
categories. We could check each joke to see if it belonged to any other categories,
and only delete those that did not, but rather than engage in such a time-consuming
process, let's allow for the possibility of including jokes in our database
that don't belong to any category at all. These jokes would be invisible to
our site visitors, but would remain in the database in case we wanted to assign
them to a category later on.Thus, to delete a category, we also need to delete any entries in the JokeLookup table that refer to that category:
<!-- deletecat.php -->
...
// Delete all joke look-up entries for the
// category along with the entry for the category.
$ok1 = @mysql_query("DELETE FROM JokeLookup WHERE CID='$id'");
$ok2 = @mysql_query("DELETE FROM Categories WHERE ID='$id'");
...
Other than this one detail, category management is functionally identical
to author management. The code for cats.php, newcat.php, deletecat.php,
and editcat.php is provided in the code archive if you
need it.