Reply to Re: algorithm help

Your name:

Reply:


Posted by Steve on 10/22/07 13:13

"The Natural Philosopher" <a@b.c> wrote in message
news:1192832545.26334.0@proxy02.news.clara.net...
> uidzer0 wrote:
>> Hey everyone,
>>
>> I'm having a little trouble writing an algorithm to create a
>> hierarchal structure from a database which looks like this:
>>
>> Code:
>>
>> parent_id | child_id |
>> ----------------+-------------+
>> 1 | 2 |
>> 2 | 3 |
>> 3 | 4 |
>> 3 | 5 |
>> 5 | 6 |
>> 5 | 7 |
>>
>>
>> So coming out of the database, I get an array which looks like this:
>>
>> Code:
>>
>> Array
>> (
>> [0] => Array
>> (
>> [parent_id] => 1
>> [child_id] => 2
>> )
>>
>> [1] => Array
>> (
>> [parent_id] => 2
>> [child_id] => 3
>> )
>>
>> [2] => Array
>> (
>> [parent_id] => 3
>> [child_id] => 4
>> )
>> ...
>>
>>
>> I would like to put this into an array which represents the following
>> structure:
>>
>> Code:
>>
>> 1
>> \
>> 2
>> \
>> 3
>> / \
>> 4 5
>> / \
>> 6 7
>>
>>
>> Can anyone point me in the right direction? Let me know if this isn't
>> clear - I can try to elaborate.
>>
>> Thanks!
>>
>> Ben
>>
>
>
> Try a recursive call to the database..

that's wretched! your db is needlessly put through the ringer!

if you're going to have the db do it, then put the recursion in a udf in the
db. that way there's only one call to the db and one result set returned.
plenty of examples of this on the net for any db you want to use.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация