Creating Website Directories—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. You'll need the PrintScreen key for screen capture and a graphics program like ImageForge or at least Irfanview.
This page will give you the code needed to create a website directory. 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. As you can see, at first you see only a list of categories. When you choose a category, you see a list of the links in that category. There are 2 columns, Thumbnails and URLs. The first column has links which, if hovered, give a thumbnail of the site. The second column spells out the URL. The 4th link under the XHTML category shows the maximum length of the URL. If you try to make them longer, the URL will be 2 rows tall and the links after that one will not produce the correct info boxes when hovered with the cursor. The PHP category shows that if your URL list requires scrolling, that will work out okay.
So let's peruse the code. We create db tables called categories—which has the fields category and id—and urls—which has the fields category and url 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 or comma. The reason for the allowing of the comma is that categories will be entered in one input box as a comma-separated entry (not in separate rows or Return separated). The url filter is more lenient, allowing whatever can be in a url. The 047 is a single quote that you may choose to leave out along with the 2 backslashes before it, for security reasons. Notice that also for security, we used strip_tags() and mysql_real_escape_string() on the urls and categories as well.
Next, if there is no evidence they've input a string of categories, the form for entering categories gets echoed onto the screen. The example shown is: fish,birds,mammals,insects. Note that explode(",", $C) dumps the categories into a nice, convenient array—$ct[]. In a FOR loop, the categories are stored in the db table. Then we push the categories in the table into an array—$cat[]. Next we create the form for URL entry. It contains a dropdown options list where categories may be selected for each URL entered. This gets created here on the fly. And the PHP array cat[] is used to load it:
echo "<option value='".$cat[$i]."'>".$cat[$i]."</option>";. Before saving, the urls 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 URL gets saved in the urls table along with the chosen category.
Finally, the HTML code gives users instructions and parameters for category and url entry.
SAVE THIS PAGE AS: cms-write-website-directory.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>Creating Website Directories—Content Management System (CMS)</TITLE>
<meta name="description" content="Creating Website Directories—Content Management System (CMS)">
<meta name="keywords" content="Creating 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 15px Verdana;}
.url {position:absolute;top:0px;left:10px;width:989px}
.form {position:absolute;top:140px;left:200px;width:700px}
.form2 {position:absolute;top:340px;left:200px;width:700px}
.info {position:absolute;top:80px;left:45px;width:900px;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";
$sql = "CREATE TABLE IF NOT EXISTS categories (
id int(11) NOT NULL auto_increment,
category varchar(30) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=1";
// Execute query
mysql_query($sql);
$sql = "CREATE TABLE IF NOT EXISTS urls (
id int(11) NOT NULL auto_increment,
url varchar(255) NOT NULL,
category varchar(30) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=1";
// Execute query
mysql_query($sql);
$pattern1 = '/[^a-zA-Z0-9\\_\\,]/i';
$pattern2 = '/[^a-zA-Z0-9\\.\\,\\!\\;\\-\\_\\*\\@\\=\\+\\$\\/\\&\\[\\]\\#\\?\\047\\:\\(\\)]/i';
$replacement = '';
$U=$_POST['url'];
$C=$_POST['category'];
$CY=$_POST['cy'];
$U=strip_tags($U);
$C=strip_tags($C);
$U=preg_replace($pattern2, $replacement, $U);
$C=preg_replace($pattern1, $replacement, $C);
$C=mysql_real_escape_string($C);
if (strlen($U)<5 && strlen($C)<5) { unset($U);unset($C);
echo "<div id='form' class='form'>
<form name='myform' method='post' action='cms-write-website-directory.php' onsubmit='bye()'>
<table width='700' border='0' cellpadding='2' cellspacing='2' align='center'>
<tr>
<td width='60'>Categories separated by commas, e.g.: fish,birds,mammals,insects</td><br>
<td><textarea name='category' cols='50' rows='22'></textarea></td>
</tr>
<tr>
<td> </td><td><input name='save' type='submit' value='Save Categories to DB'>
<input name='reset' type='reset' value='Reset'></td></tr>
</table>
</form>
</div>";
}
if (strlen($C)>4){
$ct = explode(",", $C);$n=count($ct);
for ($i=0;$i<$n;$i++) {
mysql_query("INSERT INTO categories (id, category)
VALUES ('','".$ct[$i]."')") or die('Error ,saving failed');}
$rc = mysql_affected_rows();
if ($rc>0){echo '<script language="javascript">alert("The saving was successfully accomplished.");</script>';}
else{echo '<script language="javascript">alert("The saving was unsuccessful.");</script>';}
}
$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);
if (strlen($U)>4||strlen($C)>4){unset($C);
echo "<div class='form2'><form name='myform2' method='post' action='cms-write-website-directory.php'><table width='700' border='0' cellpadding='2' cellspacing='2' align='center'><tr><td width='60'>URL</td><td><input name='url' type='text' size='66'></td></tr><tr><td>Category</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><tr><td> </td><td><input name='save2' type='submit' value='Add URL to DB'>
<input name='reset2' type='reset' value='Reset'></td></tr></table></form></div>";
}
if (strlen($U)>4){
$check_user_data = mysql_query("SELECT url FROM urls WHERE url='$U'") or die(mysql_error());
if(mysql_num_rows($check_user_data) >0)
{echo '<script language="javascript">alert("This url exists. Please try again.")</script>;';
}else{
$U=mysql_real_escape_string($U);
mysql_query("INSERT INTO urls (id, url, category)
VALUES ('','$U','$CY')") or die('Error ,saving failed');
$rc = mysql_affected_rows();
if ($rc>0){unset($U);unset($CY);
echo '<script language="javascript">alert("The saving was successfully accomplished.");</script>';}
else{echo '<script language="javascript">alert("The saving was unsuccessful.");</script>';}
}}
mysql_close();
?>
<div id='top' class='url'>
<h1>Creating Website Directories—Content Management System (CMS)</h1>
<div id='info' class='info'>No single or double quotes or Enter/Return allowed in urls. Use letters, numbers, and underscores only in categories. Categories must be separated by commas with NO SPACES ANYWHERE and each category must be under 30 characters long, e.g.: <b>fish,birds,mammals,insects</b>. URLs must be legitimate urls. Submit comma-separated categories first, then submit URLs, one at a time, selecting categories from the dropdown.<br></div>
</div>
<script language="javascript">
function bye(){e=document.getElementById('form');e.style.display='none';}
</script>
<?php include("nav.html"); ?>
</body>
</html>