Jump to content

Repeat function


jamiet757

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.