PHP ?? ?? ??? ??? ???? ?? ??? ?????. (1)
?? ??? ?????? ???? ???? ???
<?php $link = mysqli_connect('localhost','username','password','test'); mysqli_set_charset($link, "utf8"); if (!$link) { die("連接失敗:".mysqli_connect_error()); } ?>
? ??? ???? ??? 3?? ??? ?????.
sort id type int.
???? ?? ?? varchar ??.
???? pid int ??.
??? ??? ???:
????:
??? ?? ?? get_str? ????, ?? ??? pid = 0?? ????, SQL ?? ???? ?? ???? ????, ??? ?? ???? $? ?????. ?? ,
while ??? ???? ?? ???? ????, ???? ???? ?? ??? ???, ??? ?? ?? get_str? ????, ?? ???? ID? ??? ?? ??
? ??? ?? ???? ?????. ?? ??.
<?php function get_str($id = 0) { global $str; global $link; //global 關(guān)鍵詞用于訪問(wèn)函數(shù)內(nèi)的全局變量。 $sql = "select id,title from class where pid= $id"; $result = mysqli_query($link,$sql);//查詢pid的子類的分類 if($result){//如果有子類 $str .= '<ul>'; while ($row = mysqli_fetch_array($result)) { //循環(huán)記錄集 $str .= "<li>" . $row['id'] . "--" . $row['title'] . "</li>"; //構(gòu)建字符串 get_str($row['id']); //調(diào)用get_str(),將記錄集中的id參數(shù)傳入函數(shù)中,繼續(xù)查詢下級(jí) } $str .= '</ul>'; } return $str; } echo get_str(0); ?>
??? ??? ?????.