Use a PHP array for unlimited categorization, without using a database, without using recursion. _php Tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:

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 = " $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 = " $array. = ' $class = Array '. " RN (RN ";
foreach ($rs as $key = $value)
{
$array. = "RN";
$array. = ' "'. $key. '" = "=". " 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 ';
Echo ';
Exit
}
if (file_exists ("data.php"))
{
$cat->add_cat ($_post["Sortname"],$_post["Sortlevel"]);
Echo ';
Echo ';
Exit
}
Else
{
$cat->wirte ($_post["Sortname"],$_post["Sortlevel"]);
Echo ';
Echo ';
Exit
Exit
}
}

if ($_get["action"] = = "TPL")
{
Echo ' ";
Exit
}

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

http://www.bkjia.com/PHPjc/317449.html www.bkjia.com true http://www.bkjia.com/PHPjc/317449.html techarticle Copy the code as follows: PHP classcat {public$data; Publicfunction__construct () {@include "data.php"; $this-data= $class;} public Functioncreatesortlevel ($fatherlevel) {if (Empt ...

  • Related Article

    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.