List Info

Thread: Tree print




Tree print
user name
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
user name
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

&nbsp;I suppose that you get the result from your database
as an array like this :

$categories = array(
&nbsp; &nbsp;   ;  array(1,'books',0 ),
   ; &nbsp; &nbsp; array(2,'electronics',0),
 ; &nbsp; &nbsp; &nbsp; array(3,'computers',0),
 &nbsp;   ; &nbsp; array(6,'germany',0),
 &nbsp; &nbsp; &nbsp;  array(5,'English',0),
 &nbsp; &nbsp; &nbsp;  array(7,'fezia',0),
  ; &nbsp; &nbsp;  array(8,'DRAMA1',0),
 &nbsp; &nbsp; &nbsp;  array(9,'dr Fausts', 8),
 ; &nbsp; &nbsp; &nbsp; array(10,'jow of malta', 8),
 ; &nbsp; &nbsp; &nbsp; array(11,'every man', 8),
 ; &nbsp; &nbsp; &nbsp; array(12,'every man s1', 11),
&nbsp; &nbsp; &nbsp; &nbsp; array(13,'every man s2',11),
  ; &nbsp; &nbsp;  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) {
 &nbsp;   //Is it the first child of this parents we register?
 &nbsp; &nbsp; &nbsp;  if (is_array($parents[$category[2]])) { //not it's not,  so we add it to list for the parents $category[3]
 &nbsp;   ; &nbsp; &nbsp; &nbsp; $parents[$category[2]] []= $category[0]; 
 &nbsp; &nbsp;   ; }
 &nbsp;   ; &nbsp; else { //Yes it is
   ; &nbsp; &nbsp; &nbsp; &nbsp; $parents[$category[2]] = array($category[0]);&nbsp;
 &nbsp; &nbsp;   ; }
}

$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) {
 &nbsp; 
 &nbsp;  global $already_printed;
&nbsp; &nbsp; $html = '<ul>;';
   ; $parent = $parent_array[$parent_id];
&nbsp; &nbsp; foreach ($parent as $child) {
 &nbsp;   ; &nbsp; $html .= '<li>;' . $child . '</li&gt;';
 ; &nbsp; &nbsp; &nbsp; &nbsp;   if (isParent($child, $parent_array) && !in_array($child, $already_printed)) {
 &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   $already_printed []= $child;
&nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; $html .= showTree($child, $parent_array);
&nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp;  break;
&nbsp; &nbsp;   ; &nbsp; &nbsp;  }
 &nbsp;  }
 &nbsp;  $html .= '</ul&gt;';
 ; &nbsp; return $html;
}


function isPArent($parent_id, $parent_array) {

 ; &nbsp; &nbsp; &nbsp; foreach($parent_array as $k => $v) {
 &nbsp;   ; &nbsp; &nbsp; &nbsp; if ($k == $parent_id) {
 &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   return true;
&nbsp; &nbsp; &nbsp;   ; &nbsp;  }
 &nbsp;   ; &nbsp; }
 &nbsp;   ; &nbsp; return false;
}



?>;
Tree print
user name
2006-01-26 15:44:03
SORRY there is no break here:

 

 &nbsp; &nbsp; &nbsp;  $html .= '<li>;' . $child . '</li&gt;';
 ; &nbsp; &nbsp; &nbsp; &nbsp;   if (isParent($child, $parent_array) && !in_array($child, $already_printed)) {
 &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   $already_printed []= $child;
&nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; $html .= showTree($child, $parent_array);
&nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp;  break; //There is no break here;
&nbsp; &nbsp; &nbsp;   ; &nbsp;  }
 &nbsp;  }
 &nbsp; 

Tree print
user name
2006-01-28 08:50:39
<? thaaaaaaaaaaaaaanx very much "Tanoor Dieng";
?>

[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )