Format the classification data from the database, such as: News --Sports News --Entertainment News Financial --Forex --Financial
- Class Tree
- {
- /** Raw Data */
- Public $original;
- /**id's Key Name */
- public $id;
- /** the key name of the parent ID */
- Public $parentId;
- id*/at initialization of/**
- protected $initId;
- The level of the/** node */
- protected $thisLevel = 0;
- /** Final Tree */
- Protected $tree = Array ();
- /**
- * Constructor function
- +------------------------------------------
- * @access Public
- +------------------------------------------
- * @param array $original raw data
- * @param the key name of string $id ID
- * @param string $parentId The key name of the parent ID
- +------------------------------------------
- * @return void
- */
- Public function __construct ($original = ", $id =", $parentId = ")
- {
- if ($original && $id && $parentId)
- {
- $this->initialize ($original, $id, $parentId);
- }
- }
- /**
- * Initialization
- +------------------------------------------
- * @access Public
- +------------------------------------------
- * @param array $original raw data
- * @param the key name of string $id ID
- * @param string $parentId The key name of the parent ID
- +------------------------------------------
- * @return void
- */
- Public Function Initialize ($original, $id, $parentId)
- {
- $this->original = $original;
- $this->id = $id;
- $this->parentid = $parentId;
- }
- /**
- * Get initial node
- +----------------------------------------------
- * @access protected
- +----------------------------------------------
- * @param int $parentId The level of the initial node
- +----------------------------------------------
- * @return Array $parentTree
- */
- protected function Getparenttree ($parentId)
- {
- $parentTree = Array ();
- foreach ($this->original as $key = $value)
- {
- if ($value [$this->parentid] = = $parentId)
- {
- Array_push ($parentTree, $value);
- }
- }
- return $parentTree;
- }
- /**
- * Get subtree
- +----------------------------------------------
- * @access protected
- +----------------------------------------------
- * @param int $id The ID of the node
- * @param string $levelTag indent marker
- +----------------------------------------------
- * @return void
- */
- protected function Getchildrentree ($id, $levelTag)
- {
- foreach ($this->original as $key = $value)
- {
- if ($id = = $value [$this->parentid])
- {
- if ($LEVELTAG)
- {
- $this->getlevel ($value [$this->parentid]);
- $value [' leveltag '] = Str_repeat ($levelTag, $this->thislevel);
- $this->thislevel = 0;
- }
- $this->tree[] = $value;
- $this->getchildrentree ($value [$this->id], $levelTag);
- }
- }
- }
- /**
- * Get the level of the node
- +-------------------------------------------------
- * @access protected
- +-------------------------------------------------
- * @param int $PARENTID The parent ID of the node
- +-------------------------------------------------
- * @return void
- */
- protected function Getlevel ($parentId)
- {
- foreach ($this->original as $key = $value)
- {
- if ($parentId = = $value [$this->id] && $parentId! = $this->initid)
- {
- $this->thislevel++;
- $this->getlevel ($value [$this->parentid]);
- }
- }
- }
- /**
- * Get the full tree
- +-------------------------------------------------
- * @access Public
- +-------------------------------------------------
- * @param int $level from what level to start getting
- * @param string $levelTag indent marker
- +-------------------------------------------------
- * @return Array $this->tree complete tree
- */
- Public Function Gettree ($parentId =0, $levelTag = ")
- {
- $this->initid = $parentId;
- $parentTree = $this->getparenttree ($parentId);
- foreach ($parentTree as $key = $value)
- {
- $this->tree[] = $value;
- $this->getchildrentree ($value [$this->id], $levelTag);
- }
- return $this->tree;
- }
- }
- $conf = Array (
- 1 = = Array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' 1 '),
- 2 = = Array (' id ' = ' 2 ', ' ParentID ' =>0, ' name ' = ' 2 '),
- 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' 1-1 '),
- 4 = = Array (' id ' = ' 4 ', ' ParentID ' =>1, ' name ' = ' 1-2 '),
- 5 = = Array (' id ' = ' 5 ', ' ParentID ' =>2, ' name ' = ' 2-1 '),
- 6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' 1-1-1 '),
- 7 = = Array (' id ' = ' 7 ', ' ParentID ' =>4, ' name ' = ' 1-2-1 '),
- 8 = = Array (' id ' = ' 8 ', ' ParentID ' =>5, ' name ' = ' 2-1-1 '),
- 9 = = Array (' id ' = ' 9 ', ' ParentID ' =>8, ' name ' = ' 2-1-1-1 ')
- );
- $tree = new Tree ($conf, ' id ', ' parentid ');
- $arr = $tree->gettree (0, ");
- foreach ($arr as $val)
- {
- if ($val [' Leveltag '])
- {
- echo $val [' Leveltag ']. ' | - ';
- }
- echo $val [' name ']. '
';
- }
- ?>
Copy Code
- Class Tree
- {
- /** Raw Data */
- Public $original;
- /**id's Key Name */
- public $id;
- /** the key name of the parent ID */
- Public $parentId;
- id*/at initialization of/**
- protected $initId;
- The level of the/** node */
- protected $thisLevel = 0;
- /** Final Tree */
- Protected $tree = Array ();
- /**
- * Constructor function
- +------------------------------------------
- * @access Public
- +------------------------------------------
- * @param array $original raw data
- * @param the key name of string $id ID
- * @param string $parentId The key name of the parent ID
- +------------------------------------------
- * @return void
- */
- Public function __construct ($original = ", $id =", $parentId = ")
- {
- if ($original && $id && $parentId)
- {
- $this->initialize ($original, $id, $parentId);
- }
- }
- /**
- * Initialization
- +------------------------------------------
- * @access Public
- +------------------------------------------
- * @param array $original raw data
- * @param the key name of string $id ID
- * @param string $parentId The key name of the parent ID
- +------------------------------------------
- * @return void
- */
- Public Function Initialize ($original, $id, $parentId)
- {
- $this->original = $original;
- $this->id = $id;
- $this->parentid = $parentId;
- }
- /**
- * Get initial node
- +----------------------------------------------
- * @access protected
- +----------------------------------------------
- * @param int $parentId The level of the initial node
- +----------------------------------------------
- * @return Array $parentTree
- */
- protected function Getparenttree ($parentId)
- {
- $parentTree = Array ();
- foreach ($this->original as $key = $value)
- {
- if ($value [$this->parentid] = = $parentId)
- {
- Array_push ($parentTree, $value);
- }
- }
- return $parentTree;
- }
- /**
- * Get subtree
- +----------------------------------------------
- * @access protected
- +----------------------------------------------
- * @param int $id The ID of the node
- * @param string $levelTag indent marker
- +----------------------------------------------
- * @return void
- */
- protected function Getchildrentree ($id, $levelTag)
- {
- foreach ($this->original as $key = $value)
- {
- if ($id = = $value [$this->parentid])
- {
- if ($LEVELTAG)
- {
- $this->getlevel ($value [$this->parentid]);
- $value [' leveltag '] = Str_repeat ($levelTag, $this->thislevel);
- $this->thislevel = 0;
- }
- $this->tree[] = $value;
- $this->getchildrentree ($value [$this->id], $levelTag);
- }
- }
- }
- /**
- * Get the level of the node
- +-------------------------------------------------
- * @access protected
- +-------------------------------------------------
- * @param int $PARENTID The parent ID of the node
- +-------------------------------------------------
- * @return void
- */
- protected function Getlevel ($parentId)
- {
- foreach ($this->original as $key = $value)
- {
- if ($parentId = = $value [$this->id] && $parentId! = $this->initid)
- {
- $this->thislevel++;
- $this->getlevel ($value [$this->parentid]);
- }
- }
- }
- /**
- * Get the full tree
- +-------------------------------------------------
- * @access Public
- +-------------------------------------------------
- * @param int $level from what level to start getting
- * @param string $levelTag indent marker
- +-------------------------------------------------
- * @return Array $this->tree complete tree
- */
- Public Function Gettree ($parentId =0, $levelTag = ")
- {
- $this->initid = $parentId;
- $parentTree = $this->getparenttree ($parentId);
- foreach ($parentTree as $key = $value)
- {
- $this->tree[] = $value;
- $this->getchildrentree ($value [$this->id], $levelTag);
- }
- return $this->tree;
- }
- }
- $conf = Array (
- 1 = = Array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' 1 '),
- 2 = = Array (' id ' = ' 2 ', ' ParentID ' =>0, ' name ' = ' 2 '),
- 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' 1-1 '),
- 4 = = Array (' id ' = ' 4 ', ' ParentID ' =>1, ' name ' = ' 1-2 '),
- 5 = = Array (' id ' = ' 5 ', ' ParentID ' =>2, ' name ' = ' 2-1 '),
- 6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' 1-1-1 '),
- 7 = = Array (' id ' = ' 7 ', ' ParentID ' =>4, ' name ' = ' 1-2-1 '),
- 8 = = Array (' id ' = ' 8 ', ' ParentID ' =>5, ' name ' = ' 2-1-1 '),
- 9 = = Array (' id ' = ' 9 ', ' ParentID ' =>8, ' name ' = ' 2-1-1-1 ')
- );
- $tree = new Tree ($conf, ' id ', ' parentid ');
- $arr = $tree->gettree (0, ");
- foreach ($arr as $val)
- {
- if ($val [' Leveltag '])
- {
- echo $val [' Leveltag ']. ' | - ';
- }
- echo $val [' name ']. '
';
- }
- ?>
Copy Code |