Edit Website Directories: Add 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 add a category to a website directory. In cms-creating-website-directories.html we saw how to create the website directory, and categories were added as one long comma-separated string that was converted into an array. Here we will add one category at a time.
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 a db table called categories—which has the fields category and id. If you don't have primary fields, your table will mess up, so we always stick one in, such as id. Don't use the main field as a primary. Note that the tables won't get created if they are already there.
Next comes the input filters. Category names must use alphanumeric or underscore characters—our preg_replace() sees to that. Notice that for security, we used strip_tags() and mysql_real_escape_string() on the categories as well although there's no way the latter is needed since all characters—like quotes—handled by it are dumped by the preg_replace() before it's run anyway but overcautiousness beats undercautiousness anyday. The strip_tags() is run before the replace so that the acceptable characters between tags, like the B between < and >, are dumped as well as the tags. If these latter were dumped in the preg_replace(), the in-the-tags debris would remain.
Next, the form for entering categories gets echoed onto the screen. Then we push the categories in the db table into an array—$cat[]. Next the PHP array cat[] is used to load a dropdown form displaying the current existing categories, using:
echo "<option value='".$cat[$i]."'>".$cat[$i]."</option>";. Before saving, the categories table is searched for the one entered in the form. If it already exists, a message reveals to the user that it exists—no one want duplicates in their directory. It it's new, the category gets saved in the categories table.
Finally, the HTML code gives users instructions and parameters for category entry.
SAVE THIS PAGE AS: cms-edit-website-directory-add-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)—Add Category</TITLE>
<meta name="description" content="Editing Website Directories—Content Management System (CMS)—Add Category">
<meta name="keywords" content="Add 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}
.form {position:absolute;top:140px;left:200px;width:700px}
.form2 {position:absolute;top:240px;left:200px;width:700px}
.info {position:absolute;top:80px;left:200px;width:600px;border:1px solid blue;padding:6px;background-color:#bbb}
.side {position:absolute;top:160px;left:715px;width:277px;padding:6px;background-color:#bbb;border:1px solid blue}
</style>
</head>
<body onload='fix()'>
<?php
include_once"config.php";
$pattern1 = '/[^a-zA-Z0-9\\_]/i';
$replacement = '';
$C=$_POST['category'];
$C=strip_tags($C);
$C=preg_replace($pattern1, $replacement, $C);
$C=mysql_real_escape_string($C);
echo "<div id='form' class='form'>
<form name='myform' method='post' action='cms-edit-website-directory-add-category.php'>
<table width='700' border='0' cellpadding='2' cellspacing='2' align='center'>
<tr>
<td width='60'>New Category</td><br>
<td><input name='category' type='text' size='29'></input></td>
</tr>
<tr>
<td> </td><td><input name='save' type='submit' value='Save Category to DB'>
<input name='reset' type='reset' value='Reset'></td></tr>
</table>
</form>
</div>";
$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=' '><table width='700' border='0' cellpadding='2' cellspacing='2' align='center'><td width='130'>Existing categories</td><td><select name='cy'>";
for ($i=0;$i<$num_cats_in_table;$i++) {
echo "<option value='".$cat[$i]."'>".$cat[$i]."</option>";}
echo "</select></td></tr></table></form></div>";
if (strlen($C)>0){
$check_user_data = mysql_query("SELECT category FROM categories WHERE category='$C'") or die(mysql_error());
if(mysql_num_rows($check_user_data) >0)
{echo '<script language="javascript">alert("This category exists. Please try again.")</script>;';
}else{
mysql_query("INSERT INTO categories (id, category)
VALUES ('','".$C."')") or die('Error ,saving failed');
$rc = mysql_affected_rows();
if ($rc>0){echo '<script language="javascript">alert("The saving was successfully accomplished.");window.location = "cms-edit-website-directory-add-category.php";</script>';}
else{echo '<script language="javascript">alert("The saving was unsuccessful.");</script>';}
}}
mysql_close();
?>
<div id='top' class='url'>
<h1>Editing Website Directories—Content Management System (CMS)—Add Category</h1>
<div id='info' class='info'><h3>Use letters, numbers, and underscores only in categories. Categories must be under 30 characters long.</h3></div>
</div>
<?php include("nav.html"); ?>
</body>
</html>