Heim Backend-Entwicklung PHP-Tutorial 无限分级和树状节点输出

无限分级和树状节点输出

Jul 25, 2016 am 08:49 AM

';
  • $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 = '';
  • $strRe = '';
  • 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 $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;
  • }
  • }
  • 复制代码


    Erklärung dieser Website
    Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

    Heiße KI -Werkzeuge

    Undresser.AI Undress

    Undresser.AI Undress

    KI-gestützte App zum Erstellen realistischer Aktfotos

    AI Clothes Remover

    AI Clothes Remover

    Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

    Undress AI Tool

    Undress AI Tool

    Ausziehbilder kostenlos

    Clothoff.io

    Clothoff.io

    KI-Kleiderentferner

    AI Hentai Generator

    AI Hentai Generator

    Erstellen Sie kostenlos Ai Hentai.

    Heißer Artikel

    R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
    2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
    Repo: Wie man Teamkollegen wiederbelebt
    4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Abenteuer: Wie man riesige Samen bekommt
    4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

    Heiße Werkzeuge

    Notepad++7.3.1

    Notepad++7.3.1

    Einfach zu bedienender und kostenloser Code-Editor

    SublimeText3 chinesische Version

    SublimeText3 chinesische Version

    Chinesische Version, sehr einfach zu bedienen

    Senden Sie Studio 13.0.1

    Senden Sie Studio 13.0.1

    Leistungsstarke integrierte PHP-Entwicklungsumgebung

    Dreamweaver CS6

    Dreamweaver CS6

    Visuelle Webentwicklungstools

    SublimeText3 Mac-Version

    SublimeText3 Mac-Version

    Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

    11 beste PHP -URL -Shortener -Skripte (kostenlos und Premium) 11 beste PHP -URL -Shortener -Skripte (kostenlos und Premium) Mar 03, 2025 am 10:49 AM

    Lange URLs, die oft mit Schlüsselwörtern und Tracking -Parametern überfüllt sind, können Besucher abschrecken. Ein URL -Verkürzungsskript bietet eine Lösung, die präzise Links erstellt, die ideal für soziale Medien und andere Plattformen sind. Diese Skripte sind für einzelne Websites a wertvoll

    Einführung in die Instagram -API Einführung in die Instagram -API Mar 02, 2025 am 09:32 AM

    Nach seiner hochkarätigen Akquisition durch Facebook im Jahr 2012 nahm Instagram zwei APIs für den Einsatz von Drittanbietern ein. Dies sind die Instagram -Graph -API und die Instagram Basic Display -API. Ein Entwickler, der eine App erstellt, die Informationen von a benötigt

    Arbeiten mit Flash -Sitzungsdaten in Laravel Arbeiten mit Flash -Sitzungsdaten in Laravel Mar 12, 2025 pm 05:08 PM

    Laravel vereinfacht die Behandlung von temporären Sitzungsdaten mithilfe seiner intuitiven Flash -Methoden. Dies ist perfekt zum Anzeigen von kurzen Nachrichten, Warnungen oder Benachrichtigungen in Ihrer Anwendung. Die Daten bestehen nur für die nachfolgende Anfrage standardmäßig: $ Anfrage-

    Erstellen Sie eine React -App mit einem Laravel -Back -Ende: Teil 2, reagieren Erstellen Sie eine React -App mit einem Laravel -Back -Ende: Teil 2, reagieren Mar 04, 2025 am 09:33 AM

    Dies ist der zweite und letzte Teil der Serie zum Aufbau einer Reaktionsanwendung mit einem Laravel-Back-End. Im ersten Teil der Serie haben wir eine erholsame API erstellt, die Laravel für eine grundlegende Produktlistenanwendung unter Verwendung von Laravel erstellt hat. In diesem Tutorial werden wir Dev sein

    Vereinfachte HTTP -Reaktion verspottet in Laravel -Tests Vereinfachte HTTP -Reaktion verspottet in Laravel -Tests Mar 12, 2025 pm 05:09 PM

    Laravel bietet eine kurze HTTP -Antwortsimulationssyntax und vereinfache HTTP -Interaktionstests. Dieser Ansatz reduziert die Code -Redundanz erheblich, während Ihre Testsimulation intuitiver wird. Die grundlegende Implementierung bietet eine Vielzahl von Verknüpfungen zum Antworttyp: Verwenden Sie Illuminate \ Support \ facades \ http; Http :: fake ([ 'Google.com' => 'Hallo Welt',, 'github.com' => ['foo' => 'bar'], 'Forge.laravel.com' =>

    Curl in PHP: So verwenden Sie die PHP -Curl -Erweiterung in REST -APIs Curl in PHP: So verwenden Sie die PHP -Curl -Erweiterung in REST -APIs Mar 14, 2025 am 11:42 AM

    Die PHP Client -URL -Erweiterung (CURL) ist ein leistungsstarkes Tool für Entwickler, das eine nahtlose Interaktion mit Remote -Servern und REST -APIs ermöglicht. Durch die Nutzung von Libcurl, einer angesehenen Bibliothek mit Multi-Protokoll-Dateien, erleichtert PHP Curl effiziente Execu

    12 Beste PHP -Chat -Skripte auf Codecanyon 12 Beste PHP -Chat -Skripte auf Codecanyon Mar 13, 2025 pm 12:08 PM

    Möchten Sie den dringlichsten Problemen Ihrer Kunden in Echtzeit und Sofortlösungen anbieten? Mit Live-Chat können Sie Echtzeitgespräche mit Kunden führen und ihre Probleme sofort lösen. Sie ermöglichen es Ihnen, Ihrem Brauch einen schnelleren Service zu bieten

    Ankündigung von 2025 PHP Situation Survey Ankündigung von 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

    Die 2025 PHP Landscape Survey untersucht die aktuellen PHP -Entwicklungstrends. Es untersucht Framework -Nutzung, Bereitstellungsmethoden und Herausforderungen, die darauf abzielen, Entwicklern und Unternehmen Einblicke zu geben. Die Umfrage erwartet das Wachstum der modernen PHP -Versio

    See all articles