Code for Adding PHP Photo Gallery Category
So why might anyone want to learn to make a PHP photo gallery when there are plenty of freebies out there and in control panels of websites? Because learning is fun, and you might try using this as a stepping stone for writing your own PHP gallery script. And if you don't want to learn PHP, we have a JavaScript photo gallery that we teach you about. These scripts are fun to tinker with, modify, and learn from.
For more on viewing and creating a PHP photo gallery, simply go to code-for-php-photo-gallery.html
Here are the tutorial files for all the photo gallery functions:
View Gallery
Create Gallery
Add Category
Delete Category
Add Photo
Delete Photo
So, on to the code. We define the CSS, then run browser sniffers and finetune the display with the fix() function, run by the onload in the body tag. Next, there's the PHP. The PHP code starts as so many PHP codes start: by getting the db connection data from a config file. It shows the password, username, and database name. Then we define the input filter as allowing only numbers, letters, and underscore for category input, and a preg_replace function makes it happen. We do the usual strip_tags() and mysql_real_escape_string() just because it's a good habit to get into.
Next we echo the input form to the screen, still in PHP. The form's action is 'cms-edit-photo-gallery-add-category.php'—i.e., it reloads itself in order to POST the category from the form to the PHP script. Next, the categories table is read and its content pushed into a cat[] array. These categories are shown to the user as a dropdown list so he will know what he already has. Then the categories table is searched for the currently entered category and the user is told if it already exists. If it doesn't exist already, the new category is INSERTed into the categories table.
TERMS OF USE: You must put a link on your site's home page to:
<a HREF="http://www.css-resources.com">css-resources.com</a> and it must NOT be a "nofollow" link, now or ever, and the link must stay live and unbroken for as long as you use the photo gallery script you'll find on this page, and you may not edit any part of the link, which must say "css-resources.com". Your site must NOT, now or ever, be about violence or hate or crime or treason or porn or terrorism, and your photo gallery script site/page must not have photos involving violence or hate or crime or treason or porn or terrorism. Do not use the sample photos in our sample JavaScript photo gallery for anything: they're copyrighted by © Focus Multimedia Ltd., Serif (Europe) Ltd., and Hemera Technologies Inc. If you cannot accept these terms, please do not use our script. We reserve the right to deny our script use to anyone with an unacceptable website. Our script is copyrighted by us, at MCS Investments, Inc. All these codes work—we have a working version we use here using this same code.
SAVE THIS PAGE AS: cms-edit-photo-gallery-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 Photo Gallery—Content Management System (CMS)—Add Category</TITLE>
<meta name="description" content="Editing Photo Gallery—Content Management System (CMS)—Add Category">
<meta name="keywords" content="Add Category,Editing Photo Gallery,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-photo-gallery-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-photo-gallery-add-category.php";</script>';}
else{echo '<script language="javascript">alert("The saving was unsuccessful.");</script>';}
}}
mysql_close();
?>
<div id='top' class='url'>
<h1>Editing Photo Gallery—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("navi.html"); ?>
</body>
</html>