PHP implements infinite pole column classification

Source: Internet
Author: User

First, create a DB

CREATE TABLE IF not EXISTS ' class ' (   ' id ' mediumint (6) is not NULL auto_increment,   ' title ' varchar () ' is not NULL,   ' PID ' Mediumint (6) Not NULL default ' 0 ',   PRIMARY KEY (' id ')) engine=myisam  default Charset=utf8

And then insert the data

INSERT INTO ' class ' values (1, ' Brand Ladies ', 0); insert INTO ' class ' values (2, ' fine menswear ', 0); insert INTO ' class ' values (3, ' dresses ', 1) INSERT INTO ' class ' values (4, ' cheongsam ', 1); INSERT INTO ' class ' values (5, ' Tuxedo ', 2);

The structure of the data can be designed by itself

Recursive method is used to realize infinite pole column classification.

<?php $con = mysql_connect ("localhost", "root", "123456") mysql_query ("Set names ' UTF8 '"); mysql_select_db ("Test"); function get_str ($id = 0) {     global $str;     $sql = "Select Id,title from class where pid= $id";      $result = mysql_query ($sql);    if ($result && mysql_affected_rows ()) {        $str. = ' <ul> ';         while ($row = Mysql_fetch_array ($result)) {             $str. = ' <li> '. $row [' id ']. "--" . $row [' title ']. "</li>";             Get_str ($row [' id ']);         }         $str. = ' </ul> ';     }     return $str; } Echo get_str (0);?>

Returns an array

<?php $con = mysql_connect ("localhost", "root", "123456") mysql_query ("Set names ' UTF8 '"); mysql_select_db ("Test"); function Get_array ($id =0) {     $sql = "Select Id,title from class where pid= $id";     $result = mysql_query ($sql);    $arr = Array ();     if ($result && mysql_affected_rows ()) {while         ($rows =mysql_fetch_assoc ($result)) {             $rows [' list '] = Get _array ($rows [' id ']);             $arr [] = $rows;         }         return $arr;     } } $list = Get_array (0); Var_dump ($list);? >

PHP implements infinite pole column classification

Related Article

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.