簡単なオリジナルデータを渡すだけで、N種類のノード間の関係を導き出し、ツリー状のDOMを出力できます 注: git.oschina.net の最新コードを参照してください ([ソース コード ソース] リンクをクリックします)。
- <?php
- /**
- * 出力無制限の分類、私は自分で書きました~
- *
- * @author binny_w@qq.com
- * @since 2013-09-24 AM
- */
- /*使用例*/
- /*
- $arrAll = array(
- array('id' => 1, 'name' => '列分類_1', '名前_en' => 'cat_1', 'parent_id' => 0),
- array('id' => 2, '名前' => '列分類_2', '名前_en' = > 'cat_2', 'parent_id' => 0),
- array('id' => 3, 'name' => '列分類_3', 'name_en' => ' cat_3', 'parent_id' => 1),
- array('id' => 4, 'name' => '列分類_4', 'name_en' => 'cat_4', 'parent_id' = > 1),
- array( 'id' => 5, 'name' => '列分類_5', 'name_en' => 'cat_5', 'parent_id' => 2),
- 配列 ('id' => 6, ' name' => '列分類_6', 'name_en' => 'cat_6', 'parent_id' => 4),
- array('id' => 7, 'name' => '列分類_7' , 'name_en' => 'cat_7', 'parent_id' => 6),
- array('id' => 8, 'name' => '列分類_8', 'name_en' => 'cat_8 ', 'parent_id' => 7),
- array('id' => 9, 'name' => '列分類_9' , 'name_en' => 'cat_9', 'parent_id' => 6 )
- );
- $objT = new TreeList($arrAll);
- print_r($objT->arrAll);
- print_r($objT->arrIdAll);
- print_r($objT->arrIdChildren);
- print_r ($objT->arrIdSon);
- print_r($objT->arrIdLeaf);
- print_r($objT->arrIdRelation);
- print_r($objT->arrIdRelationSimple);
- print_r($objT->gt; arrIdRoot);
- print_r($objT->arrIdBackPath);
- print($objT->getTable());
- print($objT ->getSelect('cat', array(1, 8), true) );
- */
- // !define('IN_FRAME') && die('404 Page');
- class TreeList {
-
- /* *
- * 考えられるすべてのデータを分析します
- */
- public $arrAll = array(); // オリジナルdata
- public $arrIdRelation = array(); // _ID をキー名とする多次元リレーションシップ
- public $arrIdRelationSimple = array(); // _ID をキー名とする多次元リレーションシップの簡略化、ツリー図の出力に使用されます
- public $arrIdAll = array(); // 元のデータをキー名として配列に変換します
- public $arrIdSon = array() // すべての親子関係
- public $arrIdLeaf = array();リーフノード
- public $arrIdRoot = array(); // ルートノードの _ID
- public $arrIdChildren = array(); // 各ノードの子孫 ID
- public $arrIdBackPath = array(); // 各ノードは、 root
- public $strItem = '<br />{$strSep}{$name}'; / / ツリーの構造を出力します
-
- /**
- *コンストラクター、元のデータを渡します
- */
- public function __construct($arrData) {
- $this->arrAll = $arrData;
- $this->processData();
- }
-
- / **
- * シンプルなツリー
- */
- public function getHtml() {
- return $this->genHtml() ;
- }
-
- /**
- * テーブルを使用して木を描画します
- */
- public function getTable() {
- $this-> strItem = '<tr><td>{$strSep}{$name}</td> <td align="center">{$name}</td><td align="center ">{$name_en}</td></tr>';
- $strRe = ' <table border="1" width="50%">';
- $strRe .= '< tr><th width="30%">構造</th><th width=" 20%>中国語名</th><th width="10%">英語名</th></tr>';
- $strRe .= $this->genHtml();
- $strRe .= '</table>';
- return $strRe;
- }
-
- /* *
- * ドロップダウンボックスに表示します
- * 例:
- * $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] ]['選択済み'] = ' 選択済み=選択済み"';
- }
- }
- $this->strItem = '<option value="{$id}"{$selected} title="{$name_en}">{$strSep}{$name}</option>' ;
- $strRe = '<select id="id_' . $strName . '" name="' . $strName . ($blmMulti ? '[]' : '') . '"';
- $strRe .= ($blmMulti ? ' multiple="multiple"' : '') . (empty($strExt) ? '' : ' . $strExt) '>';
- if (is_array($arrFirst) && count($) arrFirst) == 2) {
- $strRe .= '<option value="' . $arrFirst[0] . ''>' '</option>';
- }
- $strRe .= $this->getHtml() . '</select>';
- return $strRe;
- }
-
- /* ----- 以下は、データ、再帰、ループを処理するためのプライベート関数です。 、非常に複雑です ----- */
- プライベート関数 helpForGetRelation($arrData) {
- $arrRe = array();
- foreach ($arrData as $strTemp => $arrTemp) {
- $arrRe[$ strTemp] = $arrTemp;
- if (isset($this->gt;arrIdRelation[$strTemp])) {
- $arrRe[$strTemp] = $this->gt;arrIdRelation[$strTemp];
- }
- if (count( $arrRe [$strTemp]) > 0) {
- $arrRe[$strTemp] = $this->helpForGetRelation($arrRe[$strTemp]);
- } else {
- array_push($this->arrIdLeaf, $ strTemp) ;
- }
- }
- return $arrRe;
- }
-
- プライベート関数 helpForGetChildren($arrData) {
- $arrRe = array_keys($arrData);
- foreach ($arrData as $arrTemp) {
- $arrRe = array_merge( $arrRe , $this->helpForGetChildren($arrTemp));
- }
- return $arrRe;
- }
-
- プライベート関数 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;
- }
-
- プライベート関数 processData() {
- foreach ($this->arrAll as $arrTemp) {
- $strTemp = ' _' . $arrTemp['id'];
- $this->arrIdAll[$strTemp] = $arrTemp;
- if ($arrTemp['parent_id'] > 0) {
- $strTemp_ = '_' . '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->gt;helpForGetRelation($this->arrIdRelation);
- $this->arrIdLeaf = array_unique($this->arrIdLeaf) ;
- foreach ($this->arrIdRelation as $strTemp =>$arrTemp) {
- $this->gt;arrIdChildren[$strTemp] = $this->gt;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);
- }
- }
-
- プライベート関数 genSeparator($intLen) {
- $strRe = '';
- $i = 0;
- while ( $i < $intLen) {
- $strRe .= ' ' . (($i + 1 == $intLen) ? '§' : '│');
- $i ++;
- }
- !empty($ strRe) && $strRe .= '─';
- return $strRe;
- }
-
- プライベート関数 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 . '";'); ($arrTemp) > 0 && $strRe .= $this->genHtml($arrTemp, ($intSep + 1));
- }
- }
- return $strRe;
- }
- }
-
コードをコピー
|