I am attemptting to attach a small CMS to a website I am creating. However I have come across a small problem. The CMS uses PHP functions for inserting menus, these PHP functions create the HTML. The particular function I wish to use (treemenu) creates a nested ul li that can then be used for a drop down menu. However the nested ul li is structured like so:
<li>Projects (Menu Level 1)</li>
<ul>
<li>Project 1 (Menu Level 2)</li>
<li>Project 2 (Menu Level 2)</li>
<li>Project 3 (Menu Level 2)</li>
</ul>
<li>News (Menu Level 1)</li>
<li>Contact (Menu Level 1)</li>
When creating a drop down menu in CSS I believe the Menu Level 1 li should wrap its children like so:
<li>Projects (Menu Level 1)
<ul>
<li>Project 1 (Menu Level 2)</li>
<li>Project 2 (Menu Level 2)</li>
<li>Project 3 (Menu Level 2)</li>
</ul>
</li>
<li>News (Menu Level 1)</li>
<li>Contact (Menu Level 1)</li>
I have never before worked with PHP and therefore would not know how to alter the function in order to accomplish the above. I would hope it would be a simple change. Below is the PHP function that outputs the first example structure:
function treemenu($generat=0) {
global $pagenum, $menu, $selected, $extension, $set;
$count=0;
$out="\n";
$intend=0;
while($menu[$count][0] != "") {
if(strpos($menu[$count][3],"#") === false) {
if($menu[$count][2]=="0" && $intend==2) {
$intend--;
$out.="</ul>\n";
}
if($menu[$count][1]=="0" && $intend==1) {
$intend--;
$out.="</ul>\n";
}
if($menu[$count][1]!="0" && $intend<1) {
$intend=1;
$out.="<ul>\n";
}
if($menu[$count][2]!="0" && $intend<2) {
$intend=2;
$out.="<ul>\n";
}
$out.="<li class=\"LNE_menu\"><a ";
if($menu[$count][4]==$selected['name'])
$out.= 'class="selected" ';
if(strpos($menu[$count][3],"*"))
$out.='href="'.str_replace("*", "",$menu[$count][3]).'">';
elseif($generat)
$out.='href="'.$menu[$count][3].".".$set['extension'].'">';
else
$out.='href="'.$set['indexfile'].'?page='.$menu[$count][3].'">';
$out.=$menu[$count][4]."</a></li>\n";
}
$count++;
}
return $out;
}
Could anyone possibly point me in the right direction as to how to make the closing li tag of a level 1 menu item wrap the ul immediately after, as in the second example?
This would be a excellent example of the use of recursion. An array (with sub-arrays within it) defines each level, and a function loops, calling itself whenever it finds a new array to process. As long as the function cleans up appropriately (closing the
</li> & </ol>
), it's largely automatic.I think this outputs the correct HTML structure.
Same test code as above:
It appears Topbit already beat me to this, but mine is slightly differs in that it doesn't echo the value straight to the output stream, but saves it in a variable that you may
echo
at your convenience:Edit:
Thanks Gumbo and Topbit, now that I'm on my machine with PHP installed, I have tested it and it works fine.
I wrote this utilizing the PHP DOMDocument class. It will create your list as long and as deep as the list you give it using one simple and easy to follow loop. If you want it organized, just ask your database for the list sorted by title (or whatever) first. It assumes your query object data is contained in $menu_categories and it at least has properties of ->id ->parent_id & ->title (change to your needs)
Here's the commented version...
A simple function is all you need.
Test code: