欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-09-20 10:18:49
复制代码 代码如下:
复制代码 代码如下:

<?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.php?action=tpl"</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.php"</script>';
        exit;
    }
    else
    {
        $cat->wirte($_post["sortname"],$_post["sortlevel"]);
        echo '<script language="javascript">alert("添加成功")</script>';
        echo '<script language="javascript">location.href="/cat.php"</script>';
        exit;
        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->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>";
}
?>