|
Posted by Paul Lautman on 12/03/06 17:45
strawberry wrote:
> strawberry wrote:
>> I know this is probably completely straightforward but how do can
>> adapt this function so as to merge the results of the recursive
>> queries into a single, flat array?
>>
>> function display_children($parent) {
>> if($parent == 'null'){
>> $query = "SELECT `id` FROM `task` WHERE ISNULL(`parent`);";}else{
>>
>> $query = "SELECT `id` FROM `task` WHERE `parent`= '$parent';";}
>> $result = mysql_query($query);
>> while ($row = mysql_fetch_array($result)) {
>> display_children($row['id']);
>> }
>> }
>>
>> So if the results of the 1st query was 1,2,3
>> and the results of the first recursion was 11,21,31
>> and the results of second recursion was 118,213,315
>>
>> I'd end up with array(1,2,3,11,21,31,118,213,315)
>>
>> As always, any help appreciated.
>
> here's my stab at it. it doesn't work - but i'm not sure why not:
>
> function display_children($parent) {
>
> if($parent == 'null'){
> $query = "SELECT `id` FROM `task` WHERE ISNULL(`parent`);";}else{
>
> $query = "SELECT `id` FROM `task` WHERE `parent`= '$parent';";}
> $result = mysql_query($query);
> while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
> if ( !is_array($myarray) ) $myarray = array();
> $myarray[] = array_push($myarray,$row['id']);
> display_children($row['id']);
> }
> return $myarray;
> echo "<br>";
> }
It doesn't work because you're not doing anything with the return value of
display_children() and $myarray is a local array, local to each recursion of
display_children().
You want something along the lines of:
$myarray = array_merge($myarray,display_children($row['id']));
although I haven't examined the logic fully.
You should be
Navigation:
[Reply to this message]
|