I have the following code, which works as I need it to, however, it repeats itself over and over.
I'm relatively new to PHP and haven't grasped recursive functions yet. Which I've been told I'll need to understand as this function could potentially have many levels.
if ($_POST['parent'] == "None") {
$parent = "";
} else {
$parent = $connection->real_escape_string($_POST['parent']);
}
if (!empty($parent)) {
$query = $connection->query("SELECT * FROM pages WHERE filename='$parent'");
while($row = $query->fetch_array()) {
$path_arr = explode('/',$row['path_to']);
end($path_arr);
$key = key($path_arr)-1;
$parent1 = $path_arr[$key];
if (!empty($parent1)) {
$query = $connection->query("SELECT * FROM pages WHERE filename='$parent1'");
while($row = $query->fetch_array()) {
$path_arr = explode('/',$row['path_to']);
end($path_arr);
$key = key($path_arr)-1;
$parent2 = $path_arr[$key];
if (!empty($parent2)) {
$query = $connection->query("SELECT * FROM pages WHERE filename='$parent2'");
while($row = $query->fetch_array()) {
$path_arr = explode('/',$row['path_to']);
end($path_arr);
$key = key($path_arr)-1;
$parent3 = $path_arr[$key];
if (!empty($parent3)) {
$query = $connection->query("SELECT * FROM pages WHERE filename='$parent3'");
while($row = $query->fetch_array()) {
$path_arr = explode('/',$row['path_to']);
end($path_arr);
$key = key($path_arr)-1;
$parent4 = $path_arr[$key];
}
}
}
}
}
}
}
}
$path_to = rtrim(ltrim($parent4.'/'.$parent3.'/'.$parent2.'/'.$parent1.'/'.$parent.'/'.$filename,'/'),'/');
As you can see there are minimal differences within the nested while loops, but I'm really not sure how to make the function loop for each level.
I was pointed to this, and while I understand what the factorial is, I still am unsure on how to refactor my code into a looping function.
Any pointers?
UPDATE
Certainly moving in the right direction of what I'm hoping to achieve with Simon André Forsberg's suggestion
However (I probably should have made this clearer in my explanation), The loop(s) determine whether a 'file' (a database entry, masquerading as a file) has a parent 'file', returns that parent 'file', then checks that parent 'file' for a parent 'file' and returns it and so on and so on until there are no more parents.
It is used to create a dynamic path directory.
like so /parent4/parent3/parent2/parent1/filename/
The line of code that follows the loops (again, I probably should have included this), is below...
$path_to = rtrim(ltrim($parent4.'/'.$parent3.'/'.$parent2.'/'.$parent1.'/'.$parent.'/'.$filename,'/'),'/');
So each level of the loop outputs to this string.
I have a feeling I may have to do this using palacsint's method of taking the working part of each loop and creating that as a function.
I may be wrong but could Simon's method output to an array? first parent in pos 0, second in pos 1, etc?
However, then the query would have to have the filename='$parent'
updated to look at the last array entry.
$query = $connection->query("SELECT * FROM pages WHERE filename='$parent'");
Have I bitten off more than I can chew?