You are here: Re: Recursive array from id/parent_id relationship in MySQL result set « PHP Language « IT news, forums, messages
Re: Recursive array from id/parent_id relationship in MySQL result set

Posted by Stefan Rybacki on 12/15/05 19:19

Hilarion wrote:
>> Zilla wrote:
>> > ...
>
>
> Stefan Rybacki <stefan.rybacki@gmx.net> wrote:
>
>> Here is another solution:
>
>
> :) Actually I also wrote my solution before posting first post in this
> topic,
> but I decided not to post it when I saw your (Stefan) post reading:
>

me too, but after reading Zillas post I thought of presenting another, maybe
more straight forward, solution can't be wrong ;)

>> First you could start with creating items in a single array from the
>> mysql resultset, use the id as key.
>> Afterwards you simply can create a tree like array by using the first
>> array accessed by the parentid.
>
>
> This was exact description of my solution.
>
> Your script looks almost like mine (differences in some details, which
> I described below and in names of variables).
>

Ok, go ahead.

>
>> <?php
>>
>> //creates a node array
>> function make_node($id,$uri,$name,$parent) {
>> return array(
>>
>> 'id'=>$id,'uri'=>$uri,'name'=>$name,'parent_id'=>$parent,'sub_forums'=>array()
>>
>> );
>> }
>
>
> You do not really need that function because the result from the DB is
> almost
> exactly like what you need.

Correct, I did it because I thought an extraction of this task can't be wrong
since the OP may wants to rethink this kind of storage in a node however its not
really needed yet.

>
>
>> //target array
>> $root=array();
>
>
> In my solution I created $root (under different name)
> at the end of the script.

yeah. Its equal.

>
>
>> //helps us to access items fast
>> $helptable=array();
>>
>> //insert root node
>> $helptable[0]=array('sub_forums'=>&$root);
>>
>> //get items
>> $con=mysql_connect("localhost","root","");
>> mysql_select_db("test");
>>
>> $res=mysql_query("SELECT * FROM testtable");
>>
>> //first collect all items
>> while($row=mysql_fetch_assoc($res)) {
>> //insert item into help table
>> $helptable[$row['id']] =
>> make_node($row['id'],$row['uri'],$row['name'],$row['parent_id']);
>
>
> I've done it like this:
>
> $helptable[ $row['id'] ] = $row;
> $helptable[ $row['id'] ][ 'sub_forums' ] = array();
>

yes thats fine.


> and it also could be done like this:
>
> $row[ 'sub_forums' ] = array();
> $helptable[ $row['id'] ] = $row;
>

sure.

>
>> } // while
>>
>> //now run thru all items and build the tree array (just means add
>> childs to //parents)
>> foreach ($helptable as $k=>$v) {
>> if ($k!=0) {
>>
>>
>> $helptable[$helptable[$k]['parent_id']]['sub_forums'][count($helptable[$helptable[$k]['parent_id']]['sub_forums'])]=&$helptable[$k];
>>
>
>
> I made it a bit shorter like this:
>
> $helptable[$v['parent_id']]['sub_forums'][] = & $helptable[$k];
>

yes thats even better and more readable, my first solution worked with
array_push but array_push didn't take references of variables so I came up with
the line above with missing the obvious ;)


>
>> }
>> }
>
>
> Here I created my $root:
>
> $root = $helptable[0]['sub_forums'];
>

yep almost the same.

>
>> unset($helptable);
>> //now $root contains the desired tree
>> print_r($root);
>>
>> mysql_free_result($res);
>> mysql_close($con);
>>
>> ?>
>

Well done, Hilarion ;)

>
>
> Hilarion


Regards
Stefan

 

Navigation:

[Reply to this 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

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