1 回答

TA貢獻1839條經驗 獲得超15個贊
我會為此使用遞歸函數。
function generate_sitemap($site_id, $parent_id=0) {
? ? global $dbcon;
? ? $return = '';
? ? $query = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id'");
? ? // side note: once you get this working it will be worth the effort to turn
? ? // this query into a prepared statement. Using variables in your query like
? ? // this puts you at risk of a SQL injection attack.
? ? if (mysql_num_rows($query) > 0) {
? ? ? ? $return = '<ul>';
? ? ? ? $icon = array('page' => 'fa-file-word', 'post' => 'fa-newspaper', 'event' => 'fa-calendar-star', 'calander' => 'fa-calendar-alt', 'training' => 'fa-award', 'people' => 'fa-user-alt', 'group' => 'fa-users-class');
? ? ? ? while ($array = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
? ? ? ? ? ? $return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array['post_type']] . '"}\'>' . $array['post_title'];
? ? ? ? ? ? $return .= generate_sitemap($site_id, $array['id']);
? ? ? ? ? ? $return .= '</li>';
? ? ? ? }
? ? ? ? $return .= '</ul>';?
? ? }
? ? return $return;
}
所以基本上,開始時假設父頁面沒有任何子頁面(初始$return值為空字符串,所以我們不返回空值<ul></ul>)。
僅當有孩子時,才開始構建<ul>所有子頁面。
在<li>子頁面的內部,讓函數調用自身,并將孩子的 ID 作為新的父 ID。如果孩子沒有子頁面,嵌套函數調用將返回一個空字符串。否則,它將返回 a<ul>及其所有子項。
- 1 回答
- 0 關注
- 128 瀏覽
添加回答
舉報