Jump to content

stuck in a loop ...


enkidu72

Recommended Posts

Hi all ,

I'm stuck with a while that doesn't seems to work ...

 

here the code :

$amber_dir_suffix='_amber';
	$amber_dir_new=$amber_dir.$amber_dir_suffix;
	$count=0;
	$underscore="-";
                $query="select id from jobs where workdir='$amber_dir_new' and owner='$user_id'";
                
           
                
	while (mysql_numrows(mysql_query($query))) {
	    $count++;
      		    $amber_dir_new=$amber_dir.$amber_dir_suffix.$underscore.$count;
	}

$result=mkdir("$home_dir/$user_dir/Xplor-SC/$amber_dir_new/");
	    if($result){
		echo "Directory $amber_dir_new created succesfully ";
	    }else{
	    echo "Cannot create dir  $home_dir/$user_dir/Xplor-SC/$amber_dir_new!";
	    }

 

 

Basically i need to check if the record exist , and if does increment count ...

I'm sure It must be a stupid thing ...

Link to comment
Share on other sites

I'm not 100% sure what you're trying to do, but the following made sense;

 

<?php
$amber_dir_suffix = '_amber';
$amber_dir_new = $amber_dir . $amber_dir_suffix;
$count = 0;
$underscore = "-";
$query = "SELECT id FROM `jobs` WHERE `workdir`='$amber_dir_new' AND `owner`='$user_id'";
$result = mysql_query($query) or die(mysql_error());


if(mysql_fetch_assoc($result)) {
   $count++;
   $amber_dir_new = $amber_dir . $amber_dir_suffix . $underscore . $count;
}

$dir_result = mkdir("$home_dir/$user_dir/Xplor-SC/$amber_dir_new/");
if($dir_result) {
   echo "Directory $amber_dir_new created succesfully ";
} else {
   echo "Cannot create dir  $home_dir/$user_dir/Xplor-SC/$amber_dir_new!";
}

Link to comment
Share on other sites

I need to check if the record exists in the db ... ( a directory )

If it does , increment count by one .

 

So :

count  = 0

1) check for "dir"  -> dir exists

2) count = 1

check for dir1 -> dir1 exists

3)count=2

check for dir2 -> dir2 doesn't exist

create dir dir2 ...

 

 

 

Link to comment
Share on other sites

This was how was working before , now all infos have been saved in a db ...

while (file_exists("$home_dir/$user_dir/Xplor-SC/$amber_dir_new/") ){
	    $count++;
	    $amber_dir_new=$amber_dir.$amber_dir_suffix.$underscore.$count;
}

 

I just need to do the same querying mysql ...

Link to comment
Share on other sites

I need to check if the record exists in the db ... ( a directory )

A directory? or a db?

 

$count = 0;
$query = "SELECT id FROM `jobs` WHERE `workdir`='$amber_dir_new' AND `owner`='$user_id' LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
$count += mysql_num_rows($result);

Link to comment
Share on other sites

The problem is pretty obvious

while (mysql_numrows(mysql_query($query))) {
    $count++;
    $amber_dir_new=$amber_dir.$amber_dir_suffix.$underscore.$count;
}

 

Aside from the fact that it should be mysql_num_rows(), the condition never changes, so the loop will continue indefinitely. $amber_dir_new is the only thing changed within the loop, but it is not part of the condition, $query is. If $query was changing records in the db it might be used to exit the loops, but it is only doing a select. In this instance you would need to modify $query to have the loop exit.

 

while (mysql_num_rows(mysql_query($query))) {
    $count++;
    $amber_dir_new=$amber_dir.$amber_dir_suffix.$underscore.$count;
    $query="select id from jobs where workdir='$amber_dir_new' and owner='$user_id'";
}

 

But, there are better ways to handle this than running multiple queries. Try this:

$amber_dir_suffix = '_amber';
$amber_dir_new = $amber_dir.$amber_dir_suffix;

$query="SELECT workdir FROM jobs WHERE workdir LIKE '$amber_dir_new%' AND owner='$user_id'";
$result - mysql_query($query) or die (mysql_error());

$directories = array();
while ($record = mysql_fetch_assoc($result))
{
    $directories[] = $record['workdir'];
}


if (in_array($amber_dir_new, $directories))
{
    $i = 0;
    do
    {
        $amber_dir_new = "{$amber_dir}{$amber_dir_suffix}_{$i}";
        $i++;
    }
    while (in_array($amber_dir_new, $directories));
}

$full_path = "{$home_dir}/{$user_dir}/Xplor-SC/{$amber_dir_new}/";

if(!mkdir($full_path))
{
    echo "Directory $amber_dir_new created succesfully ";
}
else
{
    echo "Cannot create dir  $home_dir/$user_dir/Xplor-SC/$amber_dir_new!";
}

 

 

Link to comment
Share on other sites

I'm not 100% sure what you're trying to do, but the following made sense;

 

<?php
$amber_dir_suffix = '_amber';
$amber_dir_new = $amber_dir . $amber_dir_suffix;
$count = 0;
$underscore = "-";
$query = "SELECT id FROM `jobs` WHERE `workdir`='$amber_dir_new' AND `owner`='$user_id'";
$result = mysql_query($query) or die(mysql_error());


if(mysql_fetch_assoc($result)) {
   $count++;
   $amber_dir_new = $amber_dir . $amber_dir_suffix . $underscore . $count;
}

$dir_result = mkdir("$home_dir/$user_dir/Xplor-SC/$amber_dir_new/");
if($dir_result) {
   echo "Directory $amber_dir_new created succesfully ";
} else {
   echo "Cannot create dir  $home_dir/$user_dir/Xplor-SC/$amber_dir_new!";
}

 

I made the fixes in the code I posted earlier

Link to comment
Share on other sites

MANY thanks !

 

Still doesn't work , but at least I've understood which was my mistake !

 

Can you state what problems there are? I wrote my code off the cuff and was not able to test even for syntax errors. But the logic should be sound. I did notice that I had the wrong check on the last IF statement. Should have been

if(mkdir($full_path))

Link to comment
Share on other sites

Yes , I had already noticed

if(mkdir($full_path))

:)

 

Basically the problem is that if the dir somedir_amber_0 already exists , doesn't create somedir_amber_1 ...

Here the output :

 

Array ( [0] => 22-01-09_18-17-03 [1] => 22-01-09_18-17-03_amber [2] => 22-01-09_18-17-03_amber_0 )
SELECT workdir FROM jobs WHERE workdir LIKE '%22-01-09_18-17-03%' AND owner='27'

Warning: mkdir() [function.mkdir]: File exists in /var/www/html/stable/xplor_sc.php on line 305

 

so I think the problem is in the

 

if (in_array($amber_dir_new, $directories))
                        {
                             $i = 0;
                            do
                            {
                                $amber_dir_new = "{$amber_dir}{$amber_dir_suffix}_{$i}";
                                $i++;
                            }
                            while (in_array($amber_dir_new, $directories));
                        }

 

part of the code ...

 

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.