zoukankan      html  css  js  c++  java
  • 评论递归无极显示

    <?php
    /*
    $a['son'] = array('name' =>'a','con'=>'b');
    $b[] = $a;
    var_dump($b);
    
    echo '<br />';
    
    $aa[]['son2'] = array('name' =>'a','con'=>'b');
    var_dump($aa);
    exit;
    */
    
    header('Content-type:text/html;charset=UTF-8');
    mysql_connect('localhost', 'root', '123');
    mysql_select_db('wordpress');
    mysql_query('set names utf8');
    
    $sql = 'select * from emlog_comment where gid = 1';
    $rs = mysql_query($sql);
    
    $categories = array();
    
    while ($row = mysql_fetch_array($rs)) {
         $categories[] = $row;
    }
    
    $tree = show_comment($categories, 0);
    var_dump($tree);
    echo procHtml($tree);
    
    function show_comment($categories, $pid) {
    
         $array = '';
    
         foreach($categories as $category) {
              if ($pid == $category['pid']) {
                    $category['son'] = show_comment($categories, $category['cid']);
                    $array[] = $category;
              }
         }
    
         return $array;
    }
    
    
    
    
    function procHtml($tree)
    {
    $html = '';
    foreach($tree as $t)
    {
       if($t['son'] == '')
       {
        $html .= "<li>{$t['comment']}</li>";
       }
       else
       {
        $html .= "<li>".$t['comment'];
        $html .= procHtml($t['son']);
        $html = $html."</li>";
       }
    }
    return $html ? '<ul>'.$html.'</ul>' : $html ;
    }
    
    
    
    function getTree($data, $pId)
    {
    $html = '';
    foreach($data as $k => $v)
    {
       if($v['cate_ParentId'] == $pId)
       {         //父亲找到儿子
        $html .= "<li>".$v['cate_Name'];
        $html .= getTree($data, $v['cate_Id']);
        $html = $html."</li>";
       }
    }
    return $html ? '<ul>'.$html.'</ul>' : $html ;
    }
    //echo getTree($data, 0);
    

      

  • 相关阅读:
    python函数对象
    生成器表达式,列表推导式
    python转换excel成py文件
    Python处理excel表
    Go基础:接口相关
    JAVA03-输入和输出
    python6-while循环
    python5-字典
    自动化8-xpath
    网络学习day1-计算机网络基础
  • 原文地址:https://www.cnblogs.com/adtuu/p/4688303.html
Copyright © 2011-2022 走看看