At 11:16 25-2-03, you wrote:
Hi all!

i have to solve one problem, but i have no idea how to make this. I have
got a tree strucutre in mysql table, id - parent - name. I need make
function, which return array which will contains a strucutre of this
tree-menu, table example:

ID     Parent Name
------ ------ ----------------------
1      0      Components
4      1      Main boards
5      1      HD
7      5      IDE
8      5      SCSI

and result:

$array = Array(
        "id" => "1",
        "name" => "Components",
        "sub" => array(
                "id" => "4",
                "name" => "Mainboards",
                "sub" => no_array  (no sub pages)),
                array(
                "id" => "5",
                "name" => "HD",
                "sub" => Array(
                         "id" => "7",
                         "name" => "IDE",
                         "sub" => no_array  (no sub pages)),
                         Array(
                         "id" => "8",
                         "name" => "SCSI",
                         "sub" => no_array (no sub pages))
                )

);

an so on, source table has got about 300 items, so i need function which
make an array for next work with this tree structure.

thanks a lot for some help!

jiri.nemec at menea.cz
http://www.menea.cz
ICQ: 114651500

hi jiri,
i found this an interesting question, so i tried to think up some rough function to do it.
But while writing that, i wondered why you wanted to make the array with the structure you propose. Are you sure this is the easiest way to do it?


The problem with your proposal is in my eyes that for instance the SCSI name will be in $array['sub']['sub']['sub']['name'];

So i stopped trying to finish this function because my problem is i cannot think of an elegant way to indicate where the subarray should be glued to.

The rough idea of this could be an iterative function that does a query every time:


function buildtree($parentID=0, $arrayposition) { global $Tree;

$result=query("SELECT ID, Parent,Name FROM sourcetable WHERE Parent=$parentID") or die('error:'.mysql_error());

walkthrough $result //use your favourite loop to go through the result set
{
$branch=array($ID,$Name, 'sub'=> (buildtree( $ID, $arrayposition )) );
}


return $branch;
}



It may be quicker to just load in the entire table in a simpe array in the same format as the table, and then use it in a smart way.

So q: could you elaborate on the why of the array structure you proposed?




-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to