Jump to content


Photo

PHP Image Gallery: Adding a Delete Function


  • Please log in to reply
4 replies to this topic

#1 CJP89

CJP89

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 17 March 2010 - 05:54 AM

Hi there I've been following this tutorial in order to create an Image Gallery:

http://www.pixel2lif...ry_with_jquery/

I've managed to incorporate a login and an upload feature already, but I'm having trouble figuring out how I can also delete images from the gallery. I'm extremely new to PHP so I don't fully know what all of the function mean or do, but i do have experience with other languages.

As you can see in the tutorial each image is output inside a while using an "echo". What i tried to do in my code is to add a checkbox to each of the images like so:
/* Outputting each image: */
					
					echo '
					<div id="pic-'.($i++).'" class="pic" style="top:'.$top.'px;left:'.$left.'px;background:url('.$thumb_directory.'/'.$file.') no-repeat 50% 50%;"><form method="post"><input type="checkbox" name="deleteMe[]"></form>
					<a class="fancybox" rel="fncbx" href="'.$orig_directory.'/'.$file.'" target="_blank">'.$title.'</a>
					</div>';
					$picCount++;

				}

This does indeed output every image with a checkbox. I then found some instructions on how to check for "ticked" checkboxes in PHP and wrote this just bellow:


if(isset($_SESSION['loggedin']))
					{
						echo'<form method="post"><input type="submit" name="Submit" value="Delete" id="deleteButton"/></form>';
						if(isset($_POST['Submit']))
						{
							foreach($_POST['deleteMe'] as $checked)
							{
								echo'hello world i have been $checked';
							}
						}
					}


However when i push the Delete button I get:

Warning: Invalid argument supplied for foreach() in /home/muzosroc/public_html/mediaPHP.php on line 158

From what i understand of PHP, that means that the type of variable which foreach is calling is not an array.
However I thought $_POST was supposed to be an array. I'm fairly confused. Could someone clear this up please? Any ideas?

Thanks =].

#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 17 March 2010 - 08:48 PM

$_POST is an array, but you have a few problems here:
1. When you hit the submit button of a form, it submits the form that is the closest parent of that button (not all forms on the page).
2. Assuming the code you provided us contains line 158, then the issue is with $_POST['deleteMe'], not $_POST. If none of your 'deleteMe' checkboxes are checked, then $_POST['deleteMe'] will not be an array (technically, calling isset() on it won't even return true).

To fix these problems, you need to remove all of the <form> tags that you added and wrap the <?php ?> tag from step1 of the tutorial (line 4) in one form tag.
Additionaly, my understanding of your logic is right, you need to change the following line of code:
if(isset($_POST['Submit']))
// To:
 if(isset($_POST['Submit']) && is_array($_POST['deleteMe']))


#3 CJP89

CJP89

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 03 April 2010 - 11:11 AM

Hey, thanks for that, It's helped me along quite a bit. However now I'm looking for a way to check a specific part of a string, in order to match up filenames.

The tutorial above works by using two images, one from the thumbnail directory and one from the original directory

images/original/myPicture.jpg
images/thumb/myPicture.jpg

When the images are generated i've told each of the check boxes to set the original image directory and filename as its value.

<input type="checkbox" name="deleteMe[]" value="'.$orig_directory.'/'.$file.'">

I've also told it to construct an array of all of the required thumbnails using this code below:

$tempThumbPath = $thumb_directory.'/'.$file;
$thumbPath_Array[] = $tempThumbPath;

What I need to do is to delete both the original image and the corresponding thumbnail in order to remove them from the gallery.

I was looking for a way to compare a specific part of the string value being stored in the array for example

images/original/JimmyPage1.jpg
images/thumb/JimmyPage1.jpg

to be able to check for just the "JimmyPage1" part of the string without worrying about the extension or preceding filepath.

So basically being able to check if they both contain "JimmyPage1"

Thanks! =]

Edited by CJP89, 03 April 2010 - 11:45 AM.


#4 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 03 April 2010 - 12:07 PM

Use: basename()

#5 camboreadcenter

camboreadcenter

    Young Padawan

  • Members
  • Pip
  • 31 posts
  • Gender:Male
  • Location:Phnom Penh

Posted 05 May 2010 - 11:15 AM

oh. that is my problem too




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users