Jump to content

my picture gallery needs delete option!


astralaaron

Recommended Posts

Hey, let me attempt to explain with out confusing everyone..I'm trying to make an option for the admin of a site to be able to delete pictures from a picture gallery.i imagine i loop the pictures like normal but add the <form> and check box's to the loopthat part sounds like it can be done to me. THe next part is confusing my how to catch the $_POST['']; variables for the selected pictures.i know how to get a variable but if you don't know how many are being sent.. does anyone get what i am trying to say?

Link to comment
Share on other sites

Let's assume your picture directory is in the directory 'pictures'. With that assumption(and whatever administration check you have going) this should do the trick:

<?php  //Your admin check here   $dir = './pictures/*';   $files = glob($dir);	if(sizeof($files)>0){	   echo "<form action=\"yourDeletePage.php\" method=\"post\">";		 foreach($files as $file){			echo "<input type=\"checkbox\" name=\"deleteMe[]\" value=\"".('./pictures/'.basename($file))."\" /><br />";		 }	   echo "<input type=\"submit\" value=\"Delete selected\" /><br />";	   echo "</form>";	}else{	  echo "No files to be displayed";	 }?>

Now, yourDeletePage.php will look something like this:

<?php  //Again, admin check here;  if(isset($_POST['deleteMe'])){	$deletable = $_POST['deleteMe'];	foreach($deletable as $file){	   unlink(dirname(__FILE__).'/'.$file);	}  }else{	  echo "No files selected";   }  ?>

Link to comment
Share on other sites

<?php												$dir = './container/' . $id . '/*';												$files = glob($dir);												if(sizeof($files)>0){													echo "<form action=\"adm_del_pics_s2.php\" method=\"post\">";													foreach($files as $file){														echo "<input type=\"checkbox\" name=\"deleteMe[]\" value=\"".('container/' . $id .'/'. basename($file))."\" /><br />";													}													echo "<input type=\"submit\" value=\"Delete selected\" /><br />";													echo "</form>";												}else{													echo "No files to be displayed";												}

why arent my pictures showing up? just checkboxes show up for each picture and also a weird file called thumbs.dbthe output was this:

<form action="adm_del_pics_s2.php" method="post"><input type="checkbox" name="deleteMe[]" value="container/a479ae759c28ce/Bed 023.jpg" /><br /><input type="checkbox" name="deleteMe[]" value="container/a479ae759c28ce/Bed 024.jpg" /><br /><input type="checkbox" name="deleteMe[]" value="container/a479ae759c28ce/Bed011.jpg" /><br /><input type="checkbox" name="deleteMe[]" value="container/a479ae759c28ce/Thumbs.db" /><br /><input type="submit" value="Delete selected" /><br /></form>	

Link to comment
Share on other sites

This line in the foreach loop:

echo "<input type=\"checkbox\" name=\"deleteMe[]\" value=\"".('container/' . $id .'/'. basename($file))."\" /><br />";

Should be THIS if you want for the picture to show up.

echo "<input type=\"checkbox\" name=\"deleteMe[]\" value=\"".('container/' . $id .'/'. basename($file))."\" />";echo "<img src=\"container/$id/".basename($file)."\" alt=\"Picture\" /><hr />";

And, as for your earlier thing of deleting form the db, instead of passing the file name, pass the database ID from the MySQL statement. You could then get the picture value from the database, call the unlink statement, and then use the DELETE FROM mySQL statement. If that confuses you, let me know so that I can show you what I mean.By the way, the foreach loop is basically (if you know javascript) the for(x in variable) statement from Javascript. Basically, it takes into consideration that there are some things that are not always indexed(basically, there isn't always numeric values for keys). It just makes you do one less line in a for loop in most circumstances, i use it out of sheer laziness, you could use a for($i... loop if you really want to.

Link to comment
Share on other sites

Well, I'd do it this way:

<!--admin_panel.php--><form action="[some page].php" method="post"><? $num_elements=count($pictures);for ($i=0;$i<$num_elements;$i++) 	echo "<input type=\"checkbox\" name=\"pic_".$i."\"> <img src=\"".$pictures[$i]."\">";?><input type="hidden" name="num_elements" value="<?echo $num_elements?>"><input type="submit" value="Delete"></form>

And in [some page].php:

<?php$num_elements=$_POST['num_elements'];for ($i=0;$i<$num_elements;$i++)	if ($_POST['pic_'.$i]=="on")	{		$query="delete from pictures where id=(int)$i";		$mysql_query($query);	}?>

Obviously I'm taking for granted that you have the names of the pictures in a table calledpictures and the DB you're using is MySQL.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...