亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP站點地圖函數

PHP站點地圖函數

PHP
慕尼黑8549860 2023-05-12 15:30:35
和往常一樣,過去幾個小時我都在這上面花了,但無法找到更好的方法。我有一個 CMS,需要創建站點地圖。我有作品的功能,但很亂,不太適合用途,想知道是否有人有任何建議。我在下面有 for 循環和類似的東西,但我還不是所有的人都知道如何使用它們。我已經包括了我已經擁有的功能以及數據庫的演示。tbl_website_posts| ID  | Title             | Parent ID ||-----|-------------------|-----------|| 1   | Parent 1          | 0         || 2   | Parent 2          | 0         || 3   | Child of 2        | 2         |該功能也非常有限,只能容納這么多孩子。如果有人對我可以從哪里開始提出建議,那就太棒了。預先感謝您的寶貴時間。
查看完整描述

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>及其所有子項。


查看完整回答
反對 回復 2023-05-12
  • 1 回答
  • 0 關注
  • 128 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號