简体   繁体   English

如何在php中显示树状视图?

[英]How to show tree view in php?

I am developing Tree View in php, how to connect my php array code in html UI designs. 我正在用php开发Tree View,如何在html UI设计中连接我的php数组代码。

My Php code is: 我的PHP代码是:

<?php
 //if order by parentid, id
$arr = array(
array('id'=>100, 'parentid'=>0, 'name'=>'first'),
array('id'=>101, 'parentid'=>100, 'name'=>'second'),
array('id'=>102, 'parentid'=>100, 'name'=>'third'),
array('id'=>103, 'parentid'=>100, 'name'=>'fourth'),
array('id'=>104, 'parentid'=>101, 'name'=>'five'),
array('id'=>105, 'parentid'=>102, 'name'=>'six'),
array('id'=>106, 'parentid'=>103, 'name'=>'seven'),
array('id'=>107, 'parentid'=>101, 'name'=>'eight'),
array('id'=>108, 'parentid'=>101, 'name'=>'nine'),
array('id'=>109, 'parentid'=>102, 'name'=>'ten'),
);
$arr_tree = array();
$arr_tmp = array();
foreach ($arr as $item) {
$parentid = $item['parentid'];
$id = $item['id'];

if ($parentid  == 0)
{
    $arr_tree[$id] = $item;
    $arr_tmp[$id] = &$arr_tree[$id];
}
else 
{
    if (!empty($arr_tmp[$parentid])) 
    {
        $arr_tmp[$parentid]['children'][$id] = $item;
        $arr_tmp[$id] = &$arr_tmp[$parentid]['children'][$id];
    }
   }
 }
unset($arr_tmp);
echo '<pre>'; print_r($arr_tree); echo "</pre>";
?>

and my html UI code 和我的HTML UI代码

<div class="tree">  
  <ul>
    <li>
      <a href="#"></a>
      <ul>
        <li><a href="#"></a>
            <ul>
              <li><a href="#"></a></li>
              <li><a href="#"></a></li>
              <li><a href="#"></a></li>
            </ul>
        </li>
        <li><a href="#"></a>
            <ul>
              <li><a href="#"></a></li>
              <li><a href="#"></a></li>
              <li><a href="#"></a></li>
            </ul>
        </li>
        <li><a href="#"></a>
            <ul>
              <li><a href="#"></a></li>
              <li><a href="#"></a></li>
              <li><a href="#"></a></li>
            </ul>
        </li>
      </ul>
    </li>       

  </ul>
 </div>

and style sheet code 和样式表代码

* {margin: 0; padding: 0;}
.tree ul {
 padding-top: 20px; position: relative;  
 transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
.tree li {
 float: left; text-align: center;
list-style-type: none;
position: relative;
padding: 20px 5px 0 5px;  
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
 }
 .tree li::before, .tree li::after{
 content: '';
 position: absolute; top: 0; right: 50%;
 border-top: 1px solid #ccc;
 width: 50%; height: 20px;
 }
 .tree li::after{
   right: auto; left: 50%;
  border-left: 1px solid #ccc;
 }
.tree li:only-child::after, .tree li:only-child::before {
 display: none;
 }
.tree li:only-child{ padding-top: 0;}
.tree li:first-child::before, .tree li:last-child::after{
 border: 0 none;
 }
.tree li:last-child::before{
 border-right: 1px solid #ccc;
 border-radius: 0 5px 0 0;
 -webkit-border-radius: 0 5px 0 0;
 -moz-border-radius: 0 5px 0 0;
 }
.tree li:first-child::after{
 border-radius: 5px 0 0 0;
-webkit-border-radius: 5px 0 0 0;
-moz-border-radius: 5px 0 0 0;
}
.tree ul ul::before{
 content: '';
 position: absolute; top: 0; left: 50%;
 border-left: 1px solid #ccc;
 width: 0; height: 20px;
 }
 .tree li a{
  border: 1px solid #ccc;

 text-decoration: none;
 color: #666;
 font-family: arial, verdana, tahoma;
 font-size: 11px;
 display: inline-block;
 background: url('people.png');
 -webkit-border-radius: 50%;
  -moz-border-radius: 5px;
 transition: all 0.5s;
 -webkit-transition: all 0.5s;
 -moz-transition: all 0.5s;
 width: 40px;
 height: 40px;
 }
 .tree li a:hover, .tree li a:hover+ul li a {
 background: #c8e4f8; color: #000; border: 1px solid #94a0b4;
 }
 .tree li a:hover+ul li::after, 
 .tree li a:hover+ul li::before, 
 .tree li a:hover+ul::before, 
 .tree li a:hover+ul ul::before{
 border-color:  #94a0b4;
 }

How to integrate my php code in html UI 如何在HTML UI中集成我的PHP代码

my desired result is like this image 我想要的结果就是这张图片

图片

We can use a "real" tree in php code and that makes the things much easier. 我们可以在php代码中使用“真正的”树,这使事情变得容易得多。

class Branch {
  public $branches = [];
  public $name;

  public function __construct($name) {
    $this->name = $name;
  }

  public function to_html() {
    $str = '<li><a href="#">' . $this->name . "</a>\n";
    if (!empty($this->branches)) {
      $str .= '<ul>';
      foreach ($this->branches as $branch) {
        $str .= $branch->to_html();
      }
      $str .= '</ul>';
    }
    $str .= "</li>\n";
    return $str;
  }
}

$tree = new Branch("first");
$tree->branches[] = new Branch("second");
$tree->branches[] = new Branch("third");
$tree->branches[] = new Branch("fourth");
$tree->branches[0]->branches[] = new Branch("five");
$tree->branches[0]->branches[] = new Branch("eight");
$tree->branches[0]->branches[] = new Branch("nine");
$tree->branches[1]->branches[] = new Branch("six");
$tree->branches[1]->branches[] = new Branch("ten");
$tree->branches[2]->branches[] = new Branch("seven");

$html = $tree->to_html();

The output html will be 输出的HTML将是

  • first 第一
    • second 第二
      • five
      • eight
      • nine
    • third 第三
      • six
      • ten
    • fourth 第四
      • seven
  • 由于php是html可嵌入的代码,因此您可以在html中的任何位置使用php,例如从头到尾再到正文等等。

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM