Use PHP Arrays for unlimited classification, without databases or recursion.

Source: Internet
Author: User

Copy codeThe Code is as follows:
<? 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 ("add category name") </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 ("added successfully") </script> ';
Echo '<script language = "JavaScript"> location. href = "/cat. php" </script> ';
Exit;
}
Else
{
$ Cat-> wirte ($ _ POST ["sortname"], $ _ POST ["sortlevel"]);
Echo '<script language = "JavaScript"> alert ("added successfully") </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"> root Category </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 = "submit"/> ';
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> ";
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.