noob push/merge problem

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.
zac.carey [ Fr, 01 Dezember 2006 19:18 ] [ ID #1554388 ]

Re: noob push/merge problem

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>";
}
zac.carey [ Sa, 02 Dezember 2006 00:19 ] [ ID #1554389 ]

Re: noob push/merge problem

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
Paul Lautman [ So, 03 Dezember 2006 18:45 ] [ ID #1555891 ]
PHP » alt.php.sql » noob push/merge problem

Vorheriges Thema: How Many Cities?
Nächstes Thema: Check Please.