943,169 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 61
  • PHP RSS
Aug 31st, 2010
0

For Each Loop with Array Not Quite Working Right

Expand Post »
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:

PHP Syntax (Toggle Plain Text)
  1. if($delete1 || $delete2 || $delete3 || $delete4 || $delete5 || $delete6 || $delete7 || $delete8 || $delete9 || $delete10) {
  2. $getfile = mysql_query("SELECT * FROM usertable WHERE album='$album'");
  3. $find = mysql_fetch_array($getfile);
  4.  
  5. $file1 = $find['file1'];
  6. $title1 = $find['title1'];
  7. $file2 = $find['file2'];
  8. $title2 = $find['title2'];
  9. $file3 = $find['file3'];
  10. $title3 = $find['title3'];
  11. $file4 = $find['file4'];
  12. $title4 = $find['title4'];
  13. $file5 = $find['file5'];
  14. $title5 = $find['title5'];
  15. $file6 = $find['file6'];
  16. $title6 = $find['title6'];
  17. $file7 = $find['file7'];
  18. $title7 = $find['title7'];
  19. $file8 = $find['file8'];
  20. $title8 = $find['title8'];
  21. $file9 = $find['file9'];
  22. $title9 = $find['title9'];
  23. $file10 = $find['file10'];
  24. $title10 = $find['title10'];
  25. }
  26. $deletearray = array();
  27.  
  28. if($delete10) {
  29. array_push($deletearray, 'delete10');
  30. }
  31. if($delete9) {
  32. array_push($deletearray, 'delete9');
  33. }
  34. if($delete8) {
  35. array_push($deletearray, 'delete8');
  36. }
  37. if($delete7) {
  38. array_push($deletearray, 'delete7');
  39. }
  40. if($delete6) {
  41. array_push($deletearray, 'delete6');
  42. }
  43. if($delete5) {
  44. array_push($deletearray, 'delete5');
  45. }
  46. if($delete4) {
  47. array_push($deletearray, 'delete4');
  48. }
  49. if($delete3) {
  50. array_push($deletearray, 'delete3');
  51. }
  52. if($delete2) {
  53. array_push($deletearray, 'delete2');
  54. }
  55. if($delete1) {
  56. array_push($deletearray, 'delete1');
  57. }
  58.  
  59.  
  60. foreach($deletearray as $delkey => $delvalue) {
  61. switch($delvalue) {
  62. case 'delete10':
  63.  
  64. chmod ($file10, 0777);
  65. unlink($file10);
  66.  
  67. $update = mysql_query("UPDATE usertable SET file10='',title10='' WHERE album='$album'") or die (mysql_error());
  68.  
  69. break;
  70. case 'delete9':
  71.  
  72. chmod ($file9, 0777);
  73. unlink($file9);
  74.  
  75. $update = mysql_query("UPDATE usertable SET file9='$file10',title9='$title10' WHERE album='$album'") or die (mysql_error());
  76.  
  77. break;
  78. case 'delete8':
  79.  
  80. chmod ($file8, 0777);
  81. unlink($file8);
  82.  
  83. $update = mysql_query("UPDATE usertable SET file8='$file9',title8='$title9',file9='$file10',title9='$title10' WHERE album='$album'") or die (mysql_error());
  84.  
  85. break;
  86. case 'delete7':
  87.  
  88. chmod ($file7, 0777);
  89. unlink($file7);
  90.  
  91. $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());
  92.  
  93. break;
  94. case 'delete6':
  95.  
  96. chmod ($file6, 0777);
  97. unlink($file6);
  98.  
  99. $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());
  100.  
  101. break;
  102. case 'delete5':
  103.  
  104. chmod ($file5, 0777);
  105. unlink($file5);
  106.  
  107. $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());
  108.  
  109. break;
  110. case 'delete4':
  111.  
  112. chmod ($file4, 0777);
  113. unlink($file4);
  114.  
  115. $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());
  116.  
  117. break;
  118. case 'delete3':
  119.  
  120. chmod ($file3, 0777);
  121. unlink($file3);
  122.  
  123. $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());
  124.  
  125. break;
  126. case 'delete2':
  127.  
  128. chmod ($file2, 0777);
  129. unlink($file2);
  130.  
  131. $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());
  132.  
  133. break;
  134. case 'delete1';
  135.  
  136. chmod ($file1, 0777);
  137. unlink($file1);
  138.  
  139. $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());
  140. break;
  141. }
  142. }

Any help is appreciated!
Reputation Points: 10
Solved Threads: 2
Newbie Poster
jkaye is offline Offline
11 posts
since Jul 2010
Aug 31st, 2010
0
Re: For Each Loop with Array Not Quite Working Right
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.
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
623 posts
since Jul 2008
Sep 1st, 2010
0
Re: For Each Loop with Array Not Quite Working Right
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:
PHP Syntax (Toggle Plain Text)
  1. tbl_album
  2. id | user_id | name
  3. 1 | 5 | Moview
  4. 2 | 6 | Friends
  5.  
  6. tbl_album_files
  7. id | album_id | file | title
  8. 1 | 1 | moview1_2010.jpg | 2010 Release 1
  9. 2 | 1 | moview2_2010.jpg | 2010 Release 2
  10. 3 | 1 | moview3_2010.jpg | 2010 Release 3
  11. ..
  12. ..
  13. ..
  14. 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.
Reputation Points: 154
Solved Threads: 162
Master Poster
vibhadevit is offline Offline
791 posts
since Apr 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Image Upload issue with extension ,jpg .png etc
Next Thread in PHP Forum Timeline: How to update a picture





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC