jamiet757 Posted January 29, 2010 Share Posted January 29, 2010 I have a script that will look for certain entries in a database (where the published column is 2) and select the folder from those entries, then it will delete some files in that folder. The problem is, it only does it with the first one it comes to, for example I expect it to delete the files from 2 folders that match the parameters, however it only deletes the files from the first. Maybe someone can tell me how to make it repeat for every "folder" that it gets. function delete_declined_files() { global $db; $dp = new TMySQLQuery; $dp->connection = $db; $folder=""; $sql="select folder from photos where published=2"; $dp->open($sql); if(!$dp->eof) { $folder=$dp->row["folder"]; } if($folder!="" and file_exists($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder)) { $dir = opendir ($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder); while ($file = readdir ($dir)) { if($file != "thumb1.jpg") { @unlink($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder."/".$file); } } } } Quote Link to comment Share on other sites More sharing options...
schilly Posted January 29, 2010 Share Posted January 29, 2010 well you only select one row from your result set i think (not exactly sure how your mysql class works) so it will only delete that one folder. you need to loop through your results and perform what you're doing. Quote Link to comment Share on other sites More sharing options...
jamiet757 Posted January 29, 2010 Author Share Posted January 29, 2010 well you only select one row from your result set i think (not exactly sure how your mysql class works) so it will only delete that one folder. you need to loop through your results and perform what you're doing. Yeah that is what I was thinking, unfortunately my knowledge of php is very limited, I can modify some things and figure out others, but I don't know how to set it up to loop through the results and stop when it is finished. Can you give me some insight? Quote Link to comment Share on other sites More sharing options...
schilly Posted January 30, 2010 Share Posted January 30, 2010 im not sure how that mysql wrapper works but it looks like you could do this: while($folder=$dp->row["folder"]){ //put folder remove code in here } Quote Link to comment Share on other sites More sharing options...
jamiet757 Posted January 30, 2010 Author Share Posted January 30, 2010 well, I tried this, but when I accessed the script, it just hung and did not do anything: function delete_declined_files() { global $db; $dp = new TMySQLQuery; $dp->connection = $db; $folder=""; $sql="select folder from photos where published=2"; $dp->open($sql); if(!$dp->eof) { $folder=$dp->row["folder"]; } while($folder=$dp->row["folder"]){ if($folder!="" and file_exists($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder)) { $dir = opendir ($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder); while ($file = readdir ($dir)) { if($file != "thumb1.jpg") { @unlink($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder."/".$file); } } } } } I didn't really know where to put the while statement. Quote Link to comment Share on other sites More sharing options...
schilly Posted January 30, 2010 Share Posted January 30, 2010 hmmmm your not using your first row. function delete_declined_files() { global $db; $dp = new TMySQLQuery; $dp->connection = $db; $folder=""; $sql="select folder from photos where published=2"; $dp->open($sql); while($folder=$dp->row["folder"]){ if($folder!="" and file_exists($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder)) { $dir = opendir ($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder); while ($file = readdir ($dir)) { if($file != "thumb1.jpg") { @unlink($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder."/".$file); } } } } } if this doesn't work i would read up on the TMySQLQuery class you are using. Quote Link to comment Share on other sites More sharing options...
jamiet757 Posted January 30, 2010 Author Share Posted January 30, 2010 Nope, it didn't work. Guess I will have to hire someone to look through the code and figure out how to make it work. Thanks for the help! Quote Link to comment 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.