473,324 Members | 2,417 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,324 software developers and data experts.

Threading articles from a SQL DB using PHP

BN

I have a table with (for brevity) 2 columns. RecID and ParentID.

All absolute or generation 0 PARENT records will have a ParentID of 0
(zero) but their children will have their parentID pointed to an
existing parent's RecID (a parent of any generation).

I want to list out the parents with all their children
and grandchildren under them and then start listing the next parent and
all the children and grand children under them etc.

example

RECID ParentID
PARENT 1 0
child 2 1 child of p1
child 6 1 child of p1
child 4 6 child of child
child 5 4 child of child of child
PARENT 3 0 next parent of gen 0
child 7 3 child of p3 or RecID 3
etc.. I want to list it in that order.

When I select it of the PostgreSQL database, I get records but not in
the above order. I have to load it into a temporary array and then put
it into that threaded ordered. I am using PEAR:DB for access to the
PGSQL database.

How do I do this? Arrays? Maps?

Can elements in maps be listed through iteration?

Are there processor efficient ways to do this?

Any help would be appreciated!!
Sep 18 '05 #1
2 1274
I had come across a similiar exercise in a C++ class I had in college.
What you are describing is a binary tree (except in this case it has
more than just two children), where the branches show generations. I
have to tell you the methodology in C++ and then you can apply it in
PHP. We solved this through the use of a linked list with a set of
pointers and recursion. Each object points to its children and while
the pointers are not null, it calls the childs method and starts the
process all over again until it reaches the end. When it hits null, it
climbs up and down into the next child. When children are exhausted for
a given parent, it calls its parent etc back up the tree. I have found
an article which shows the ideas, problems, and a few code solutions.

http://cslibrary.stanford.edu/110/BinaryTrees.html

I hope this helps and makes sense.

Sep 19 '05 #2
> I have a table with (for brevity) 2 columns. RecID and ParentID.

All absolute or generation 0 PARENT records will have a ParentID of 0
(zero) but their children will have their parentID pointed to an
existing parent's RecID (a parent of any generation).

I want to list out the parents with all their children
and grandchildren under them and then start listing the next parent and
all the children and grand children under them etc.

example

RECID ParentID
PARENT 1 0
child 2 1 child of p1
child 6 1 child of p1
child 4 6 child of child
child 5 4 child of child of child
PARENT 3 0 next parent of gen 0
child 7 3 child of p3 or RecID 3
etc.. I want to list it in that order.

When I select it of the PostgreSQL database, I get records but not in
the above order. I have to load it into a temporary array and then put
it into that threaded ordered. I am using PEAR:DB for access to the
PGSQL database.

How do I do this? Arrays? Maps?

Can elements in maps be listed through iteration?

Are there processor efficient ways to do this?

If it'd be Oracle, then you could use hierarchical query, but PostgreSQL
does not support it (AFAIK).

You could do it with recursive function retrieving children of
a specified parent (which means multiple selects on database):

<?php

function get_children( $connection, $callback_function, $parentid = 0 )
{
$qry = 'SELECT recid, parentid, name '
. 'FROM my_table '
. 'WHERE parentid = ' . $parentid . ' '
. 'ORDER BY recid '
;
$data = query_the_database_in_some_way( $connection, $qry );
if ($data===FALSE)
{
// Throw exception or issue warning and:
return FALSE;
}
foreach( $data as $row )
{
call_user_func( $callback_function, $row );
if (!get_children( $connection, $callback_function, $row['recid'] ))
return false;
}
return TRUE;
}

$conn = establish_database_connection_in_some_way();

function print_results( $row )
{
echo "<tr>\n";
foreach( $row as $cell )
echo '<td>' . htmlspecialchars( $cell ) . "</td>\n";
echo "</tr>\n";
}

echo "<table>\n";
get_children( $conn, 'print_results' );
echo "</table>\n";

?>
Hilarion
Sep 20 '05 #3

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

Similar topics

1
by: Dave | last post by:
VB is relatively new to our company and I am putting together some general guidelines for people to follow when deploying their applications. On the General Tab of the Project Properties dialog,...
4
by: Charles A. Lackman | last post by:
Hello and thank you for your assistance. I have attempted to accomplish what I need using delegates with no success. i.e. //Button Click// Dim PollThread As Threading.Thread PollThread = New...
7
by: Tom B | last post by:
I've written the code below to try and figure out how threading works. My assumption is that when starting a new thread it would run at the same time as the original thread. Am I wrong? I've...
3
by: rollasoc | last post by:
Hi, Having a slight problem with how to pass parameters to functions between threads. I have a form with a progress bar and a description label on it. It has a public function...
9
by: Edward | last post by:
Hello I hope someone could help me I'm trying to prevent code from running before the thread I created completes. Here's the code snippet DataTransformerWorker dtw = new...
5
by: - Dan - | last post by:
anyone know any good threading articles? I don't know all the good places to search. I'd like to find an article that can discuss passing variables to a thread and getting info back that...
10
by: Bob | last post by:
Okay, I've done this for years but now I'm going to question it just because this idea has been at the back of my head since I started using DotNet... My WinForms app queries a database every 60...
7
by: Tim | last post by:
Hi, I have developed a windows forms application that works fine on a single thread. Part of the requirements of this app is that it monitors the status of a couple of services. So I think...
6
by: hzgt9b | last post by:
Using VS 2003, .NET: I developed a windows application that performs several actions based on an input file. The application displays a progress bar as each action executes. Based on new...
1
by: Charlie Brown | last post by:
I am looking for any good books or online articles dealing with vb.net threading in .net 2.0 Yes, I could google this info myself, but I want good information that others found useful and would...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.