patternphpMinor
Need to condense the following into a looping function
Viewed 0 times
loopingtheneedintocondensefunctionfollowing
Problem
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;
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;
Solution
You seem to always end with the check: Is there another parent? Or as it can be formulated, while there is another parent, grab that parent.
Luckily for you, there are while loops.
This code will continue looping while there is a parent available. Note that the new parent uses the same variable as the previous one, this is an important part in making this loop work.
Other suggestions:
-
Use prepared statements! It is good that you seem to sanitize your inputs by using the
-
Since this code only gets the first result, you don't need the inner while loop and can replace it by a
-
I do have to ask though: What's the point in just grabbing the parent until there are no more parents? To store all the parents in an array, let's do like this:
Now, when this loop finishes,
Luckily for you, there are while loops.
while (!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;
$parent = $path_arr[$key];
break; // break from the inner loop that fetches each row
}
}This code will continue looping while there is a parent available. Note that the new parent uses the same variable as the previous one, this is an important part in making this loop work.
Other suggestions:
-
Use prepared statements! It is good that you seem to sanitize your inputs by using the
real_escape_string method. But prepared statements is always better. And then you wouldn't have to use the real_escape_string call.-
Since this code only gets the first result, you don't need the inner while loop and can replace it by a
if instead.while (!empty($parent)) {
$query = $connection->query("SELECT * FROM pages WHERE filename='$parent'");
if ($row = $query->fetch_array()) {
$path_arr = explode('/',$row['path_to']);
end($path_arr);
$key = key($path_arr)-1;
$parent = $path_arr[$key];
}
}-
I do have to ask though: What's the point in just grabbing the parent until there are no more parents? To store all the parents in an array, let's do like this:
$parents = array();
while (!empty($parent)) {
$parents[] = $parent; // add parent to the array
$query = $connection->query("SELECT * FROM pages WHERE filename='$parent'");
if ($row = $query->fetch_array()) {
$path_arr = explode('/',$row['path_to']);
end($path_arr);
$key = key($path_arr)-1;
$parent = $path_arr[$key];
}
}Now, when this loop finishes,
$parents contains all the parents that was not empty. Now you can loop through that using a foreach loop, or use it with implode or whatever you'd like :)Code Snippets
while (!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;
$parent = $path_arr[$key];
break; // break from the inner loop that fetches each row
}
}while (!empty($parent)) {
$query = $connection->query("SELECT * FROM pages WHERE filename='$parent'");
if ($row = $query->fetch_array()) {
$path_arr = explode('/',$row['path_to']);
end($path_arr);
$key = key($path_arr)-1;
$parent = $path_arr[$key];
}
}$parents = array();
while (!empty($parent)) {
$parents[] = $parent; // add parent to the array
$query = $connection->query("SELECT * FROM pages WHERE filename='$parent'");
if ($row = $query->fetch_array()) {
$path_arr = explode('/',$row['path_to']);
end($path_arr);
$key = key($path_arr)-1;
$parent = $path_arr[$key];
}
}Context
StackExchange Code Review Q#44682, answer score: 7
Revisions (0)
No revisions yet.