Example of the function of retrieving the drop-down tree from a database using PHP + mysql
This example describes how to obtain the drop-down tree from a database using PHP + mysql. We will share this with you for your reference. The details are as follows:
<? Phpinclude "config. php "; include" MySQL. php "; $ db = new Mysql ('test'); // Several Simple classes that you can understand without listing. Just instantiate a database connection. Function RootMenu ($ PID, $ n) {global $ arr, $ db; $ SQL = "select * from menu where 'pid' = $ PID "; $ result = $ db-> query ($ SQL); while ($ I = $ db-> fetch_array ($ result )) {$ I ["TITLE"] = str_repeat ('--', $ n ). $ I ["TITLE"]; $ arr [] = $ I; RootMenu ($ I ["ID"], ($ n + 4);} return $ arr ;} $ arr = RootMenu (0, 0);?> <Select id = ""> <option value = "0" selected = "selected"> select a department </option> <? Phpfor ($ I = 0; $ I <count ($ arr); $ I ++) {?> <Option value = "<? Php echo $ arr [$ I] ["ID"]?> "> <? Php echo $ arr [$ I] ["TITLE"]?> </Option> <? Php }?> </Select>