0

Hi All,

The basis of this project is that I have an album of pictures, where you can add and delete pictures. What I wanted to do, was when you delete a picture, have the rest of the pictures move down to fill in the gap.

The database is set up like so:

Each user has a table, and each row in the table is an album. Each album has a limit of ten files.

albumname
file1
title1
file2
title2
...
file8
title8
file9
title9
file10
title10

What's happening is if I select picture 3 and picture 5 and click delete, picture 3 is being deleted perfectly, where picture 5 is deleting the file, but not replacing the info in the database with the respective data. The foreach loops through the array with the same code for every entry. I can't figure out why it would be doing something different for the second array entry.

Here is my code:

if($delete1 || $delete2 || $delete3 || $delete4 || $delete5 || $delete6 || $delete7 || $delete8 || $delete9 || $delete10) {
		$getfile = mysql_query("SELECT * FROM usertable WHERE album='$album'");
		$find = mysql_fetch_array($getfile);
		
		$file1 = $find['file1'];
		$title1 = $find['title1'];
		$file2 = $find['file2'];
		$title2 = $find['title2'];
		$file3 = $find['file3'];
		$title3 = $find['title3'];
		$file4 = $find['file4'];
		$title4 = $find['title4'];
		$file5 = $find['file5'];
		$title5 = $find['title5'];
		$file6 = $find['file6'];
		$title6 = $find['title6'];
		$file7 = $find['file7'];
		$title7 = $find['title7'];
		$file8 = $find['file8'];
		$title8 = $find['title8'];
		$file9 = $find['file9'];
		$title9 = $find['title9'];
		$file10 = $find['file10'];
		$title10 = $find['title10'];
	}
	$deletearray = array();
	
	if($delete10) {
		array_push($deletearray, 'delete10');
	}
	if($delete9) {
		array_push($deletearray, 'delete9');
	}
	if($delete8) {
		array_push($deletearray, 'delete8');
	}
	if($delete7) {
		array_push($deletearray, 'delete7');
	}
	if($delete6) {
		array_push($deletearray, 'delete6');
	}
	if($delete5) {
		array_push($deletearray, 'delete5');
	}
	if($delete4) {
		array_push($deletearray, 'delete4');
	}
	if($delete3) {
		array_push($deletearray, 'delete3');
	}
	if($delete2) {
		array_push($deletearray, 'delete2');
	}
	if($delete1) {
		array_push($deletearray, 'delete1');
	}
	
	
	foreach($deletearray as $delkey => $delvalue) {
		switch($delvalue) {
			case 'delete10':
				
				chmod ($file10, 0777);
				unlink($file10);
				
				$update = mysql_query("UPDATE usertable SET file10='',title10='' WHERE album='$album'") or die (mysql_error());
			
			break;
			case 'delete9':
				
				chmod ($file9, 0777);
				unlink($file9);
				
				$update = mysql_query("UPDATE usertable SET file9='$file10',title9='$title10' WHERE album='$album'") or die (mysql_error());
			
			break;
			case 'delete8':
				
				chmod ($file8, 0777);
				unlink($file8);
				
				$update = mysql_query("UPDATE usertable SET file8='$file9',title8='$title9',file9='$file10',title9='$title10' WHERE album='$album'") or die (mysql_error());
			
			break;
			case 'delete7':
				
				chmod ($file7, 0777);
				unlink($file7);
				
				$update = mysql_query("UPDATE usertable SET file7='$file8',title7='$title8',file8='$file9',title8='$title9',file9='$file10',title9='$title10' WHERE album='$album'") or die (mysql_error());
			
			break;
			case 'delete6':
				
				chmod ($file6, 0777);
				unlink($file6);
				
				$update = mysql_query("UPDATE usertable SET file6='$file7',title6='$title7',file7='$file8',title7='$title8',file8='$file9',title8='$title9',file9='$file10',title9='$title10' WHERE album='$album'") or die (mysql_error());
			
			break;
			case 'delete5':
				
				chmod ($file5, 0777);
				unlink($file5);
				
				$update = mysql_query("UPDATE usertable SET file5='$file6',title5='$title6',file6='$file7',title6='$title7',file7='$file8',title7='$title8',file8='$file9',title8='$title9',file9='$file10',title9='$title10' WHERE album='$album'") or die (mysql_error());
			
			break;
			case 'delete4':
				
				chmod ($file4, 0777);
				unlink($file4);
				
				$update = mysql_query("UPDATE usertable SET file4='$file5',title4='$title5' ,file5='$file6',title5='$title6',file6='$file7',title6='$title7',file7='$file8',title7='$title8',file8='$file9',title8='$title9',file9='$file10',title9='$title10' WHERE album='$album'") or die (mysql_error());
			
			break;
			case 'delete3':
				
				chmod ($file3, 0777);
				unlink($file3);
				
				$update = mysql_query("UPDATE usertable SET file3='$file4',title3='$title4',file4='$file5',title4='$title5' ,file5='$file6',title5='$title6',file6='$file7',title6='$title7',file7='$file8',title7='$title8',file8='$file9',title8='$title9',file9='$file10',title9='$title10' WHERE album='$album'") or die (mysql_error());
			
			break;
			case 'delete2':
				
				chmod ($file2, 0777);
				unlink($file2);
				
				$update = mysql_query("UPDATE usertable SET file2='$file3',title2='$title3',file3='$file4',title3='$title4',file4='$file5',title4='$title5' ,file5='$file6',title5='$title6',file6='$file7',title6='$title7',file7='$file8',title7='$title8',file8='$file9',title8='$title9',file9='$file10',title9='$title10' WHERE album='$album'") or die (mysql_error());
			
			break;
			case 'delete1';
				
				chmod ($file1, 0777);
				unlink($file1);
				
				$update = mysql_query("UPDATE usertable SET file1='$file2',title1='$title2',file2='$file3',title2='$title3',file3='$file4',title3='$title4',file4='$file5',title4='$title5' ,file5='$file6',title5='$title6',file6='$file7',title6='$title7',file7='$file8',title7='$title8',file8='$file9',title8='$title9',file9='$file10',title9='$title10' WHERE album='$album'") or die (mysql_error());
			break;
		}
	}

Any help is appreciated!

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by vibhaJ
0

Can you describe your database a little more in depth, this seems overly complex for what you're trying to do and I think it has to do with how the database is structured.

0

Table design is not proper.You should normalize your table.
you can have following structure:

tbl_album :
id : auto id
user_id : id of user
name : name of album

tbl_album_files :
id : auto id
album_id : reference to tbl_album
file : name of file
title : title of album file


Now the entries will be as folows:

tbl_album
id   |  user_id | name
1    |   5      | Moview
2    |   6      | Friends

tbl_album_files
id   | album_id |  file           |  title 
1    |   1      | moview1_2010.jpg | 2010 Release 1
2    |   1      | moview2_2010.jpg | 2010 Release 2
3    |   1      | moview3_2010.jpg | 2010 Release 3
..
..
..
10    |   1      | moview10_2010.jpg | 2010 Release 10

Thus when you delete any album file it will be just deleted from tbl_album_files.
So your php code will remain as it is.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.