無限分級和樹狀節點輸出

來源:互聯網
上載者:User
  • $strRe = '
  • ';
    傳入簡單的未經處理資料,可以得出節點間的N種關係,並能輸出樹狀的DOM
    注意:請參考git.oschina.net上的最新代碼(點"源碼出處"連結)
    1. /**
    2. * 輸出無限分類,我自己寫的哦~
    3. *
    4. * @author binny_w@qq.com
    5. * @since 2013-09-24 AM
    6. */
    7. /* 使用樣本 */
    8. /*
    9. $arrAll = array(
    10. array('id' => 1, 'name' => '欄目分類_1', 'name_en' => 'cat_1', 'parent_id' => 0),
    11. array('id' => 2, 'name' => '欄目分類_2', 'name_en' => 'cat_2', 'parent_id' => 0),
    12. array('id' => 3, 'name' => '欄目分類_3', 'name_en' => 'cat_3', 'parent_id' => 1),
    13. array('id' => 4, 'name' => '欄目分類_4', 'name_en' => 'cat_4', 'parent_id' => 1),
    14. array('id' => 5, 'name' => '欄目分類_5', 'name_en' => 'cat_5', 'parent_id' => 2),
    15. array('id' => 6, 'name' => '欄目分類_6', 'name_en' => 'cat_6', 'parent_id' => 4),
    16. array('id' => 7, 'name' => '欄目分類_7', 'name_en' => 'cat_7', 'parent_id' => 6),
    17. array('id' => 8, 'name' => '欄目分類_8', 'name_en' => 'cat_8', 'parent_id' => 7),
    18. array('id' => 9, 'name' => '欄目分類_9', 'name_en' => 'cat_9', 'parent_id' => 6)
    19. );
    20. $objT = new TreeList($arrAll);
    21. print_r($objT->arrAll);
    22. print_r($objT->arrIdAll);
    23. print_r($objT->arrIdChildren);
    24. print_r($objT->arrIdSon);
    25. print_r($objT->arrIdLeaf);
    26. print_r($objT->arrIdRelation);
    27. print_r($objT->arrIdRelationSimple);
    28. print_r($objT->arrIdRoot);
    29. print_r($objT->arrIdBackPath);
    30. print($objT->getTable());
    31. print($objT->getSelect('cat', array(1, 8), true));
    32. */
    33. // !defined('IN_FRAME') && die('404 Page');
    34. class TreeList {
    35. /**
    36. * 分析出所有可能用到的資料
    37. */
    38. public $arrAll = array(); // 未經處理資料
    39. public $arrIdRelation = array(); // 按_ID作鍵名的多維關係
    40. public $arrIdRelationSimple = array(); // 按_ID作鍵名的多維關係的簡化,用來輸出樹狀圖
    41. public $arrIdAll = array(); // 將未經處理資料轉化成的_ID作鍵名的數組
    42. public $arrIdSon = array(); // 所有的父子關係
    43. public $arrIdLeaf = array(); // 葉子節點的_ID
    44. public $arrIdRoot = array(); // 根節點的_ID
    45. public $arrIdChildren = array(); // 每個節點下的子孫後代_ID
    46. public $arrIdBackPath = array(); // 每個節點回逆到根
    47. public $strItem = '
      {$strSep}{$name}'; // 輸出樹的結構
    48. /**
    49. * 建構函式,傳入未經處理資料
    50. */
    51. public function __construct($arrData) {
    52. $this->arrAll = $arrData;
    53. $this->processData();
    54. }
    55. /**
    56. * 簡單的樹
    57. */
    58. public function getHtml() {
    59. return $this->genHtml();
    60. }
    61. /**
    62. * 用Table來畫樹
    63. */
    64. public function getTable() {
    65. $this->strItem = '
    {$strSep}{$name} {$name} {$name_en}
  • $strRe .= '
  • $strRe .= $this->genHtml();
  • '; ';
  • $strRe .= '
  • 結構 中文名 英文名
    ';
  • return $strRe;
  • }
  • /**
  • * 在下拉框中顯示
  • * example:
  • * $objTreeList->getSelect('parent_id', 0, false, 'class="span5"', array(0, '≡ 作為一級欄目 ≡')))
  • */
  • public function getSelect($strName = 'tree', $arrValue = array(), $blmMulti = false, $strExt = '', $arrFirst = null) {
  • !is_array($arrValue) && $arrValue = array($arrValue);
  • foreach ($this->arrIdAll as $strTemp => $arrTemp) {
  • $this->arrIdAll[$strTemp]['selected'] = '';
  • if (in_array($arrTemp['id'], $arrValue)) {
  • $this->arrIdAll[$strTemp]['selected'] = ' selected="selected"';
  • }
  • }
  • $this->strItem = '{$strSep}{$name}';
  • $strRe = ' $strRe .= ($blmMulti ? ' multiple="multiple"' : '') . (empty($strExt) ? '' : ' ' . $strExt) . '>'; if (is_array($arrFirst) && count($arrFirst) == 2) { $strRe .= '' . $arrFirst[1] . ''; } $strRe .= $this->getHtml() . '';
  • return $strRe;
  • }
  • /* ----- 以下的都是處理資料的私人函數,遞迴和迴圈之類,很複雜! ----- */
  • private function helpForGetRelation($arrData) {
  • $arrRe = array();
  • foreach ($arrData as $strTemp => $arrTemp) {
  • $arrRe[$strTemp] = $arrTemp;
  • if (isset($this->arrIdRelation[$strTemp])) {
  • $arrRe[$strTemp] = $this->arrIdRelation[$strTemp];
  • }
  • if (count($arrRe[$strTemp]) > 0) {
  • $arrRe[$strTemp] = $this->helpForGetRelation($arrRe[$strTemp]);
  • } else {
  • array_push($this->arrIdLeaf, $strTemp);
  • }
  • }
  • return $arrRe;
  • }
  • private function helpForGetChildren($arrData) {
  • $arrRe = array_keys($arrData);
  • foreach ($arrData as $arrTemp) {
  • $arrRe = array_merge($arrRe, $this->helpForGetChildren($arrTemp));
  • }
  • return $arrRe;
  • }
  • private function helpForGetBackPath($str) {
  • $arrRe = array();
  • $intTemp = $this->arrIdAll[$str]['parent_id'];
  • if ($intTemp > 0) {
  • $intTemp = '_' . $intTemp;
  • array_push($arrRe, $intTemp);
  • $arrRe = array_merge($arrRe, $this->helpForGetBackPath($intTemp));
  • }
  • return $arrRe;
  • }
  • private function processData() {
  • foreach ($this->arrAll as $arrTemp) {
  • $strTemp = '_' . $arrTemp['id'];
  • $this->arrIdAll[$strTemp] = $arrTemp;
  • if ($arrTemp['parent_id'] > 0) {
  • $strTemp_ = '_' . $arrTemp['parent_id'];
  • !isset($this->arrIdRelation[$strTemp_]) && $this->arrIdRelation[$strTemp_] = array();
  • $this->arrIdRelation[$strTemp_][$strTemp] = array();
  • !isset($this->arrIdSon[$strTemp_]) && $this->arrIdSon[$strTemp_] = array();
  • array_push($this->arrIdSon[$strTemp_], $strTemp);
  • } else {
  • !isset($this->arrIdRelation[$strTemp]) && $this->arrIdRelation[$strTemp] = array();
  • array_push($this->arrIdRoot, $strTemp);
  • }
  • }
  • $this->arrIdRelation = $this->helpForGetRelation($this->arrIdRelation);
  • $this->arrIdLeaf = array_unique($this->arrIdLeaf);
  • foreach ($this->arrIdRelation as $strTemp => $arrTemp) {
  • $this->arrIdChildren[$strTemp] = $this->helpForGetChildren($arrTemp);
  • in_array($strTemp, $this->arrIdRoot) && $this->arrIdRelationSimple[$strTemp] = $arrTemp;
  • }
  • $arrTemp = array_keys($this->arrIdAll);
  • foreach ($arrTemp as $strTemp) {
  • $this->arrIdBackPath[$strTemp] = $this->helpForGetBackPath($strTemp);
  • }
  • }
  • private function genSeparator($intLen) {
  • $strRe = '';
  • $i = 0;
  • while ($i < $intLen) {
  • $strRe .= ' ' . (($i + 1 == $intLen) ? '├' : '│');
  • $i ++;
  • }
  • !empty($strRe) && $strRe .= '─';
  • return $strRe;
  • }
  • private function genHtml($arrRelation = null, $intSep = 0) {
  • $strRe = '';
  • null === $arrRelation && $arrRelation = $this->arrIdRelationSimple;
  • foreach ($arrRelation as $strKey => $arrTemp) {
  • if (count($this->arrIdAll[$strKey]) > 0) {
  • $strSep = $this->genSeparator($intSep);
  • extract($this->arrIdAll[$strKey]);
  • eval('$strRe .= "' . $this->strItem . '";');
  • count($arrTemp) > 0 && $strRe .= $this->genHtml($arrTemp, ($intSep + 1));
  • }
  • }
  • return $strRe;
  • }
  • }複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.