Edit Website Directories: Delete Category—Content Management System (CMS)
The cool thing about making website directories is that they can be very useful when you need to go somewhere on the Web but you're not sure of the name. We use our directory as a programmer's resource which is better than Favorites in a browser because you can get a thumbnail and description and other info dynamically just from hovering over a link, without having to click on the link and go there (which you can also do, of course).
You may, of course, choose to leave out the code about thumbnails and dynamic info boxes and just have clickable links for the categories and the URLs, or store your own thumbnails of sites in an images folder and your own descriptions in MySQL fields. You'll need the PrintScreen key for screen capture and a graphics program like ImageForge or at least Irfanview for the thumbnails data.
This page will give you the code needed to delete a category from a website directory. In cms-creating-website-directories.html we saw how to create the website directory, and categories were added all at one time, comma-separated. We'll dump only one at a time with deletion.
We assume you have access to a MySQL database and permission and password and user name, and you know the db name. We assume you know what is in a config file, since the PHP on this page uses include_once"config.php". If this is confusing, see the-configure-file.html. If you wish to see our directory demo, which shows how the directory looks and works, go to cms-view-website-directory-demo.html.
So let's peruse the code. There is no input filtering since users merely select the category to delete from a dropdown list. There is a db table called categories—which has the fields category and id. In deleting, the category and id fields add up to one row or record, and that is what will get deleted. However, it's more complex than the script for deleting URLs where records in the urls table with the input url get deleted and that's that. In this script, you must choose whether you want to delete the category (in both the categories and urls tables) or replace it with a different category (in the urls table only, since in the categories table it's just deleted). The form that gets echoed to the screen has radio buttons where you must choose which way you wish to go. Users select the category to delete from a dropdown list and also select the category to replace it with (in the urls table) from a dropdown list. The latter gets ignored if they've chosen deletion without replacing.
Finally, the HTML code gives users the instructions for selecting deletion categories and replacement categories in these terms: "To delete a category, choose whether to delete all URLs in that category along with the category itself, or select a different category to place these URLs in before deleting the category".
SAVE THIS PAGE AS: cms-edit-website-directory-delete-category.php
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>Editing Website Directories—Content Management System (CMS)—Delete Category</TITLE>
<meta name="description" content="Editing Website Directories—Content Management System (CMS)—Delete Category">
<meta name="keywords" content="Delete Category,Editing Website Directories,Content Management System,Content Management System Articles,php,CMS,javascript, dhtml, DHTML">
<script language="javascript">
var cat=new Array();
mactest=(navigator.userAgent.indexOf("Mac")!=-1) //My browser sniffers
is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1
Netscape=(navigator.appName.indexOf("Netscape") != -1)
msafari=(navigator.userAgent.indexOf("Safari")!= -1)
wsafari=0; if(!mactest&&msafari){wsafari=1;msafari=0}
is_opera = 0; if(window.opera){is_opera=1}
is_ie_mac = 0; is_ie=0;if(document.all){is_ie=1}
if(is_ie&&mactest){is_ie_mac=1}
function fix(){if(Netscape||is_opera){e=document.getElementById('top');e.style.marginTop='1px';e=document.getElementById('info');e.style.marginTop='1px';}}
</script>
<style type="text/css">
BODY {margin-left:0; margin-right:0; margin-top:0;text-align:left;background-color:#ddd}
p, li {font:13px Verdana; color:black;text-align:left}
h1 {font:bold 20px Verdana; color:black;text-align:center}
h2 {font:bold 24px Verdana;text-align:center}
h3 {font:bold 11px Verdana;}
.url {position:absolute;top:0px;left:10px;width:989px}
.form2 {position:absolute;top:240px;left:215px;width:700px}
.info {position:absolute;top:80px;left:200px;width:700px;border:1px solid blue;padding:6px;background-color:#bbb}
.options {width:700px;padding:6px;border:1px solid blue;background-color:#bbb}
</style>
</head>
<body onload='fix()'>
<?php
include_once"config.php";
$C=$_POST['category'];
$R=$_POST['replace'];
$RD=$_POST['replace_delete'];
$cat=array();
$res = mysql_query("SELECT category FROM categories ORDER BY category") or die(mysql_error());
while ($row = mysql_fetch_row($res)) {
array_push ($cat, $row[0]);
}
$num_cats_in_table=mysql_num_rows($res);
echo "<div class='form2'><form name='myform2' method='post' action='cms-edit-website-directory-delete-category.php''><table width='700' border='0' cellpadding='2' cellspacing='2' align='center'><div class='options'>
<b> Options: </b>
<input type='radio' name='replace_delete' value='replace_category' checked> replace category with selected one </b>
<input type='radio' name='replace_delete' value='delete_category'> delete category and its URLs<br></div>
<tr><tr><td width='540'><b>Category to delete or replace</b></td><td><select name='category'>";
for ($i=0;$i<$num_cats_in_table;$i++) {
echo "<option value='".$cat[$i]."'>".$cat[$i]."</option>";}
echo "</select></td></tr>
<tr><td width='540'><b>Category to replace it with (IF you are replacing it—if not, this will be ignored)</b></td><td><select name='replace'>";
for ($i=0;$i<$num_cats_in_table;$i++) {
echo "<option value='".$cat[$i]."'>".$cat[$i]."</option>";}
echo "</select></td></tr><tr>
<td><input name='submit' type='submit' value='Replace or Delete Category in DB'> <br><br>
<input name='reset' type='reset' value='Reset'></td>
</tr></table></form></div>";
if ($RD=="replace_category"){
mysql_query("UPDATE urls SET category='$R' WHERE category='$C'") or die('Error ,replacing failed');
mysql_query("DELETE FROM categories WHERE category='$C'") or die('Error ,category deleting failed');
$rc = mysql_affected_rows();
if ($rc>0){echo '<script language="javascript">alert("The replacing was successful.");window.location = "cms-edit-website-directory-delete-category.php";</script>';}
else{echo '<script language="javascript">alert("The replacing was unsuccessful.");</script>';}
}
if ($RD=="delete_category"){
mysql_query("DELETE FROM urls WHERE category='$C'") or die('Error ,deleting failed');
mysql_query("DELETE FROM categories WHERE category='$C'") or die('Error ,deleting failed');
$rc = mysql_affected_rows();
if ($rc>0){echo '<script language="javascript">alert("The deleting was successful.");window.location = "cms-edit-website-directory-delete-category.php";</script>';}
else{echo '<script language="javascript">alert("The deleting was unsuccessful.");</script>';}
}
mysql_close();
?>
<div id='top' class='url'>
<h1>Editing Website Directories—Content Management System (CMS)—Delete Category</h1>
<div id='info' class='info'><h3>To delete a category, choose whether to delete all URLs in that category along with the category itself, or select a different category to place these URLs in before deleting the category.</h3></div>
</div>
<?php include("nav.html"); ?>
</body>
</html>