用php数组实现无限分类,效率高,不使用数据库,不使用递归

匿名网友 匿名网友 发布于: 2015-08-30 00:00:00
阅读 170 收藏 0 点赞 0 评论 0

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 = "<?phprn";
 $array .= '$class = array'."rn(rn";
 $array .= ' "'.time().$this->random(5).'" => array'."rn";
 $array .= ' ('."rn";
 $array .= ' "sortname" => "'.$sortname.'"'.",rn";
 $array .= ' "sortlevel" => "'.$this->CreateSortLevel($sortlevel).'"'."rn";
 $array .= ' )'."rn";
 $array .= ")rn?>";
 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 = "<?phprn";
 $array .= '$class = array'."rn(rn";
 foreach($rs as $key=>$value)
 {
 $array .= "rn";
 $array .= ' "'.$key.'" => array'."rn";
 $array .= ' ('."rn";
 $array .= ' "sortname" => "'.$value["sortname"].'"'.",rn";
 $array .= ' "sortlevel" => "'.$value["sortlevel"].'"'."rn";
 $array .= ' ),';
 }
 $array = substr($array,0,-1);
 $array .= "rn)rn?>";
 //生成数组字符串写入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);
 }
}
?>






[Copy to clipboard] [ - ]
CODE:

<?php
$cat = new cat();
if($_GET["action"] == "add")
{
 if(strlen($_POST["sortname"]) < 2)
 {
 echo '<script language="JavaScript">alert("请添加分类名字")</script>';
 echo '<script language="JavaScript">location.href="/cat.php?action=tpl"</script>';
 exit;
 }
 if(file_exists("data.php"))
 {
 $cat->add_cat(str_replace('"',"",htmlspecialchars(stripslashes(trim($_POST["sortname"])))),$_POST["sortlevel"]);
 echo '<script language="JavaScript">alert("添加成功")</script>';
 echo '<script language="JavaScript">location.href="/cat.php"</script>';
 exit;
 }
 else
 {
 $cat->wirte(str_replace('"',"",htmlspecialchars(stripslashes(trim($_POST["sortname"])))),$_POST["sortlevel"]);
 echo '<script language="JavaScript">alert("添加成功")</script>';
 echo '<script language="JavaScript">location.href="/cat.php"</script>';
 exit;
 }
}

if($_GET["action"] == "tpl")
{
 echo '<form id="form1" name="form1" method="post" action="cat.php?action=add">';"rn";
 echo '<select name="sortlevel">'."rn";
 echo '<option value="" selected="selected">根分类</option>'."rn";
 foreach($cat->data as $val)
 {
 echo '<option value="'.$val["sortlevel"].'">';
 $clevel = strlen(substr($val['sortlevel'],0,-3));
 for($i = 0; $i < $clevel; $i++)
 {
 echo "-";
 }
echo $val['sortname']."</option>rn";
 }
 echo "</select>rn";
 echo '&nbsp;&nbsp;<input name="sortname" type="text" id="sortname" />'."rn";
 echo '<input type="submit" name="Submit" value="提交" />';
echo "</form>";
 exit;
}

if($_GET["action"] == "delete")
{
 $cat->delete(trim($_GET["id"]));
 echo '<script language="JavaScript">alert("删除成功")</script>';
 echo '<script language="JavaScript">location.href="/cat.php"</script>';
 exit;
}

if($_GET["action"] == "module")
{
 echo '<form id="update" name="update" method="post" action="cat.php?action=update">'."rn";
 echo '分类名:<input name="sortname" type="text" id="sortname" value="'.urldecode($_GET["name"]).'"/>&nbsp;&nbsp;'."rn";
 echo '<input name="key" type="hidden" id="key" value="'.$_GET["key"].'" />'."rn";
 echo '<input name="submit" type="submit" id="submit" value="提交" />'."rn";
 echo '</form>'."rn";
 echo '&nbsp;&nbsp;&nbsp;<a href="cat.php?action=delete&id='.$_GET["level"].'" title="会把该分类下的子分类全部删除">删除该分类</a>';
 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 '<script language="JavaScript">alert("修改成功")</script>';
 echo '<script language="JavaScript">location.href="/cat.php"</script>';
 exit;
}

echo '<a href="cat.php?action=tpl">添加新分类</a>';
echo "<br>rn";
echo "<br>rn";
echo "<br>rn";
foreach($cat->data as $key=>$value)
{
 $level = strlen(substr($value['sortlevel'],0,-3));
 for($i = 0;$i < $level;$i++)
 {
 echo "-";
 }
echo '<a href="cat.php?action=module&level='.$value["sortlevel"].'&name='.urlencode($value["sortname"]).'&key='.$key.'">'.$value["sortname"].'</a>';
 echo '<br>';
 echo "rn";
}
?>

评论列表
文章目录