使用PHP数组实现的无限分类(不使用数据库与用递归),有需要的朋友可以参考下。
<?php
class cat
{
public $data;
public function __construct()
{
@include "data.php";
$this->data = $class;
}
public 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 orders()
{
$op = $this->data;
$this->array_usort($op,"sortlevel",SORT_ASC);
return $op;
}
public function add_cat($sortname,$sortlevel)
{
$data = time();
$arr = array
(
"{$data}" => array
(
"sortname" => $sortname,
"sortlevel" => $this->CreateSortLevel($sortlevel)
)
);
$rs = $this->data + $arr;
$this->add_wirte($rs);
}
private function array_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]'])";
}
else if($args[$i] === SORT_DESC)
{
$order = "-1*";
}
else if(is_string($args[$i]))
{
$i--;
break;
}
}
$cmd .= "if($num = $con)return $order$num;else ";
if($order != '')
{
$i++;
}
}
@usort($array,@create_function('$a,$b',"$cmd return 0;"));
}
public function wirte($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);
}
public function add_wirte($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?>";
file_put_contents("data.php",$array);
}
}
$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/action/tpl.html"</script>';
exit;
}
if(file_exists("data.php"))
{
$cat->add_cat($_POST["sortname"],$_POST["sortlevel"]);
echo '<script language="JavaScript">alert("添加成功")</script>';
echo '<script language="JavaScript">location.href="/cat.html"</script>';
exit;
}
else
{
$cat->wirte($_POST["sortname"],$_POST["sortlevel"]);
echo '<script language="JavaScript">alert("添加成功")</script>';
echo '<script language="JavaScript">location.href="/cat.html"</script>';
exit;
exit;
}
}
if($_GET["action"] == "tpl")
{
echo '<form id="form1" name="form1" method="post" action="/blog_article/cat/action/add.html">';"rn";
echo '<select name="sortlevel">'."rn";
echo '<option value="" selected="selected">根分类</option>'."rn";
foreach($cat->orders() 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 ' <input name="sortname" type="text" id="sortname" />'."rn";
echo '<input type="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>";
}
?>
您可能感兴趣的文章:
php将地区分类排序的算法
php写的一个递归实现无限分类生成下拉列表的函数
php与mysql实现的无限级分类
php把无限级分类生成数组的类
php用递归方法实现无限级分类的代码
php实现的无极分类(递归)的代码
函数的原理很简单,主要就是用了一下递归调用。
下面来看代码。
function file_list($path){
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($path."/".$file)) {
echo $path.": ".$file."<br>";//去掉此行显示的是所有的非目录文件
file_list($path."/".$file);
} else {
echo $path.": ".$file."<br>";
}
}
}
}
}
?>
此函数还可以做进一步改进,加入一些文件夹或文件的图标什么的,这样就可以做成更强大的一个函数。
php写的自定义函数,递归实现无限分类生成下拉列表,不用每次都从数据库读取数据,提高了效率。
有需要的朋友,建议参考下。
/*—————————————————— */
//– 递归实现无限分类生成下拉列表函数
//– $tpl->assign('sort_list',createSortOptions ());
//– $tpl->assign('sort_list',createSortOptions ($sort_id));
/*—————————————————— */
function createSortOptions ($selected=0,$parent_id=0,$n=-1)
{
global $db;
$sql = "SELECT * FROM `@__article_sort` WHERE `parent_id` = '{$parent_id}'";
$options = ";
static $i = 0;
if ($i == 0)
{
$options .= '<option value="0″ >请选择</option>';
}
$res = $db->query ($sql);
if ($res)
{
$n++;
while ($row = $db->fetch_assoc ($res))
{
$i++;
$options .="<option value='{$row['sort_id']}'";
if ($row['sort_id'] == $selected)
{
$options .=' selected ';
}
$options .=">".str_repeat(' ',$n*3).$row['sort_name']."</option>\n";
$options .=createSortOptions ($selected,$row['sort_id'],$n);
}
}
return $options;
}
?>
您可能感兴趣的文章:
php将地区分类排序的算法
使用php数组实现的无限分类(不使用数据库与用递归)
php与mysql实现的无限级分类
对ecshop中的无限级分类的分析
php用递归方法实现无限级分类的代码
php实现的无极分类(递归)的代码