PHP Unlimited classification using concat How to achieve _php skills

Source: Internet
Author: User
Tags explode

First, the database design

---  
table structure for table ' category '- 
-  
 
CREATE Table ' category ' ( 
 ' id ' int (one) not NULL Auto_increme NT, 
 ' catpath ' varchar (255) default NULL, 
 ' name ' varchar (255) default NULL, 
 PRIMARY KEY (' id ') 
engine= MyISAM DEFAULT Charset=utf8 auto_increment=11; 
 
---  
dumping data for table ' category '- 
-  
 
inserts into ' category ' VALUES (1, ' 0 ', ' homepage '); 
INSERT into ' category ' VALUES (2, ' 0-1 ', ' Linux OS '); 
INSERT into ' category ' VALUES (3, ' 0-1 ', ' Apache server '); 
INSERT into ' category ' VALUES (4, ' 0-1 ', ' MySQL database '); 
INSERT into ' category ' VALUES (5, ' 0-1 ', ' PHP scripting language '); 
INSERT into ' category ' VALUES (6, ' 0-1-2 ', ' Linux system Tutorial '); 
INSERT into ' category ' VALUES (7, ' 0-1-2 ', ' Linux Network Technology '); 
INSERT into ' category ' VALUES (8, ' 0-1-2 ', ' Linux security Basics '); 
INSERT into ' category ' VALUES (9, ' 0-1-2-7 ', ' Linux LAMP '); 
INSERT into ' category ' VALUES (Ten, ' 0-1-3-10 ', ' Apache Server ');  

In this note, the Catpath-link symbol is not fixed, you can choose, such as special symbols.

Second, the PHP code implementation

? 
$conn = mysql_connect (' localhost ', ' root ', ' root '); 
mysql_select_db (' Test ', $conn); 
mysql_query (' Set names UTF8 '); 
$sql = "Select Id,concat (Catpath, '-', id) as Abspath,name from category order by Abspath"; 
$query = mysql_query ($sql); 
while ($row = Mysql_fetch_array ($query))  
{ 
  //First Presentation method 
  //$space = Str_repeat (', Count (explode) (' -', $row [' Abspath '])-1); 
  Echo $space. $row [' name ']. ' <br> '// 
   
  Second Presentation method 
  $space = Str_repeat (', Count (Explode ('-', $row [' Abspath '])-1); 
  $option. = ' <option value= '. $row [' id ']. ' > '. $space. $row [' name ']. ' </option> '; 
} 
Echo ' <select name= ' opt ' > '. $option. ' </select> '; 

The MySQL concat function can connect one or more strings

Select Concat (' Yan ', ' pei ', ' Climb ')
select ' id ', ' name ', concat (' id ', '-', ' name ') as Iname

The above is the whole content of this article, introduced the PHP uses concat realizes the wireless classification, hoped for everybody's study to have the help.

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.