RE: [PHP-DB] Recursively determine parent records?
| From: | Chris Gregors | Date: | Thu, 07 Oct 2004 13:56:35 +0000 |
| Subject: | RE: [PHP-DB] Recursively determine parent records? | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-36671@lists.php.net to get a copy of this message | ||
not to nit-pick:
1. I only do 1 sql query
2. Gijs Van Tulder uses recursion, while I use while loops. Similar
effect.
3. Gijs Van Tulder function only prints out the path as a series of
strings. I give it back in an array.
-----Original Message-----
From: point of no return [mailto:pointofnoreturn@earthlink.net]
Sent: Thursday, October 07, 2004 2:38 AM
To: php-db@lists.php.net
Subject: Re: [PHP-DB] Recursively determine parent records?
http://www.sitepoint.com/print/hierarchical-data-database
has a guide on how to do the same thing but in a single SQL query,
without looping at all. It is very interesting reading and requires a
slight restructuring of how the records are stored to pull off but with
the wonderful result in being much faster to retrieve.
Chris Gregors wrote:
> Here's a piece of some code I wrote that does what you want. You'll
need
> to change the WhoIsMyParent() function to match your database
> abstraction and schema.
>
> $array = BreadCrumbs($id);
> print_r($array);
> exit;
>
> function BreadCrumbs($parent) {
> // construct an array of indexes that represents the path back
> to the
> // root from my current location in the tree.
> $PathStack = array();
> array_push($PathStack,$parent);
> $MyParent = WhoIsMyParent($parent);
> while ($MyParent != 0) { // 0 = top of
> the tree
> $PathStack[] = $MyParent;
> $MyParent = WhoIsMyParent($MyParent);
> }
> $PathStack = array_reverse($PathStack);
> return $PathStack;
> }
>
> function WhoIsMyParent($id) {
> $row=FetchRow(QueryDb("select parent from sometable where
> id=".$id));
> if ($row == NULL) return 0;
> return $row['parent'];
> }
>
> -----Original Message-----
> From: Murray @ PlanetThoughtful [mailto:lists@planetthoughtful.org]
> Sent: Tuesday, October 05, 2004 11:58 PM
> To: php-db@lists.php.net
> Subject: [PHP-DB] Recursively determine parent records?
>
>
>
>
> Hi All,
>
>
>
> I have recordset that contains a hierarchical representation of
records
> where a record's [parentid] field contains the recid of the record
> 'above'
> the current record.
>
>
>
> A representation might look like this:
>
>
>
> Recid, parentid, title
>
>
>
> 1, 0, Top level record
>
> 2, 0, Another top level record
>
> 3, 1, A record under the top level record
>
> 4, 3, Another level
>
> 5, 2, A record under the second top level record
>
>
>
> If I have currently retrieved the record with recid of 4, I want to
work
> out
> the 'chain' of records that lead back to the top level record it has
> been
> created under.
>
>
>
> In this instance, that chain would look like:
>
>
>
> 4, 3, Another level
>
> 3, 1, A record under the top level record
>
> 1, 0, Top level record
>
>
>
> I'm wondering if anyone can help me work out how to achieve this?
>
>
>
> Many thanks in advance!
>
>
>
> Much warmth,
>
>
>
> Murray
>
> <http://www.planetthoughtful.org/>
> http://www.planetthoughtful.org
>
> Building a thoughtful planet,
>
> One quirky comment at a time.
>
>
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Attachment: [application/x-pkcs7-signature] smime.p7s
Attachment: [application/x-pkcs7-signature] smime.p7s