Code for Adding PHP Photo Gallery Photo
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 POST the photo to add and the category it's in, and do input filtering since the only allowed characters are numbers, letters, underscore, dot, and hyphen. The page's instructions say: "Only letters, numbers, dots, hyphens, and underscores allowed in photos. Submit photos, one at a time, selecting categories from the dropdown." Then we get a list of available categories from the categories table—the user will select the category he wants his photo in from a dropdown list. The way we do this is to push the categories into a $cat[] array, then echo the needed HTML form to the screen, with the categories from the $cat[] array getting put into the form as a dropdown, using the HTML options tag.
Next we search the available photos in the MySQL db table photos—this table is read and if the photo is found, a message informs the user of this. If it isn't found, it's added. The HTML form's action is "cms-edit-photo-gallery-add-photo.php"—the page reloads itself to do the POSTing.
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-photo.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 Photo</TITLE>
<meta name="description" content="Editing Photo Gallery—Content Management System (CMS)—Add Photo">
<meta name="keywords" content="Add photo,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;}
.photo {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: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";
$pattern2 = '/[^a-zA-Z0-9\\.\\-\\_]/i';
$replacement = '';
$P=$_POST['photo'];
$CY=$_POST['cy'];
$P=strip_tags($P);
$P=preg_replace($pattern2, $replacement, $P);
$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-photo-gallery-add-photo.php'><table width='700' border='0' cellpadding='2' cellspacing='2' align='center'><tr><td width='60'>Photo</td><td><input name='photo' 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 photo to DB'>
<input name='reset2' type='reset' value='Reset'></td></tr></table></form></div>";
if (strlen($P)>4){
$check_user_data = mysql_query("SELECT photo FROM photos WHERE photo='$P'") or die(mysql_error());
if(mysql_num_rows($check_user_data) >0)
{echo '<script language="javascript">alert("This photo exists. Please try again.")</script>;';
}else{
$P=mysql_real_escape_string($P);
mysql_query("INSERT INTO photos (id, photo, category)
VALUES ('','$P','$CY')") or die('Error ,saving failed');
$rc = mysql_affected_rows();
if ($rc>0){unset($P);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='photo'>
<h1>Editing Photo Gallery—Content Management System (CMS)—Add Photo</h1>
<div id='info' class='info'><h3>Only letters, numbers, dots, hyphens, and underscores allowed in photos. Submit photos, one at a time, selecting categories from the dropdown.</h3></div>
</div>
<?php include("navi.html"); ?>
</body>
</html>