Recursive implementation of the infinite classification
[Email protected]_connect (' 127.0.0.1 ', ' root ', ' root ') or Die (Mysql_error ());
mysql_select_db (' test ') or Die (' Database connection failed ');
$sql []= "Create TABLE ' test '. ' Dd_book_class ' (
' ID ' INT not NULL auto_increment PRIMARY KEY,
' ClassName ' VARCHAR (not NULL),
' ClassID ' INT not NULL
//)";
$sql []= "Insert ' Dd_book_class ' (classname,classid) VALUES (' novel ', 0)";
$sql []= "Insert ' Dd_book_class ' (classname,classid) VALUES (' literary ', 0)";
$sql []= "Insert ' Dd_book_class ' (classname,classid) VALUES (' Youth ', 0)";
$sql []= "Insert ' Dd_book_class ' (classname,classid) VALUES (' Inspirational/successful ', 0)";
$sql []= "Insert ' Dd_book_class ' (classname,classid) VALUES (' children ', 0)";
$sql []= "Insert ' Dd_book_class ' (classname,classid) VALUES (' suspense ', 1) ';
$sql []= "Insert ' Dd_book_class ' (classname,classid) VALUES (' Romance ', 1) ';
$sql []= "Insert ' Dd_book_class ' (classname,classid) VALUES (' Workplace ', 1) ';
$sql []= "Insert ' Dd_book_class ' (classname,classid) VALUES (' Literature ', 2)";
$sql []= "Insert ' Dd_book_class ' (classname,classid) VALUES (' Popular science ', 5) ';
Print_r ($sql);
foreach ($sql as $value) {
mysql_query ($value) or Die ("Insert data Error");
//}
$class _arr=array ();
$recordset =mysql_query ("select * from ' Dd_book_class '");
while ($row =mysql_fetch_array ($recordset)) {
$class _arr[]=array ($row [' id '], $row [' classname '], $row [' ClassID ']);
}
function Classtree ($n, $id) {
Global $class _arr;
$n = Str_pad (', $n, '-', str_pad_right);
$n =str_replace ('-', ' ', $n);
for ($i =0; $i <count ($class _arr); $i + +) {
if ($class _arr[$i][2]== $id) {
echo $n. ' ┠<a href= '? class= '. $class _arr[$i][0]. ' " > '. $class _arr[$i][1]. ' </a><br> ';
Classtree ($n +1, $class _arr[$i][0]);
}
}
}
Classtree (0,0);
Mysql_close ($con);
PHP simple, class-free classification