评论递归无极显示

<?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);

  

转载于:https://www.cnblogs.com/adtuu/p/4688303.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值