floridaflatlander Posted October 29, 2011 Share Posted October 29, 2011 I've seen two post now about not deleting within a loop and I think I may need to ask a question about this. I have a photograph db that gives the addresses of the thumbs and photos in files for a product, it also is keyed to the products db. There can be more than one photo per product. If someone selects yes from a radio button and clicks submit the process should delete the record of the product in the product db, delete the photos in the photo images/files(this is the loop) and then delete the product id and row in the photo db Is this a decent way of doing this? if ($_POST['sure'] == 'Yes') { // If Yes delete the item and its pictures in image folders $q = "DELETE FROM product WHERE id = $item LIMIT 1"; // Delete the product record $r = @mysqli_query ($dbc, $q); $q = "SELECT id, thumb, photo FROM photos WHERE id = '$item'"; $r = @mysqli_query ($dbc, $q); if ($r){ while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){ unlink('images/members/'.$row['thumb'].''); // Remove-delete photos from folders for each row in photo db unlink('images/members/'.$row['photo'].''); } } $q = "DELETE FROM photos WHERE id_prod = $item"; // Delete the photo record $r = @mysqli_query ($dbc, $q); // When through redirect header("Location: $home/member/index.php?user=$mem_id"); exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/250053-deleting-inside-a-loop/ Share on other sites More sharing options...
mat3000000 Posted October 29, 2011 Share Posted October 29, 2011 Probably your best way is to just delete the whole folder with: public static function deleteDir($dirPath) { if (! is_dir($dirPath)) { throw new InvalidArgumentException('$dirPath must be a directory'); } if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') { $dirPath .= '/'; } $files = glob($dirPath . '*', GLOB_MARK); foreach ($files as $file) { if (is_dir($file)) { self::deleteDir($file); } else { unlink($file); } } rmdir($dirPath); } Or something similar, then remake the folder using mkdir(); Sorry if this isn't the answer you were looking for, but I didn't understand your question really. Quote Link to comment https://forums.phpfreaks.com/topic/250053-deleting-inside-a-loop/#findComment-1283337 Share on other sites More sharing options...
ManiacDan Posted October 29, 2011 Share Posted October 29, 2011 Don't delete the whole folder, that's obviously not what you need. Your code seems fine, but you should probably delete the item AFTER you delete the images. That's just me being a paranoid programmer, but I never delete the parent before I delete the children. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/250053-deleting-inside-a-loop/#findComment-1283341 Share on other sites More sharing options...
floridaflatlander Posted October 29, 2011 Author Share Posted October 29, 2011 Thanks maniacdan, I saw that (not deleteing the parent until the kids were deleted) right as I was typing the original post. I'll go back and change that in the original code. Also thanks mat3... I can't delete the folder because there are other photos in it. Quote Link to comment https://forums.phpfreaks.com/topic/250053-deleting-inside-a-loop/#findComment-1283343 Share on other sites More sharing options...
mat3000000 Posted October 30, 2011 Share Posted October 30, 2011 Ahh, OK. Quote Link to comment https://forums.phpfreaks.com/topic/250053-deleting-inside-a-loop/#findComment-1283424 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.