//从网上看到如下一段完整的无限分类代码。现在请问:如何给所有子类前加上空格或├符号,让所有分类都能分层显示呢?如树形结构那样。<?php
class category
{
private $pdo;
private $arr;
function __construct()
{
try
{
$this->pdo = new PDO('mysql:host=localhost;dbname=zhenyuoop','root','diannao');
  $this->pdo->exec("set names 'gbk'");
}
catch (PDOException $e)
{
echo '数据库连接失败:'.$e->getMessage();
  exit();
}
$this->arr();
}

function arr()//将全部分类信息放到一个二维数组中。
{
$result = $this->pdo->query("select * from zy_category");
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
$arr[] = array($row['catid'],$row['parentid'],$row['catname']);
}
$this->arr = $arr;
}
/*
结果代码获取了一个二维数组$arr。 下面,用递归函数来将这个数组中的所有数据按分类显示出来。
*/ function fenlei($parentid)//将父id传入
{
for($i = 0; $i < count($arr); $i++)//二维数组中有几个元素,就循环几次。
     {
         if($arr[$i][1] == $parentid)
         {
             echo $arr[$i][2]."<br>"; 
             $this->fenlei($arr[$i][0]);
             //将元素中的分类id做为实参,传给形参$parentid。这样递归调用,就可以输出各自的子类了。
         }
     }
}
}$category = new category();
$category->fenlei(0);
?>

解决方案 »

  1.   

    呵呵,估计是大家觉得分太少了。递归的地方小改了一下,你测试一下看看对不对。
    class category
    {
    private $pdo;
    private $arr;
    function __construct()
    {
    try
    {
    $this->pdo = new PDO('mysql:host=localhost;dbname=zhenyuoop','root','diannao');
       $this->pdo->exec("set names 'gbk'");
    }
    catch (PDOException $e)
    {
    echo '数据库连接失败:'.$e->getMessage();
       exit();
    }
    $this->arr();
    }function arr()//将全部分类信息放到一个二维数组中。
    {
    $result = $this->pdo->query("select * from zy_category");
    while($row = $result->fetch(PDO::FETCH_ASSOC))
    {
    $arr[] = array($row['catid'],$row['parentid'],$row['catname']);
    }
    $this->arr = $arr;
    }
    /*
    结果代码获取了一个二维数组$arr。 下面,用递归函数来将这个数组中的所有数据按分类显示出来。
    */function fenlei($parentid,$tab='')//将父id传入
    {
    for($i = 0; $i < count($arr); $i++)//二维数组中有几个元素,就循环几次。
      {
      if($arr[$i][1] == $parentid)
      {
      echo $tab.$arr[$i][2]."<br>";  
      $this->fenlei($arr[$i][0],$tag.' ');
      //将元素中的分类id做为实参,传给形参$parentid。这样递归调用,就可以输出各自的子类了。
      }
      }
    }
    }$category = new category();
    $category->fenlei(0);
      

  2.   


    <?php   
    class Tree   
    {   
    public $data=array();   
    public $cateArray=array();   
    //public $ids=array();   
       
    function Tree()   
    {   
       
    }    
    function setNode ($id, $parent, $value)   
    {   
    $parent = $parent?$parent:0;   
    $this->data[$id] = $value;   
    $this->cateArray[$id] = $parent;   
     // $this->ids[$id] = $id;   
    }    
    function getChildsTree($id=0)   
    {   
    $childs=array();   
    foreach ($this->cateArray as $child=>$parent)   
    {   
    if ($parent==$id)   
    {   
    $childs[$child]=$this->getChildsTree($child);   
    }   
       
    }   
    return $childs;   
    }   
    function getChilds($id=0)   
    {   
    $childArray=array();   
    $childs=$this->getChild($id);    
    foreach ($childs as $child)   
    {   
    $childArray[]=$child;   
    $childArray=array_merge($childArray,$this->getChilds($child));   
    }   
    return $childArray;   
    }   
    function getChild($id)   
    {   
    $childs=array();   //print_r( $this->cateArray );foreach ($this->cateArray as $child=>$parent)   
    {   
    if ($parent==$id)   
    {   
    $childs[$child]=$child;   
    }   
    }   //print_r( $childs );return $childs;   
    }   //单线获取父节点   
    function getNodeLever($id)   
    {   
    $parents=array();   
    if (key_exists($this->cateArray[$id],$this->cateArray))   
    {   
    $parents[]=$this->cateArray[$id];   
    $parents=array_merge($parents,$this->getNodeLever($this->cateArray[$id]));   
    }   
    return $parents;   
    }   
    function getLayer($id,$preStr='━')   
    {   
    return '┣'.str_repeat($preStr,count($this->getNodeLever($id)));   
    }   
    function getValue ($id)   
    {   
    return $this->data[$id];   
    } // end func  function getParent($id)   
    {   
    return $this->cateArray[$id];   
    } // end func  
    function showCategory(){
      // $category = $this->ids;     $category =$this->getChilds();  foreach ($category as $key=>$id)   
      {   
      //echo $id.$Tree->getLayer($id, '-').$Tree->getValue($id)."\n";  
    $data[$key]["id"]=$id;
    $data[$key]["layer"]=$this->getLayer($id, '━');
    $data[$key]["catname"]=$this->getValue($id);
    $data[$key]["pid"]=$this->getParent($id);
    $data[$key]["level"]=count($this->getNodeLever($id));
      
      }  return $data;}
        
    }   
         $Tree = new Tree();   
    //setNode(目录ID,上级ID,目录名字);   
    $Tree->setNode(1, 0, '目录1');   
    $Tree->setNode(2, 1, '目录2');   
    $Tree->setNode(5, 3, '目录5');   
    $Tree->setNode(3, 0, '目录3');   
    $Tree->setNode(4, 2, '目录4');   
    $Tree->setNode(9, 4, '目录9');   
    $Tree->setNode(6, 2, '目录6');   
    $Tree->setNode(7, 2, '目录7');   
    $Tree->setNode(8, 3, '目录8');  
    $Tree->setNode(10, 9, '目录10');   
       
    //print_r($Tree->getChildsTree(0));   
    //print_r($Tree->getChild(0));   
    //print_r($Tree->getLayer(2));   
       
    $category = $Tree->showCategory();   print_r($category);   
       
    遍历输出  
     
    foreach ($category as $key=>$value)   
    {   
    echo $value["id"].$value["layer"]."分类名:".$value["catname"]."父ID".$value["pid"]."级别".$value["level"]."\n";   
    }  
    */   ?>