Php unlimited classification how to implement using concat

Source: Internet
Author: User
This article describes how to use concat for php unlimited classification. For more information, see

This article describes how to use concat for php unlimited classification. For more information, see

-- Table structure for table 'category '-- create table 'category' ('id' int (11) not null auto_increment, '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 '-- insert into 'category' VALUES (1, '0', 'website'); 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 example'); 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 (10, '0-1-3-10 ', 'apache Server ');

Here, the-link symbol of catpath is not fixed and can be selected, and other special symbols.

Ii. 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 display method // $ space = str_repeat ('', count (explode ('-', $ row ['abspath'])-1); // echo $ space. $ row ['name'].'
'; * // Second display method $ space = str_repeat ('', count (explode ('-', $ row ['abspath'])-1 ); $ option. =''. $ Space. $ row ['name'].'';} Echo''. $ Option .'';?>

The MySQL concat function can be used to connect one or more strings.

Select concat ('yan ', 'pei', 'pan ') select 'id', 'name', concat ('id','-', 'name') as iname

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.