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); } } } } Link to comment https://forums.phpfreaks.com/topic/190299-repeat-function/ 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. Link to comment https://forums.phpfreaks.com/topic/190299-repeat-function/#findComment-1004011 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? Link to comment https://forums.phpfreaks.com/topic/190299-repeat-function/#findComment-1004014 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 } Link to comment https://forums.phpfreaks.com/topic/190299-repeat-function/#findComment-1004030 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. Link to comment https://forums.phpfreaks.com/topic/190299-repeat-function/#findComment-1004031 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. Link to comment https://forums.phpfreaks.com/topic/190299-repeat-function/#findComment-1004032 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! Link to comment https://forums.phpfreaks.com/topic/190299-repeat-function/#findComment-1004034 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.