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