There is such a function

public function get_upload_dir($id) { for($i=2;$i>=1;$i--) { $dir_file_arr[]=ceil($id/pow(1000,$i)); } $dir_file_str=implode("/", $dir_file_arr); return $dir_file_str; } 

the point is that I pass the id function and it should return the path to the folder

in the loop, the number 2 is the number of folder branches

the maximum possible value of $ id is 1000 to the 3rd power, i.e. 1000 000 000 (degree - the number of branches + 1)

let's sort the function on a simple example, like this

 public function get_upload_dir($id) { for($i=2;$i>=1;$i--) { $dir_file_arr[]=ceil($id/pow(3,$i)); } $dir_file_str=implode("/", $dir_file_arr); return $dir_file_str; } 

maximum value here is 27

those. that's what i should get

id = 1 answer 1/1

id = 2 answer 1/1

id = 3 answer 1/1

id = 4 answer 1/2

id = 5 answer 1/2

id = 11 answer 2/4

id = 25 answer 3/9

How to fix the function so that to get the numbering in this form?

id = 1 answer 1/1

id = 2 answer 1/1

id = 3 answer 1/1

id = 4 answer 1/2

id = 5 answer 1/2

id = 11 answer 2/1

id = 25 answer 3/3

because there are 3 folders 1, 2, 3

There are 1, 2, 3 in these folders as well.

and now the function does so

there are 3 folders 1, 2, 3

each has 3 folders whose names are the ordinal number calculated by the formula

    1 answer 1

    The task is somehow muddled, but if I correctly understood that you should succeed in the end, then the solution is something like this :

     function get_upload_dir($id) { $p = 3; for ($i = 2; $i >= 1; $i--) { $dir = ceil($id / pow($p, $i)) % $p; $dir_file_arr[] = $dir > 0 ? $dir : $p; } $dir_file_str = implode("/", $dir_file_arr); return $dir_file_str; } 

    I hope you understand the purpose of $p :-)

    And advice: do not regret separators. Then it will be easier for you to parse your own code.

    Generally recommend sticking to any codestyle. Examples: Zend , PEAR .

    • Thank you very much, the code itself hashcode.ru/questions/254787/… - maxiimkoo
    • A link to the code why? Now it is not clear, did my answer solve your problem or not? - Johny
    • Yes, I decided, thank you very much! you wrote "The task is somehow messy described," so I wrote the link - maxiimkoo