tags:

views:

89

answers:

1

I actually have it done, I did this math equation about 2 years ago and I am having trouble understanding it now.

Basicly I use this so that when users upload photos to my site, I can balance them out with only X amount of photo per folder.

This gives me something like this 1/1 1/2 1/3 1/4 ----1/10 2/1 2/2 2/3 and so on but I need to modify it to go 3 folders deep, each folder should have a limit of the number 1-9 or 1-10 then it will increase that number to the next

So if a large enough number is entered into my function below and the result is 3/10 then when the right number of objects is reached it would bump up to 4/1 then when so many thousands objects go by again it will jump to 4/2. What I am wanting to do is make it 3 numbers/levels deep 3/10/2 would go to 3/10/3 when it got to 3/10/10 it would go 4/1/1 4/1/2 4/1/3 when the third place got to 10 it would make it got to 4/2/1

<?PHP
function uploadfolder($x) { 
  $dir = floor($x/18001) + 1;
  $sdir = ceil($x/2000) % 9;
  return "$dir/$sdir";
}
?>

I spent a lot of time 2 years ago to get it to do this with 2 levels deep and I just kind of got lucky and now it is somewhat confusing for me looking back at it

+4  A: 

It seems to do rughly this:

It will package 2000 pictures into a subdirectory (0..8) using the line

 $sdir = ceil($x/2000) % 9

Spelled out: how many times does 2000 fit into $x. As you limit this to 9 subdirectories using the modulo 9, you would get the 18001rst photo into subdirectory 0 again.

The upper level changes therefore using 18001 as limit. All photos from 1..18000 go into directory 1. (The +1 just shifts the intervall to start from 1. That is the line

$dir = floor($x/18001) + 1;

Now you could go about it like this for 3 levels (pseudocode, as I do not know PHP):

function uploadfolder($x) {
  $numOfPics = 2000;
  $numOfFolders = 9;

  $topdir = ceil($x / ($numOfPics * $numOfFolders * $numOfFolders));
  $middir = floor($x / ($numOfPics * $numOfFolders)) % $numOfFolders + 1;
  $botdir = (floor($x / $numOfPics) % $numOfFolders) + 1;
  return "$topdir/$middir/$botdir";
}
Ralph Rickenbach
that is perfect, much more configurable too, thank you!
jasondavis