评论递归无极显示

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

  

posted @ 2015-07-30 09:52  Adtuu  阅读(339)  评论(0编辑  收藏  举报