Functionally premature

The following functions are intended to return a set of ids, and their
respective children, as a single dimensional array ($ids), however, I'm
getting nothing. I think the problem is that the results are being
returned prematurely - before the recursion has finished - but I'm not
quite sure how to fix that. Any help appreciated.

Note: Also posted at alt.php.mysql

function array_merger($a,$b){
$count = count($b);
for ($i = 0; $i < $count; $i++) {
$a[]=$b[$i];
}
return $a;
}

function display_children($parent) {
$ids=array();
$childres=array();

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)) {//
$ids[]=$row['id'];
$childres[]=display_children($row['id']);
}
$count=count($childres);
for($i=0;$i<$count;$i++){
$ids=array_merger($ids,$childres[$i]);
}
return $ids;
}
zac.carey [ Sa, 02 Dezember 2006 19:00 ] [ ID #1555250 ]

Re: Functionally premature

strawberry wrote:

> The following functions are intended to return a set of ids, and their
> respective children, as a single dimensional array ($ids), however, I'm
> getting nothing. I think the problem is that the results are being
> returned prematurely - before the recursion has finished - but I'm not
> quite sure how to fix that. Any help appreciated.
>
> Note: Also posted at alt.php.mysql
>
> function array_merger($a,$b){
> $count = count($b);
> for ($i = 0; $i < $count; $i++) {
> $a[]=$b[$i];
> }
> return $a;
> }
>
> function display_children($parent) {
> $ids=array();
> $childres=array();
>
> 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)) {//
> $ids[]=$row['id'];
> $childres[]=display_children($row['id']);
> }
> $count=count($childres);
> for($i=0;$i<$count;$i++){
> $ids=array_merger($ids,$childres[$i]);
> }
> return $ids;
> }

It really can be this simple?

$ids=display_children('null');

Anyone using an adjacency list model must need to do this kind of thing
ALL the time so I'm printing the script here in its entirety in case
anyone else can make use of it, or perhaps improve it.

<?php
function array_merger($a,$b){
$count = count($b);
for ($i = 0; $i < $count; $i++) {
$a[]=$b[$i];
}
return $a;
}

function display_children($parent) {
$ids=array();
$childres=array();

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)) {
$ids[]=$row['id'];
$childres[]=display_children($row['id']);
}
$count=count($childres);
for($i=0;$i<$count;$i++){
$ids=array_merger($ids,$childres[$i]);
}
return $ids;
}

include('../gantt.inc'); //Connection script

$ids=display_children('null');
print_r($ids);

?>
zac.carey [ Mo, 04 Dezember 2006 11:43 ] [ ID #1556919 ]

Re: Functionally premature

strawberry wrote:

> The following functions are intended to return a set of ids, and their
> respective children, as a single dimensional array ($ids), however, I'm
> getting nothing. I think the problem is that the results are being
> returned prematurely - before the recursion has finished - but I'm not
> quite sure how to fix that. Any help appreciated.
>
> Note: Also posted at alt.php.mysql
>
> function array_merger($a,$b){
> $count = count($b);
> for ($i = 0; $i < $count; $i++) {
> $a[]=$b[$i];
> }
> return $a;
> }
>
> function display_children($parent) {
> $ids=array();
> $childres=array();
>
> 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)) {//
> $ids[]=$row['id'];
> $childres[]=display_children($row['id']);
> }
> $count=count($childres);
> for($i=0;$i<$count;$i++){
> $ids=array_merger($ids,$childres[$i]);
> }
> return $ids;
> }

It really can be this simple?

$ids=display_children('null');

Anyone using an adjacency list model must need to do this kind of thing
ALL the time so I'm printing the script here in its entirety in case
anyone else can make use of it, or perhaps improve it.

<?php
function array_merger($a,$b){
$count = count($b);
for ($i = 0; $i < $count; $i++) {
$a[]=$b[$i];
}
return $a;
}

function display_children($parent) {
$ids=array();
$childres=array();

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)) {
$ids[]=$row['id'];
$childres[]=display_children($row['id']);
}
$count=count($childres);
for($i=0;$i<$count;$i++){
$ids=array_merger($ids,$childres[$i]);
}
return $ids;
}

include('../gantt.inc'); //Connection script

$ids=display_children('null');
print_r($ids);

?>
zac.carey [ Mo, 04 Dezember 2006 11:43 ] [ ID #1560567 ]
PHP » alt.php.sql » Functionally premature

Vorheriges Thema: showing multiple feilds in a drop down for selection
Nächstes Thema: How Many Cities?