recursion - PHP recursive function, to create site navigation as nested list, but without unnecessary menu items -
I dig out the idea of using the recursive function to create my site menu but I have a problem and my age Head for now I need my menu function to return the nested list, but I do not want to display non-active irelevent elements of the tree.
Description I have a mass SQL database that is placed with a table called menu_items, which contains all the common fields for each NAV item (target, link_text, title, etc.) Stores a unique ID for the item and importantly parent_id.
This is everything for debate, for example, for example, is it easy to store this information in an XML file?
For example, there is an example menu with all the elements shown here:
& lt; Ul & gt; & Lt; Li & gt; & Lt; A href = "1.html" & gt; Link 1 & lt; / A & gt; & Lt; Ul & gt; & Lt; Li & gt; & Lt; A href = "1-1.html" & gt; Link 1-1 & lt; / A & gt; & Lt; Li & gt; & Lt; A href = "1-2.html" & gt; Link 1-2 & lt; / A & gt; & Lt; / Ul & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "2.html" & gt; Link 2 & lt; / A & gt; & Lt; Ul & gt; & Lt; Li & gt; & Lt; A href = "2-1.html" & gt; Link 2-1 & lt; / A & gt; & Lt; Li & gt; & Lt; A href = "2-2.html" & gt; Link 2-2 & lt; / A & gt; & Lt; / Ul & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "3.html" & gt; Link 3 & lt; / A & gt; & Lt; / Li & gt; & Lt; / Ul & gt;
But if the current page example is for 1-2.html then I have to be one such menu:
Obviously I will either pass the ID or the name of the current page to the menu function.
Any idea anyone? I have been beaten against my wall for some time: -)
This is my complete Method where the database has a page id, a parent ID, a link (permalink) and a title; Hope it helps.
function make_nav ($ current_page_id) {nav_rec (0, $ current_page_id, static ('SITE_URL'), 0); } Function nav_rec ($ page, $ current_page_id, $ link, $ level) {if (! $ Page) {$ page = array (); $ Page ['page_id'] = '0'; } And {? & Gt; & Lt; A class = "new-link nav-level - & lt ;? = $ Level? & Gt;" Href = "& lt;? = $ Link?" & Gt; & Lt ;? = $ Page ['title']? & Gt; & Lt; / A & gt; & Lt;? } // to subpages of this page Czech $ Page_q = "SELECT id as page_id, parent_id, permalink, page_title as title FROM" .constant ( 'MYSQL_PREFIX'). "Pages WHERE 'page_type` =' page 'and` in_nav` =' 1 'and `status` =' publish 'and` is_deleted` =' 0 'and `parent_id` ='". $ Page [ 'Pej_aidi']. " '; If ($ page_rs = mysql_query ($ page_q)) {if (mysql_num_rows ($ page_rs)) {while ($ page = mysql_fetch_assoc ($ page_rs)) {nav_rec ($ page, $ Current_page_id, $ link $. ['Permalink']. '/', $ Level + 1);}} and {$ level = 0;}} and {$ level = 0;}}
Comments
Post a Comment