# 文件作用: 无限分类
#
# 文 件 名: cat.inc.php
#
# 版 权: 七夜 MSN:lijinxing20@Hotmail.com
#
# Email: lijinxing@Gmail.com
class cat
{
public $data; //无限分类数组
//======================================
// 功能: 构造函数.初始化数据
//======================================
public function __construct()
{
@require "data.php";
$this->data = $class;
}
//======================================
// 功能: 根据父等级构造出子等级
//======================================
private function CreateSortLevel($fatherlevel)
{
if(empty($fatherlevel))
{
if(is_array($this->data))
{
$fast_level = array();
foreach($this->data as $value)
{
if(strlen($value["sortlevel"]) == 3)
{
$fast_level[] = $value["sortlevel"];
}
}
$max_fast_level = max($fast_level);
unset($fast_level);
$sub = ceil($max_fast_level) + 1;
switch(strlen($sub))
{
case 1:
return "00{$sub}";
break;
case 2:
return "0{$sub}";
break;
case 3:
return $sub;
break;
}
}
else
{
return "001";
}
}
foreach($this->data as $val)
{
if(eregi("^".$fatherlevel.".{3}$",$val["sortlevel"]))
{
$level[] = $val["sortlevel"];
}
}
if(is_array($level))
{
$max_two_level = max($level);
$sub = ceil(substr($max_two_level,-3)) + 1;
switch(strlen($sub))
{
case 1:
return substr($max_two_level,0,strlen($max_two_level)-1).$sub;
break;
case 2:
return substr($max_two_level,0,strlen($max_two_level)-2).$sub;
break;
case 3:
return substr($max_two_level,0,strlen($max_two_level)-3).$sub;
break;
}
}
else
{
return $fatherlevel."001";
}
}
//======================================
// 功能: 添加一个新分类
//======================================
public function add_cat($sortname,$sortlevel)
{
$data = time().$this->random(5); //生成UNIX时间戳加5位的随机数作为健
$arr = array
(
"{$data}" => array
(
"sortname" => $sortname,
"sortlevel" => $this->CreateSortLevel($sortlevel)
)
);
$rs = $this->data + $arr; //把新的二维数组加入到原有的数组后面
$this->add_wirte($rs); //把合并后的新数组写到PHP文件里
unset($rs);
}
//============================================
// 功能: 当data.php不存在时,
// 生成无限分类的数组格式并写入PHP文件
//============================================
public function wirte($sortname,$sortlevel)
{
$array = " $array .= '$class = array'."\r\n(\r\n";
$array .= ' "'.time().$this->random(5).'" => array'."\r\n";
$array .= ' ('."\r\n";
$array .= ' "sortname" => "'.$sortname.'"'.",\r\n";
$array .= ' "sortlevel" => "'.$this->CreateSortLevel($sortlevel).'"'."\r\n";
$array .= ' )'."\r\n";
$array .= ")\r\n?>";
file_put_contents("data.php",$array);
}
//============================================
// 功能: 当data.php存在时,对数组排序
// 循环生成无限分类的数组格式并写入PHP文件
//============================================
public function add_wirte($rs)
{
$order = array();
foreach($rs as $val)
{
$order[] = $val["sortlevel"];
}
array_multisort($order,SORT_ASC,SORT_STRING,$rs); //对数组进行排序
$array = " $array .= '$class = array'."\r\n(\r\n";
foreach($rs as $key=>$value)
{
$array .= "\r\n";
$array .= ' "'.$key.'" => array'."\r\n";
$array .= ' ('."\r\n";
$array .= ' "sortname" => "'.$value["sortname"].'"'.",\r\n";
$array .= ' "sortlevel" => "'.$value["sortlevel"].'"'."\r\n";
$array .= ' ),';
}
$array = substr($array,0,-1);
$array .= "\r\n)\r\n?>";
//生成数组字符串写入data.php
file_put_contents("data.php",$array);
}
//============================================
// 功能:删除一个分类,并把他的下面的子分类
// 全部删除
//============================================
public function delete($id)
{
$op = $this->data;
foreach($op as $key=>$value)
{
if(eregi("^".$id,$value["sortlevel"]))
{
unset($op[$key]);
}
}
$this->add_wirte($op);
unset($op);
}
//======================================
// 功能: 生成随机数
//======================================
private function random($length)
{
$result = "";
$string = "zxcvbnmasdfghjklpoiuytrewq";
for ($i = 0;$i < $length;$i++)
{
$result .= $string[mt_rand(0,strlen($string) - 1)];
}
return $result;
}
//======================================
// 功能: 析构函数 注销$this->data对象
//======================================
public function __destruct()
{
unset($this->data);
}
}
?>
$cat = new cat();
if($_GET["action"] == "add")
{
if(strlen($_POST["sortname"]) < 2)
{
echo '';
echo '';
exit;
}
if(file_exists("data.php"))
{
$cat->add_cat(str_replace('"',"",htmlspecialchars(stripslashes(trim($_POST["sortname"])))),$_POST["sortlevel"]);
echo '';
echo '';
exit;
}
else
{
$cat->wirte(str_replace('"',"",htmlspecialchars(stripslashes(trim($_POST["sortname"])))),$_POST["sortlevel"]);
echo '';
echo '';
exit;
}
}
if($_GET["action"] == "tpl")
{
echo '
";
exit;
}
if($_GET["action"] == "delete")
{
$cat->delete(trim($_GET["id"]));
echo '';
echo '';
exit;
}
if($_GET["action"] == "module")
{
echo '
'."\r\n";
echo '
删除该分类';
exit;
}
if($_GET["action"] == "update")
{
$op = $cat->data;
$op[$_POST["key"]]["sortname"] = str_replace('"',"",htmlspecialchars(stripslashes(trim($_POST["sortname"]))));
$cat->add_wirte($op);
unset($op);
echo '';
echo '';
exit;
}
echo '
添加新分类';
echo "
\r\n";
echo "
\r\n";
echo "
\r\n";
foreach($cat->data as $key=>$value)
{
$level = strlen(substr($value['sortlevel'],0,-3));
for($i = 0;$i < $level;$i++)
{
echo "-";
}
echo '
'.$value["sortname"].'';
echo '
';
echo "\r\n";
}
?>