Source code comparison simple and practical PHP unlimited classification source sharing ideas good

Source: Internet
Author: User
The following code is the SQL code that creates the corresponding database:

Copy the Code code as follows:


//////////////
Database design with infinite classification and sample example
//////////////
mysql> CREATE DATABASE Db_kind;
Query OK, 1 row affected
mysql> use Db_kind;
Database changed
Mysql> CREATE TABLE Tb_kind (
-ID int NOT NULL auto_increment primary key,
-PID int,
-Path varchar (200)
);
Query OK, 0 rows affected
mysql> INSERT INTO tb_kind values (NULL, "News", 0,0);
Query OK, 1 row affected
mysql> INSERT INTO tb_kind values (null, "video", 0,0);
Query OK, 1 row affected
mysql> INSERT INTO tb_kind values (null, "Picture", 0,0);
Query OK, 1 row affected
mysql> INSERT INTO tb_kind values (null, "blog", 0,0);
Query OK, 1 row affected
mysql> INSERT INTO tb_kind values (NULL, "Sports News", 1, "0-1");
Query OK, 1 row affected
mysql> INSERT INTO tb_kind values (NULL, "Entertainment News", 1, "0-1");
Query OK, 1 row affected
mysql> INSERT INTO tb_kind values (NULL, "Financial News", 1, "0-1");
Query OK, 1 row affected
Mysql> select * from Db_kind;
ERROR 1146:table ' db_kind.db_kind ' doesnot exist
Mysql> SELECT * from TB
_kind;
+----+----------+-----+------+
| ID | PName | PID | Path |
+----+----------+-----+------+
| 1 | News | 0 | 0 |
| 2 | Video | 0 | 0 |
| 3 | Pictures | 0 | 0 |
| 4 | Blog | 0 | 0 |
| 5 | Sports News | 1 | 0-1 |
| 6 | Entertainment News | 1 | 0-1 |
| 7 | Financial News | 1 | 0-1 |
+----+----------+-----+------+
7 Rows in Set
mysql> INSERT INTO tb_kind values (NULL, "Basketball News", 5, "0-1-5");
Query OK, 1 row affected
mysql> INSERT INTO tb_kind values (NULL, "Football News", 5, "0-1-5");
Query OK, 1 row affected
Mysql> select * from Tb_kind;
+----+----------+-----+-------+
| ID | PName | PID | Path |
+----+----------+-----+-------+
| 1 | News | 0 | 0 |
| 2 | Video | 0 | 0 |
| 3 | Pictures | 0 | 0 |
| 4 | Blog | 0 | 0 |
| 5 | Sports News | 1 | 0-1 |
| 6 | Entertainment News | 1 | 0-1 |
| 7 | Financial News | 1 | 0-1 |
| 8 | Basketball News | 5 | 0-1-5 |
| 9 | Football News | 5 | 0-1-5 |
+----+----------+-----+-------+
9 Rows in Set
mysql> INSERT INTO tb_kind values (null, "NBA", 8, "0-1-5-8");
Query OK, 1 row affected
mysql> INSERT INTO tb_kind values (null, "CBA", 8, "0-1-5-8");
Query OK, 1 row affected
Mysql> select * from Tb_kind;
+----+----------+-----+---------+
| ID | PName | PID | Path |
+----+----------+-----+---------+
| 1 | News | 0 | 0 |
| 2 | Video | 0 | 0 |
| 3 | Pictures | 0 | 0 |
| 4 | Blog | 0 | 0 |
| 5 | Sports News | 1 | 0-1 |
| 6 | Entertainment News | 1 | 0-1 |
| 7 | Financial News | 1 | 0-1 |
| 8 | Basketball News | 5 | 0-1-5 |
| 9 | Football News | 5 | 0-1-5 |
| 10 | NBA | 8 | 0-1-5-8 |
| 11 | CBA | 8 | 0-1-5-8 |
+----+----------+-----+---------+
Rows in Set
Mysql> Select concat (Path, "-", id) from Tb_kind;
+---------------------+
| Concat (Path, "-", id) |
+---------------------+
| 0-1 |
| 0-2 |
| 0-3 |
| 0-4 |
| 0-1-5 |
| 0-1-6 |
| 0-1-7 |
| 0-1-5-8 |
| 0-1-5-9 |
| 0-1-5-8-10 |
| 0-1-5-8-11 |
+---------------------+
Rows in Set
Mysql> Select concat (Path, "-", id) from Tb_kind;
+---------------------+
| Concat (Path, "-", id) |
+---------------------+
| 0-1 |
| 0-2 |
| 0-3 |
| 0-4 |
| 0-1-5 |
| 0-1-6 |
| 0-1-7 |
| 0-1-5-8 |
| 0-1-5-9 |
| 0-1-5-8-10 |
| 0-1-5-8-11 |
+---------------------+
Rows in Set
Mysql> Select concat (Path, "-", id) as ABS from Tb_kind order by Abs.path;
ERROR 1054:unknown column ' Abs.path ' in ' order clause '
Mysql> Select concat (Path, "-", id) as ABS from Tb_kind ORDER BY ABS
+------------+
| ABS |
+------------+
| 0-1 |
| 0-1-5 |
| 0-1-5-8 |
| 0-1-5-8-10 |
| 0-1-5-8-11 |
| 0-1-5-9 |
| 0-1-6 |
| 0-1-7 |
| 0-2 |
| 0-3 |
| 0-4 |
+------------+
Rows in Set
Mysql> Select concat (Path, "-", id) as,id,name,path ABS from Tb_kind ORDER by ABS;
Error 1064:you has an error in your SQL syntax; Check the manual-corresponds to your MySQL server version for the right syntax-use-near ' Id,name,path abs from Tb_ Kind ORDER by ABS ' at line 1
Mysql> Select concat (Path, "-", id) as ABS,
Id,pname,path ABS from Tb_kind ORDER by ABS;
+------------+----+----------+---------+
| ABS | ID | PName | ABS |
+------------+----+----------+---------+
| 0-1 | 1 | News | 0 |
| 0-1-5 | 5 | Sports News | 0-1 |
| 0-1-5-8 | 8 | Basketball News | 0-1-5 |
| 0-1-5-8-10 | 10 | NBA | 0-1-5-8 |
| 0-1-5-8-11 | 11 | CBA | 0-1-5-8 |
| 0-1-5-9 | 9 | Football News | 0-1-5 |
| 0-1-6 | 6 | Entertainment News | 0-1 |
| 0-1-7 | 7 | Financial News | 0-1 |
| 0-2 | 2 | Video | 0 |
| 0-3 | 3 | Pictures | 0 |
| 0-4 | 4 | Blog | 0 |
+------------+----+----------+---------+
Rows in Set
Mysql>


Here is the PHP source file:

Copy the Code code as follows:






Untitled Document



$c
mysql_select_db ("Db_kind");
mysql_query ("Set names UTF8");
$sql = "Select concat (Path, '-', id) as Abspath,id,pname,path from Tb_kind order by Abspath";
$rs =mysql_query ($sql);
while ($result =mysql_fetch_assoc ($rs)) {
$num =count (Explode ("-", $result [path]))-1;
$new _str=str_repeat ("---", $num);
Echo $new _str. $result [PName];
echo "
";
}
$str =str_repeat ("=", 10);
Echo $str;
$num =count (Explode ("-", "0-1-5-8"))-1;
Echo $num;
?>



The above code in fact there are spaces in the input effect is very good, please local testing. Typographical confusion caused by editor problems.

The above introduction of the source code is relatively simple and practical PHP infinite classification source sharing ideas good, including the source of content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.