473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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;
}

Dec 2 '06 #1
1 1222
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);

?>

Dec 4 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Seagull Manager | last post by:
Running Apache 1.3, PHP 4.3, and WinXP, configured acc. to instructions on apache manual and php manual (as far as I can see), but getting "internal server error" in browser... log says "Premature...
1
by: Jason Ho-Ching | last post by:
Can anyone tell me about this error I am getting? Premature end of script headers: main.php Thanks! ##-----------------------------------------------## Article posted from PHP Freaks...
0
by: Greeshma | last post by:
Hi all, I have successfully configured Apache 2.0.4 with RedHat 7.3 and use Python 2.2.3 for cgi scripts. When I start Apache manually through /usr/local/apache/bin/apachectl startssl, I can...
1
by: Jatinder Singh | last post by:
I am running a CGI Programme. which is throwing Premature script error for some inputs. I have checked and couldn't fig out the problem. Even error log is empty. Can anybody help me out of this...
1
by: nomadx | last post by:
hello, could someone please check this out? #usr/local/bin/perl # Filelist.pl version 0.1 use strict; use CGI qw(:all); use CGI::Pretty; #use CGI::Carp qw(fatalsToBrowser); use Cwd; my...
1
by: Wayne Deleersnyder | last post by:
Hi, I'm a newbie at Perl and CGI and hoping someone can point me in the right direction. Recently I've been going through an older book I have on CGI programming to get a start. Most of it...
5
by: Erich Schreiber | last post by:
In the Python Library Reference the explanation of the time.sleep() function reads amongst others: > The actual suspension time may be less than that requested because > any caught signal will...
2
by: Ian Sykes | last post by:
I have a C# program which terminates prematurely, with no error or exception being raised, the termination point varying with what diagnostic code is present. The termination point appears to be...
5
by: kardon33 | last post by:
Im getting these errors when i use a module, could anyone tell me what they think wrong, its the second one that i think it the problem but i don't know what it means so i cant fix it, Thank you. ...
1
by: Christoph Burschka | last post by:
I have compiled PHP 5.2.3 as a CGI binary, and this works. I can, for example, do the following: $ php.cgi hello_world.php X-Powered-By: PHP/5.2.3 Content-type: text/html Hello World! ===
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.