Code for Deleting 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 POST the category to delete, the replacement category if you will be replacing rather than deleting a category, and the replace-delete flag that says which you plan to do. The page's instructions say: "To delete a category, choose whether to delete all photos in that category along with the category itself, or select a different category to place these photos in before deleting the category."
Next we get the available categories from the MySQL db table categories—this categories table is read and its content pushed into a cat[] array. Then we echo the input form to the screen, still in PHP. The form contains a radio button so users can indicate if they want to replace a category or delete it. The form's action is 'cms-edit-photo-gallery-delete-category.php'—i.e., it reloads itself in order to POST the category(s) and flag from the form to the PHP script. The categories for replacing or deleting are shown to the user as a dropdown list so he will know what he already has. Actually, there are two such dropdowns, both the same. The first lets the user choose the "Category to delete or replace" and the second lets the user choose the "Category to replace it with (IF you are replacing it—if not, this will be ignored)." If the user has selected replace, the photos table is searched for the old category and it is replaced with the new category, and the old category is deleted from the categories table. Incidentally, in order to do replacing, the user will normally go to the Add Category script first and add the new category to the categories table, and this will be the category chosen to be the "Category to replace it with." If the user has selected delete, the photos table is searched for the old category and all photos that are found are deleted (each photo has a category field), and the old category is deleted from the categories table. No photos are ever deleted in image files, of course. Just the reference to them in the MySQL table is deleted.
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-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 Photo Gallery—Content Management System (CMS)—Delete Category</TITLE>
<meta name="description" content="Editing Photo Gallery—Content Management System (CMS)—Delete Category">
<meta name="keywords" content="Delete 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;}
.abc {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-photo-gallery-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 photos<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 photos 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-photo-gallery-delete-category.php";</script>';}
else{echo '<script language="javascript">alert("The replacing was unsuccessful.");</script>';}
}
if ($RD=="delete_category"){
mysql_query("DELETE FROM photos 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-photo-gallery-delete-category.php";</script>';}
else{echo '<script language="javascript">alert("The deleting was unsuccessful.");</script>';}
}
mysql_close();
?>
<div id='top' class='abc'>
<h1>Editing Photo Gallery—Content Management System (CMS)—Delete Category</h1>
<div id='info' class='info'><h3>To delete a category, choose whether to delete all photos in that category along with the category itself, or select a different category to place these photos in before deleting the category.</h3></div>
</div>
<?php include("navi.html"); ?>
</body>
</html>