Use PHP arrays to implement an infinite assortment without using a database and recursion. _php Tips

Source: Internet
Author: User
Tags arrays strlen
Copy Code code 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 = "&LT;?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 = "&LT;?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 ("Please 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 ("Add Success") </script> ';
Echo ' <script language= "JavaScript" >location.href= "/cat.php" </script>;
Exit
}
Else
{
$cat->wirte ($_post["Sortname"],$_post["Sortlevel"));
Echo ' <script language= "JavaScript" >alert ("Add Success") </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 classification </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= "Submission" value= "submitted"/> ";
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.