下面这个多级分类,我在测试时老是在缩进的地方出问题
经测试应该是得到类别深度时出现问题
比如类别为的10分类,它的深度实际是4
但在用如下代码运行时
打印出来的却是:44444
请问这个问题怎么解决
虚心求教注,因为之前学过一些JAVA,所以有可能受JAVA习惯影响代码如下<?php
/*
 *www.zzcms.com
 * 多级分类
 * id:类别ID号
 * name:类别名字
 * farent_id:父ID号
 */
require ("mysql.php");
class type {
private $id;
private $name;
private $farent_id;
private $level = 0;
public $db;
public $num_level;
function __construct() {
$this->db = new mysql();
$this->get_level(10);
} //用递归来列出目录列表
function class_list1($class_id) {
$sql = "select * from class where farent_id =" . $class_id;
$rs = $this->db->query($sql);
//循环输出类别名字
while ($row = $this->db->fetch_array($rs, 1)) {
echo $this->num_level;
$this->num_level = $this->get_level($row[0]);
echo $this->num_level;
for ($i = 0; $i < $this->num_level; $i++) {
echo "&nbsp;&nbsp;";
}
$this->num_level = 0;
if ($this->is_leaf($row[0])) {
printf("<img src='-.gif' />" . $row[1] . "<br />"); } else {
printf("<img src='+.gif' />" . $row[1] . "<br />");
}
$this->class_list($row[0]);
}
} function class_list($id) {
$sql = "select * from class where farent_id=" . $id;
$rs = $this->db->query($sql);
while ($row = $this->db->fetch_array($rs, 1)) {
$this->num_level = $this->get_level($row[0]);
for ($i = 0; $i < $this->num_level; $i++) {
echo "&nbsp;&nbsp;";
}
$this->num_level = 0;
if ($this->is_leaf($row[0])) {
printf("<img src='-.gif' />" . $row[1] . "<br />");
} else {
printf("<img src='+.gif' />" . $row[1] . "<br />");
$this->class_list($row[0]);
}
}
} //删除类别
function class_del($class_id) {
if ($this->is_leaf($class_id)) {
$sql = "delete from class where id=" . $class_id;
} else {
$sql = "delete from class where farent_id=" . $class_id;
}
} //判断类别是不是叶子节点
function is_leaf($class_id) {
$sql = "select * from class where farent_id=" . $class_id;
$rs = $this->db->query($sql);
if ($this->db->num_rows($rs) > 0) {
return false; //如果有子类,则不是叶子节点
} else {
return true; //如果没有子类,则有叶子节点
} } //得到类别的深度
function get_level($class_id) {
$sql = "select * from class where id=" . $class_id;
$rs = $this->db->query($sql);
while ($row = $this->db->fetch_array($rs, 1)) {
if ($row[2] != 0) {
$this->level++;
$this->get_level($row[2]);

}
}
echo $this->level;
//return $this->level;
//$this->level = 0;
}}$type = new type();
?>