栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > PHP面试题库

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

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

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";}?>

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/266487.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号