Tree array

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a simple problem, but I don't found a solution for my problem.
I'm try, try and not found. If anybody help me, I very, very thankful
for this help.

Here is my problem:

I have a table with following data I have access:


I make chages add this fields to make a breadcrumbs functionality from
this system in the following fields I have stored this data:

page_id page_path       page_depth      page_order      page_has_childs
1       x       1       0       1       Home
2       x.1     2       0       1       Products
3       x.1     2       2       0       3D Cad
9       x.1.2   3       2       0       Contact
10      x.1.2   3       0       0       Knowledge

In page path I store the location from that row data, for example in
page_id 10, I have the following path x(Home), 1(Products) and
2(Knowledge). In breadcrumbs I'm show like this Home > Products >

My problem is, I need make a array tree with all data from database to
make a website map. anybody help me with this?

Thanks so much.

Leonardo da Silva Calado

Re: Tree array said the following on 30/05/2006 12:13:
Quoted text here. Click to load it

I'm not sure I understand your page_path system.  Why does 1 mean  
"Products" and 2 mean "Knowledge"?

The way I would do it is for each page, store the ID of its "parent", i.e.:

page_id   parent_id   title
1         NULL        Home
2         1           Products
3         1           3D Cad
9         2           Contact
10        2           Knowledge

That way, you can read the entire table into a PHP array, and then step  
through the array to build up a tree data structure.


Re: Tree array

In the system information is stored to make a breadcrumbs site
functionality, page_path stores de location in breadcrumbs from page.
For example: page_id 9 "Contact" have 2 levels of depht to home. It's
make the following breadcrumb Home > Products > Contact

Re: Tree array wrote:
Quoted text here. Click to load it

Which is still possible, using Oil's table.

I'm making a trail here, allthough with the build of the array this is
totally unnecessary:

function create_tree($parent=null, $depth=0, $trail='x' ){
    $return = array();
    $result = mysql_query("SELECT page_id, page_order, page_title
    FROM table
    WHERE page_parent='$parent'
    ORDER BY page_order");
    if(mysql_num_rows($result) > 0){
        while($row = mysql_fetch_assoc($result)){
            $return[$row['page_id']] = $row;
            $return[$row['page_id']]['depth'] = $depth;
            $return[$row['page_id']]['trail'] = $trail;
            $childs =
                 $return[$row['page_id']]['childs'] = $childs
    return $return;

$tree = create_tree();

function create_html_list($array){
    if(!is_array($array)) return '';
    $return = '<ul>';
    foreach($array as $row){
        $return .="<li>";
            $return .= create_html_list($row['childs']);
        $return .='</li>';
    $return = '</ul>';
    return $return;

echo create_html_list($tree);

 haven't tested the code, so maybe some debugging is in order.

Rik Wasmus

Re: Tree array wrote:
Quoted text here. Click to load it


I understand how you have it set up.  However, the way Oli proposes will be much  
easier to handle in the long run.

Your way means a lot of parsing of values, maintaining synchronization between  
fields and a lot of other extra effort.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline