Forum Moderators: coopster
Here's the problem code:
function findChildren( $parent ){
$children = array();
$query = "SELECT * FROM atree WHERE parent_id = '$parent'";
$result = mysql_query($query) or die("Query Failed:".$query." Error message:".mysql_error());
$num_of_rows = mysql_num_rows($result);
$children[] = $parent;
echo"test:".$children[0]."<br>";
echo"After push: $children <br>";
if( $num_of_rows > 0 ){
for($i=0;$i<$num_of_rows;$i++){
$get = mysql_fetch_array($result);
echo"this is the child ";
echo $get["id"]."<br>";
array_merge( $children, findChildren( $get["id"] ) );
}
}
echo"Returning the children of $parent :$children <br>";
return $children;
}
Now I have a problem where, the that prints the $children[0] element prints fine... it prints out what I set it to by doing the $children[] = $parent
When I print just by echoing $children it's nothing.... also, when the merge is done, it's seeing the 2 array's as nothing, and returns nothing....
DO I have a spelling mistake in variables? dno't think so... anyone help me on this, I'm completely stumped.
Finding all children in a single parent tree mysql table(layout --> 2 fields, id, parent_id)
PHP5
MySQL4
Apache2
Windows Xp Pro machine...
array_merge() has to be equated to something...
ie $children = array_merge($children, $morechildren );
I thought it was similar to array_push,pop etc....
I still have the problem with using the actually array now....
count($children) works to count howmany is in there, but when I go to do a for loop using each element in a SQL query, it shows up as blank.
But it's not a solution... maybe someone can help me out.
When you use $var = array();
for some reason I couldn't do
for ($x=0;$x<count($var);$x++)
$temp = $var[$x];
echo $temp;
}
It would print blank everytime.... but if I did
$temp = array_pop($var);
it worked.
Problem, I need to go through the array 2 times doing certain things each time.... if I pop the array, that means I have to keep 2 copies of it at one point, so when I pop, I can pop another array later... that just doesn't seem like I should have to do it that way....
any suggestions?
while()and
count()to iterate though an array, try using
foreach(), it's a lot simpler and straightforward, and probably faster. You also don't have to know what the keys are.
I'd probably have posted more here if I could have followed you a bit better. It helps if, in the beginning of the post, you state what your code is supposed to do. Also a good idea to try hammering out obvious errors before you post. e.g.,
echo "After push: $children <br>";will always yield: 'After push: Array' since
$childrenis an array. This is especially the case with code that's a bit complicated to understand, as recursive functions nearly always are. This way you don't have to spread your message over a number of postings, and you really know much better what it is what you need to ask. And you have a much better chance of getting a quality reply.
When writing recursive functions, it helps to have a value like
$max_depthto prevent infinite recursions. Set up a counter that gets incremented each time you recurse. If your function hits
$max_depth, get out by returning the current value, or an error flag.