File size: 5.27Kb
<?php
include("dotwidgeta_config.php"); // change if the config file is not in admin directory
include("connect_db.php");
$metatitle = $sitename . " - Manage Categories";
include($admin_header_file);
// -------------------------------------------------------------
// List the categories
// -------------------------------------------------------------
if($action == "") {
print "<blockquote><p class=\"PAGEHEADING\">Admin: Categories List";
print "<P><A HREF=\"index.php\">Main Menu</A> | <A HREF=\"categories.php?action=add\">Add a Category</A></p>";
print "<p><span class=\"TIPLABEL\">Tip</span><span class=\"TIPTEXT\">: <i>Careful!</i> A \"Delete\" is final! You click and it's gone!</span></p><br>";
$cats = mysql_query("SELECT id, name FROM article_cats ORDER BY name");
if (!$cats) {
echo("<P>Error retrieving categories from database!<BR>".
"Error: " . mysql_error());
exit();
}
echo("<table border=1 cellpadding=5>");
while ($cat = mysql_fetch_array($cats)) {
$id = $cat["id"];
$name = $cat["name"];
echo("<tr><td>$name </td>\n".
"<td><A HREF='categories.php?action=edit&id=$id'>Edit</A> | ".
"<A HREF='categories.php?action=del&id=$id'>Delete</A></td></tr>\n");
}
echo("</tr></table><br>");
// -------------------------------------------------------------
// Add a category
// -------------------------------------------------------------
} if($action == "add") {
print "<blockquote><p class=\"PAGEHEADING\">Admin: Add a Category";
print "<P><A HREF=\"index.php\">Main Menu</A> | <A HREF=\"categories.php\">Categories List</A> | <A HREF=\"categories.php?action=add\">Add a Category</A>";
if ($submit): // A new category has been entered
// using the form below.
$sql = "INSERT INTO article_cats SET " .
"name='$name'";
if (mysql_query($sql)) {
echo("<P>New category added</P><br>");
} else {
echo("<P>Error adding new category: " .
mysql_error() . "</P><br>");
}
else: // Allow the user to enter a new category
?>
<p><span class="TIPLABEL">Tip</span><span class="TIPTEXT">: Review current list of categories before adding another.</span></p>
<FORM ACTION="<?php echo($PHP_SELF."?action=add"); ?>" METHOD=POST>
<P>New category name: <INPUT TYPE=TEXT NAME="name" SIZE=20 MAXLENGTH=100><BR><br>
<INPUT TYPE=SUBMIT NAME="submit" VALUE="SUBMIT"></P>
</FORM>
<?php endif;
// -------------------------------------------------------------
// Edit a category
// -------------------------------------------------------------
} elseif($action == "edit") {
print "<blockquote><p class=\"PAGEHEADING\">Admin: Edit Categories";
print "<P><A HREF=\"index.php\">Main Menu</A> | <A HREF=\"categories.php\">Categories List</A> | <A HREF=\"categories.php?action=add\">Add a Category</A>";
if ($submit) {
$sql = "UPDATE article_cats SET " .
"name='$name', " .
"description='$description' " .
"WHERE id=$id";
if (mysql_query($sql)) {
echo("<P>Category details updated.</P><BR>");
} else {
echo("<P>Error updating category details: " .
mysql_error() . "</P><BR>");
}
} else {
$cat=mysql_query("SELECT name, description " .
"FROM article_cats " .
"WHERE id=$id");
if (!$cat) {
echo("<P>Error fetching category details: " .
mysql_error() . "</P>");
exit();
}
$cat = mysql_fetch_array($cat);
$name = $cat["name"];
$description = $cat["description"];
// Add slashes to database
// value for use as HTML attribute
$name = addslashes($name);
$description = addslashes($description);
?>
<FORM ACTION="<?php echo($PHP_SELF."?action=edit"); ?>" METHOD=POST>
<P><B>Edit the category:</B><BR><BR>
<TABLE>
<TR>
<TD>Name:</TD>
<TD><INPUT TYPE=TEXT NAME="name" VALUE="<?php echo($name); ?>" SIZE=20 MAXLENGTH=100></TD>
</TR>
<TR>
<TD>Description:</TD>
<TD><TEXTAREA NAME="description" ROWS=3 COLS=45><?php echo($description); ?></TEXTAREA></TD>
</TR>
<TR>
<TD> </TD>
<TD>
<INPUT TYPE=HIDDEN NAME="id" VALUE="<?php echo($id); ?>"><BR>
<INPUT TYPE=SUBMIT NAME="submit" VALUE="SUBMIT">
</TD>
</TR>
</TABLE>
</FORM><BR>
<?php
}
// -------------------------------------------------------------
// Delete a category
// -------------------------------------------------------------
} elseif($action == "del") {
print "<blockquote><p class=\"PAGEHEADING\">Admin: Delete Category";
print "<P><A HREF=\"index.php\">Main Menu</A> | <A HREF=\"categories.php\">Categories List</A> | <A HREF=\"categories.php?action=add\">Add a Category</A>";
$ok1 = mysql_query("DELETE FROM article_lookup WHERE CID=$id");
$ok2 = mysql_query("DELETE FROM article_cats WHERE id=$id");
if ($ok1 and $ok2) {
echo("<P>Category deleted successfully!</P><BR>");
} else {
echo("<P>Error deleting category from database!<BR>".
"Error: " . mysql_error());
}
}
// -------------------------------------------------------------
// Call the footer file
// -------------------------------------------------------------
include($admin_footer_file);
?>