Last Update:2017-09-14
Source: Internet
Author: User
Keywords
Web Programming
PHP Tutorials
PHP Tutorials Unlimited class classification class trees{ Private $_keyid = ' Id '; Private $_keyname = ' Name '; Private $_keyfid = ' Fid '; function __construct ($keyId = ', $keyName = ', $keyFid = ') { if ($keyId = = "") {$this->_keyid = $this->_keyid;} else{$this->_keyid = $keyId;} if ($keyName = = "") {$this->_keyname = $this->_keyname;} else{$this->_keyname = $keyName;} if ($keyFid = = "") {$this->_keyfid = $this->_keyfid;} else{$this->_keyfid = $keyFid;} } Public Function Treelistall ($fid, $step =0,& $fromArray,& $resultArray) { $step + +; foreach ($fromArray as $k => $v) { if ($v [$this->_keyfid] = = $fid) { $newArray [] = $v; } } if (isset ($newArray)) { foreach ($newArray as $k => $v) { $this->treelistall ($v [$this->_keyid], $step, $fromArray, $resultArray); $v [' step '] = $step; $resultArray [] = $v; } } } Public Function Gettreelistall ($fid =0, $step =0,& $fromArray,& $resultArray) { $step + +; foreach ($fromArray as $k => $v) { if ($v [$this->_keyfid] = = $fid) { $newArray [] = $v; } } if (isset ($newArray)) { foreach ($newArray as $k => $v) { $this->gettreelistall ($v [$this->_keyid], $step, $fromArray, $resultArray); $v [' step '] = $step; $resultArray [] = $v; } } } Public Function gettreelist ($id = 0,& $fromArray) { $resultArray = Array (); $this->gettreelistall ($id, 0, $fromArray, $resultArray); $resultArray = Array_reverse ($resultArray); return $resultArray; } Public Function Gettreearray ($id = 0,& $fromArray) { $result _one_array = $this->gettreelist ($id, $fromArray); foreach ($result _one_array as $k => $v) { $result _two_array[] = Array ($v [$this->_keyid]); } if (isset ($result _two_array)) { for ($i =0 $i <count ($result _two_array), $i + +) { for ($j =0 $j <count ($result _two_array[$i]); $j + +) { $result [] = $result _two_array[$i] [$j]; } } }else{ $result = Array (); } return $result; }
Public Function Treelistalltop ($fid, $step =0,& $fromArray,& $resultArray) { $step + +; foreach ($fromArray as $k => $v) { if ($v [$this->_keyid] = = $fid) { $newArray [] = $v; } } if (isset ($newArray)) { foreach ($newArray as $k => $v) { $this->treelistalltop ($v [$this->_keyfid], $step, $fromArray, $resultArray); $v [' step '] = $step; $resultArray [] = $v; } } } Public Function Gettreelistalltop ($fid =0, $step =0,& $fromArray,& $resultArray) { $step + +; foreach ($fromArray as $k => $v) { if ($v [$this->_keyid] = = $fid) { $newArray [] = $v; } } if (isset ($newArray)) { foreach ($newArray as $k => $v) { $this->gettreelistalltop ($v [$this->_keyfid], $step, $fromArray, $resultArray); $v [' step '] = $step; $resultArray [] = $v; } } } Public Function gettreelisttop ($id = 0,& $fromArray) { $resultArray = Array (); $this->gettreelistalltop ($id, 0, $fromArray, $resultArray); $resultArray = Array_reverse ($resultArray); return $resultArray; } Public Function gettreearraytop ($id = 0,& $fromArray) { $result _one_array = $this->gettreelisttop ($id, $fromArray); foreach ($result _one_array as $k => $v) { $result _two_array[] = Array ($v [$this->_keyfid]); } if (isset ($result _two_array)) { for ($i =0 $i <count ($result _two_array), $i + +) { for ($j =0 $j <count ($result _two_array[$i]); $j + +) { $result [] = $result _two_array[$i] [$j]; } } }else{ $result = Array (); } return $result; } Public Function makeoptionstring ($sourcArray, $firstHint = "top-level category", $selectId =array ('-1 '), $type =0) { if ($type ==0) { if ($firstHint!= "") { $str = ' <option value= ' 0 ' > '. $firstHint. ' </option> '; }else{ $str = '; } foreach ($sourcArray as $value) { $level = ""; for ($i =1, $i < $value [' step ']; $i + +) { $level = $level. " ----|"; } $selectStr = ""; if (In_array ($value [$this->_keyid], $selectId)) { $selectStr = "selected"; }else{ } $str. = ' <option value= '. $value [$this->_keyid]. ' " '. $selectStr. ' >| '. $level. $value [$this->_keyname]. " </option> "; $level = ""; } }else{ $flagStep =-1; $str = ' <option value= 0 ' > ' $firstHint. ' </option> '; foreach ($sourcArray as $value) { $level = ""; for ($i =1, $i < $value [' step ']; $i + +) { $level = $level. " ----|"; } $selectStr = ""; if ($type = = $value [$this->_keyid]) { $flagStep = $value [' Step ']; }else{ if ($flagStep!=-1 && $value [' Step ']> $flagStep) { }else{ if ($flagStep!=-1 && $value [' Step ']<= $flagStep) { $flagStep =-1; } if ($value [$this->_keyid] = = $selectId) { $selectStr = "selected"; } $str. = ' <option value= '. $value [$this->_keyid]. ' " '. $selectStr. ' >| '. $level. $value [$this->_keyname]. " </option> "; } } $level = ""; } } return $str; } }