Two solutions for the implementation of PHP infinite pole Classification _php example

Source: Internet
Author: User
Tags arrays

Today wrote the next infinite polar classification below the code posted up to write a bad.

Method of classify one

Copy Code code as follows:

<?php
/*

Reader: This is written by yourself the method of realizing the infinite pole classification The editing method is just to edit the category name
No mobile operation, little brother ability, forget everybody forgive me.

The first method: CREATE TABLE ' Types ' (
' type_id ' int (one) not NULL auto_increment,
' type_name ' varchar not NULL,
' type_p_id ' varchar not NULL DEFAULT '-',
PRIMARY KEY (' type_id '),
KEY ' type_name ' (' type_name '),
KEY ' Tname ' (' type_name ')
) Engine=myisam auto_increment=14 DEFAULT Charset=utf8
Note:
No field validation is done here;
TYPE_ID represents a primary key self-increasing
Type_name represents the category name
TYPE_P_ID Representation Classification path The category path here is the category path of the upper parent class plus the primary key ID of the class is separated by commas
*/
Error_reporting (7);
Header ("Content:text/html;charset=utf-8");
This is the first procedure to judge
$arr = Array (' list ', ' Add ', ' del ', ' edit ', ' Addok ', ' edit_ok ');
$act = In_array ($_get[' AC '), $arr)? $_get[' AC ': $arr [0];
Connecting to a database
$conn = mysql_connect ("localhost", "root", "root") or Die (' Database connection failed ');
mysql_select_db ("study", $conn);
mysql_query ("Set names UTF8");
Sort according to the classification layer
$sql = "SELECT * from Types order by type_p_id";
$sql 1 = mysql_query ($sql);
Add a Category
if ($act = = "Addok") {
$type _id = $_post[' type_id '];
$type _name = $_post[' type_name '];
If equal to 0 indicates the directory
if ($type _id== "0") {
$sql = "INSERT INTO types set type_name = ' {$type _name} '";
$res = mysql_query ($sql);
$id = mysql_insert_id ();
$sql = "Update types Set type_p_id = ' $id, ' where type_id= $id";
$res = mysql_query ($sql);
if (Mysql_affected_rows () >0) {
echo "<script>location.href= ' ok.php?act=list ' </script>";
}else{
echo "<script>alert (' Add failed ');</script>";
}
}//if not equal to 0
else{
According to typeID, find the type_p_id under this ID
$sql = "Select type_p_id from ' types ' where type_id = $type _id";
$res = mysql_query ($sql);
$res = Mysql_fetch_assoc ($res);
$type _id = $res [' type_p_id '];

Insert the name first
$sql = "INSERT INTO types set type_name = ' {$type _name} '";
$res = mysql_query ($sql);
Get the last executed ID then make the Data update major update type_p_id
$id = mysql_insert_id ();
$sql = "Update types Set type_p_id = ' $type _id$id, ' where type_id= $id";
$res = mysql_query ($sql);
if ($res) {
echo "<script>location.href= ' ok.php?act=list ' </script>";
}else{
echo "<script>alert (' Add failed ');</script>";
}
}
}elseif ($act = = "Add") {
?>
<form method= "POST" action= "Ac=addok" >
New category name: <input type= "text" name= "Type_name" ><br/>
Current Category: <select name= "type_id" >
<option value= "0" > Top class </option>
<?php
Loop Traversal classification
while ($res = Mysql_fetch_assoc ($sql 1)) {
Find the number of times the "," number appears
$m =substr_count ($res [' type_p_id '], ",");
Replace "space" with spaces
$strpad = Str_pad ("", $m *8*2, "");
if ($m ==1) {
$sele = "Disabled";
}else{
$sele = "";
}
?>
<option value= "<?php echo $res [' type_id ']?>" ><?php echo $strpad. $res [' Type_name ']?></option >
<?php
}
?>
</select><br/>
<input type= "Submit" value= "Add"/>
</form>
<?php
}elseif ($act = = "List") {
Get the list sorted by category layer
$sql = "SELECT * from Types order by type_p_id";
$res = mysql_query ($sql);
?>
<table width= "50%" style= "font-size:12px;margin-left:20%;" >
<tr><td>id</td><td> category name </td><td>path path </td><td> operations </td> </tr>
<?php
while ($arr = Mysql_fetch_assoc ($res)) {?>
<tr>
<td><?php echo $arr [' type_id ']?></td>
<td><?php echo $arr [' Type_name ']?></td>
<td><?php echo $arr [' type_p_id ']?></td>
&LT;TD ><a href= "? ac=edit&type_id=<?php echo $arr [' type_id '];? > > Edit </a> |
<a href= "? ac=del&type_id=<?php echo $arr [' type_id '];? > "> Delete </a></td>
</tr>
<?php
}
?>
</table>
<input type= "button" value= "Add" onclick= "(location.href= '? Ac=add ')" >
<?php
}elseif ($act = = "Edit") {
$id = $_get[' type_id '];
$sql = "Select *from ' Types ' where type_id= $id";
$res = mysql_query ($sql);
echo "<form method= ' post ' action= '" Ac=edit_ok ' > ';
while ($arr = Mysql_fetch_assoc ($res)) {
echo "Current name: {$arr [' type_name ']}". " <br/> ";
echo "New name: <input type= ' text ' name= ' N_type_name ' >". <br/> ";
echo "<input type= ' hidden ' value={$arr [' type_id ']} name= ' id '/> ';
}
echo "<input type= ' value= ' update ' >";
echo "</form>";
}elseif ($act = = "Edit_ok") {
$name = Trim ($_post[' n_type_name ']);
$id = $_post[' id '];
if (!empty ($name)) {
$sql = "Update ' types ' Set type_name= ' $name ' where type_id= $id";
mysql_query ($sql);
echo "<script>location.href (' ok.php?act=list ') </script>";
}else{
echo "<script>location.href (' ok.php?act=list ') </script>";
}
}elseif ($act = = ' del ') {
The deletion here is to remove the current category and the current subcategory, so use the $id%
$id = $_get[' type_id '];
$sql = "Delete from ' types ' where type_p_id like ' $id% '";
$res = mysql_query ($sql);
if ($res) {
echo "<script>location.href (' ok.php?act=list ') </script>";
}else{
echo "<script>alert (' delete failed ');</script>";
}
}
?>


Types Table:

Here is the effect chart:

Method of classify two

Copy Code code as follows:

<?php
/*

Reader
This is your own writing of the Infinite Pole classification implementation of the editor is very simple (you know. ) It's not written.
No mobile operation, little brother ability, forget everybody forgive me.
The second method:
CREATE TABLE ' Types ' (
' type_id ' int (one) not NULL auto_increment,
' type_name ' varchar not NULL,
' type_p_id ' varchar not NULL,
PRIMARY KEY (' type_id '),
KEY ' type_name ' (' type_name '),
KEY ' Tname ' (' type_name ')
) Engine=myisam auto_increment=14 DEFAULT Charset=utf8
Note:
No field validation is done here;
TYPE_ID represents a primary key self-increasing
Type_name represents the category name
TYPE_P_ID represents a classification path the category path here is the top category ID if it is the current category this value is 0
*/
Error_reporting (7);
Header ("Content-type:text/html;charset=utf-8");
This is the first procedure to judge
$arr = Array (' list ', ' Add ', ' del ', ' edit ', ' Addok ', ' edit_ok ');
$act = In_array ($_get[' AC '), $arr)? $_get[' AC ': $arr [0];
Connecting to a database
$conn = mysql_connect ("localhost", "root", "root") or Die (' Database connection failed ');
mysql_select_db ("study", $conn);
mysql_query ("Set names UTF8");

if ($act = = "Add") {
/**
* @access Public
* @param array $types Arrays Here are the arrays to refer to &
* @param interal $pid belong to the category upper category ID
* @param int $path classification layer by default starting from 0 each cycle plus a
* @return Array ();
*/
Function GetTypes (& $types =array (), $pid =0, $path =0) {
$sql = "SELECT * from ' type ' where type_p_id = $pid";
$res = mysql_query ($sql);
while ($row = Mysql_fetch_assoc ($res)) {
$str = "";
for ($i =0; $i < $path; $i + +) {
$str. = "";
}
$row [' new_type_name '] = $str. $row [' type_name '];
$types [] = $row;
GetTypes ($types, $row [' type_id '], ($path + 1));
}
return $types;
}
Get category Call function
GetTypes ($types);
Get the list sorted by category layer
$sql 1 = "SELECT * from Type Order by type_id";
$SQLL = mysql_query ($sql 1);
?>
<form method= "POST" action= "Ac=addok" >
New category name: <input type= "text" name= "Type_name" ><br/>
Current Category: <select name= "type_id" >
<option value= "0" > Top class </option>
<?php
Loop this array will classify the correct output
for ($i =0; $i <count ($types); $i + +) {
?>
<option value= "<?php echo $types [$i] [' type_id ']?> ' ><?php echo $types [$i] [' New_type_name ']]?></ Option>
<?php
}
?>
</select><br/>
<input type= "Submit" value= "Add"/>
</form>
<?php
}elseif ($act = = "Addok") {
$type _name = $_post[' type_name '];
$type _id = $_post[' type_id '];
$sql = "INSERT INTO ' type ' (type_name,type_p_id) VALUES (' $type _name ', ' $type _id ')";
$res = mysql_query ($sql);
if (mysql_insert_id () >0) {
echo "<script>location.href= ' two.php?act=list ' </script>";
}else{
echo "<script>alert (' Add failed ');</script>";
}
}elseif ($act = = "List") {
Get the list sorted by category layer
$sql = "SELECT * from Type Order by concat (type_id,type_p_id)";
$res = mysql_query ($sql);
?>
<table width= "50%" style= "font-size:12px;margin-left:20%;" >
<tr><td>id</td><td> category name </td><td>path path </td><td> operations </td> </tr>
<?php
while ($arr = Mysql_fetch_assoc ($res)) {?>
<tr>
<td><?php echo $arr [' type_id ']?></td>
<td><?php echo $arr [' Type_name ']?></td>
<td><?php echo $arr [' type_p_id ']?></td>
&LT;TD ><a href= "? ac=edit&type_id=<?php echo $arr [' type_id '];? > > Edit </a> |
<a href= "? ac=del&type_id=<?php echo $arr [' type_id '];? > "> Delete </a></td>
</tr>
<?php
}
?>
</table>
<input type= "button" value= "Add" onclick= "(location.href= '? Ac=add ')" >
<?php
}elseif ($act = = "Del") {
/***
To delete a large category here, you must delete the subcategories under the large category.
So here we start MySQL transaction MySQL transaction open mode transaction details please refer to
*/
mysql_query ("SET autocommit=1");/Open transaction
$type _id = $_get[' type_id '];
Delete this category
$sqlone = "Delete from ' type ' where type_id= $type _id";
Delete sub categories under this category
$sqltwo = "Delete from ' type ' where type_p_id= $type _id";
$res 1 = mysql_query ($sqlone);
$res 2 = mysql_query ($sqltwo);
if ($res 1 && $res 2)
{
mysql_query ("COMMIT");
echo "<script>location.href= ' two.php?act=list ' </script>";
}else{
mysql_query ("ROLLBACK");
echo "<script>alert (' delete failed ');</script>";
}
}
?>


Type table:

Here is the effect chart

The writing is really not how Ah also hope everyone forgive me.

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.