468,249 Members | 1,502 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

Tree array

Hi,

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:

page_id
page_path
page_depth
page_order
page_has_childs
page_title

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
page_title
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 >
Knowledge.

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

May 30 '06 #1
4 2640
le*************@gmail.com said the following on 30/05/2006 12:13:

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
page_title
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 >
Knowledge.


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.
--
Oli
May 30 '06 #2
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

May 30 '06 #3
Rik
le*************@gmail.com wrote:
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


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 =
create_tree($row['page_id'],$depth+1,$trail.'.'.$row['page_id']);
if(!empty($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>{$row['page_title']}";
if(isset($row['childs'])&&is_array($row['childs']){
$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.

Grtz,
--
Rik Wasmus
May 30 '06 #4
le*************@gmail.com wrote:
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


Leonardo,

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.
js*******@attglobal.net
==================
May 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jeffrey Silverman | last post: by
7 posts views Thread by pembed2003 | last post: by
7 posts views Thread by Casper | last post: by
3 posts views Thread by tsunami | last post: by
8 posts views Thread by ashore | last post: by
2 posts views Thread by uche | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.