PHP recursively generates tree-like instance code. This article introduces the PHP code for recursive generation of tree-like lines? Phpheader (content-type: texthtml; charsetutf-8); $ connectmysql_connect (localhost, root, 123456); mysql_select_db (wz); m This article introduces PHP recursive tree row generation instance code
-
- Header ("content-type: text/html; charset = utf-8 ");
- $ Connect = mysql_connect (localhost, root, 123456 );
- Mysql_select_db ("wz ");
- Mysql_query ("set names utf8 ");
- // Obtain the top node
- $ SQL = "select id, name, ischild from tree where parent = 0 order by id asc ";
- $ Result = mysql_query ($ SQL );
- While ($ row = mysql_fetch_array ($ result ))
- {
- Extract ($ row );
- // If a subnode exists, add an event to it to expand or close the subnode.
- $ Icon = $ ischild? "":"-";
- $ Name = $ icon. $ name;
- Echo"
". $ Name;
- If ($ ischild)
- {
- // Recursively retrieve nodes
- GetNode ($ id, 0 );
- }
- Echo"";
- }
- Function getNode ($ id, $ level)
- {
- $ SQL = "select id, name, ischild from tree where parent = $ id order by id asc ";
- $ Result = mysql_query ($ SQL );
- $ Level;
- While ($ row = mysql_fetch_array ($ result ))
- {
- Extract ($ row );
- $ Icon = $ ischild? "":"-";
- $ Name = $ icon. $ name;
- Echo"
". EchoChar (" ", $ level). $ name;
- If ($ ischild)
- {
- GetNode ($ id, $ level );
- }
- Echo"";
- }
- }
-
- Function echoChar ($ char, $ num)
- {
- For ($ I = 0; $ I <$ num; $ I)
- {
- $ StrChar. = $ char;
- }
- Return $ strChar;
- }
- ?>
-