|
List Info
Thread: Tree print
|
|
| Tree print |

|
2006-01-26 13:36:56 |
Hi I have this unlimited categories table:
products table:
pro_id pro_name
pro_parent_id
1 books
2 electronics 0
3 computers 0
6 germany 0
5 English 0
7 fezia 0
8 DRAMA1 0
9 dr Fausts 8
10 jow of malta 8
11 every man 8
12 every man s1 11
13 every man s2 11
14 every man s3 11
Just I want to prtint this tree!!!!
Thanx in advance
|
|
| Tree print |

|
2006-01-26 15:41:32 |
|
Hi, i'm pretty sure that this can be do in a proper way.
But it's a solution that works .
<?php
I suppose that you get the result from your database
as an array like this :
$categories = array(
array(1,'books',0 ),
array(2,'electronics',0),
array(3,'computers',0),
array(6,'germany',0),
array(5,'English',0),
array(7,'fezia',0),
array(8,'DRAMA1',0),
array(9,'dr Fausts', 8),
array(10,'jow of malta', 8),
array(11,'every man', 8),
array(12,'every man s1', 11),
array(13,'every man s2',11),
array(14,'every man s3',11));
//Then, i'm going to register all parents and their child in an array
$parents = array();
foreach ($categories as $category) {
//Is it the first child of this parents we register?
if
(is_array($parents[$category[2]])) { //not it's not, so we add it
to list for the parents $category[3]
$parents[$category[2]] []= $category[0];
}
else { //Yes it is
$parents[$category[2]] = array($category[0]);
}
}
$already_printed = array();
//I suppose that there is a root category which normally should be the case.
echo showTree(0, $parents);
//Recursive function that shows a tree
function showTree($parent_id, $parent_array) {
global $already_printed;
$html = '<ul>';
$parent = $parent_array[$parent_id];
foreach ($parent as $child) {
$html .= '<li>' . $child . '</li>';
if
(isParent($child, $parent_array) && !in_array($child,
$already_printed)) {
$already_printed []= $child;
$html .= showTree($child, $parent_array);
break;
}
}
$html .= '</ul>';
return $html;
}
function isPArent($parent_id, $parent_array) {
foreach($parent_array as $k => $v) {
if ($k == $parent_id) {
return true;
}
}
return false;
}
?>
|
| Tree print |

|
2006-01-26 15:44:03 |
|
SORRY there is no break here:
$html .= '<li>' . $child . '</li>';
if
(isParent($child, $parent_array) && !in_array($child,
$already_printed)) {
$already_printed []= $child;
$html .= showTree($child, $parent_array);
break; //There is no break here;
}
}
|
| Tree print |

|
2006-01-28 08:50:39 |
<? thaaaaaaaaaaaaaanx very much "Tanoor Dieng";
?>
|
|
[1-4]
|
|