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 = "<?php\r\n"; $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 = "<?php\r\n"; $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); } } ?> [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">';"\r\n"; echo '<select name="sortlevel">'."\r\n"; echo '<option value="" selected="selected">根分类</option>'."\r\n"; 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>\r\n"; } echo "</select>\r\n"; echo ' <input name="sortname" type="text" id="sortname" />'."\r\n"; 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">'."\r\n"; echo '分类名:<input name="sortname" type="text" id="sortname" value="'.urldecode($_GET["name"]).'"/> '."\r\n"; echo '<input name="key" type="hidden" id="key" value="'.$_GET["key"].'" />'."\r\n"; echo '<input name="submit" type="submit" id="submit" value="提交" />'."\r\n"; echo '</form>'."\r\n"; echo ' <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>\r\n"; echo "<br>\r\n"; echo "<br>\r\n"; 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 "\r\n"; } ?>
无限分页类
最新推荐文章于 2022-06-01 17:38:29 发布