使用PHP数组实现无限分类,不使用数据库,不使用递归.

时间:2021-05-25

复制代码 代码如下:
<?php

classcat
{
public$data;

publicfunction__construct()
{
@include"data.php";
$this->data=$class;
}


publicfunctionCreateSortLevel($fatherlevel)
{
if(empty($fatherlevel))
{
if(is_array($this->data))
{
$fast_level=array();
foreach($this->dataas$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))
{
case1:
return"00{$sub}";
break;
case2:
return"0{$sub}";
break;
case3:
return$sub;
break;
}
}
else
{
return"001";
}
}

foreach($this->dataas$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))
{
case1:
returnsubstr($max_two_level,0,strlen($max_two_level)-1).$sub;
break;
case2:
returnsubstr($max_two_level,0,strlen($max_two_level)-2).$sub;
break;
case3:
returnsubstr($max_two_level,0,strlen($max_two_level)-3).$sub;
break;
}
}
else
{
return$fatherlevel."001";
}
}

publicfunctionorders()
{
$op=$this->data;
$this->array_usort($op,"sortlevel",SORT_ASC);
return$op;
}

publicfunctionadd_cat($sortname,$sortlevel)
{
$data=time();
$arr=array
(
"{$data}"=>array
(
"sortname"=>$sortname,
"sortlevel"=>$this->CreateSortLevel($sortlevel)
)
);
$rs=$this->data+$arr;
$this->add_wirte($rs);
}

privatefunctionarray_usort(&$array)
{
$args=func_get_args();
for($i=1,$cmd='',$size=count($args);$i<$size;$i++)
{
$num=$i;
$order="";
$con="@strcmp($a['$args[$num]'],$b['$args[$num]'])";
while(++$i<$size)
{
if($args[$i]===SORT_NUMERIC)
{
$con="($a['$args[$num]']-$b['$args[$num]'])";
}
elseif($args[$i]===SORT_DESC)
{
$order="-1*";
}
elseif(is_string($args[$i]))
{
$i--;
break;
}
}
$cmd.="if($num=$con)return$order$num;else";
if($order!='')
{
$i++;
}
}
@usort($array,@create_function('$a,$b',"$cmdreturn0;"));
}

publicfunctionwirte($sortname,$sortlevel)
{
$array="<?phprn";
$array.='$class=array'."rn(rn";
$array.='"'.time().'"=>array'."rn";
$array.='('."rn";
$array.='"sortname"=>"'.$sortname.'"'.",rn";
$array.='"sortlevel"=>"'.$this->CreateSortLevel($sortlevel).'"'."rn";
$array.=')'."rn";
$array.=")rn?>";
file_put_contents("data.php",$array);
}

publicfunctionadd_wirte($rs)
{
$array="<?phprn";
$array.='$class=array'."rn(rn";
foreach($rsas$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?>";
file_put_contents("data.php",$array);
}
}


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

if($_GET["action"]=="tpl")
{
echo'<formid="form1"name="form1"method="post"action="cat.php?action=add">';"rn";
echo'<selectname="sortlevel">'."rn";
echo'<optionvalue=""selected="selected">根分类</option>'."rn";
foreach($cat->orders()as$val)
{
echo'<optionvalue="'.$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;<inputname="sortname"type="text"id="sortname"/>'."rn";
echo'<inputtype="submit"name="Submit"value="提交"/>';
echo"</form>";
exit;
}

foreach($cat->orders()as$value)
{
$level=strlen(substr($value['sortlevel'],0,-3));
for($i=0;$i<$level;$i++)
{
echo"-";
}
echo$value["sortname"];
echo"<br>";
}
?>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章