PHP: How to automate building a 100 <UL>/<LI> menuitems, while keeping the Menu Structure File Flat / Simply Managable?
- by Sam
Above: current "stupid" menu. (entire ul/li menu for javascript menu system) + (some li lines as page-specific submenu)
Hi folks! With passion for automation and elegancy, but limited knowledge/knowhow, im stuck with "my hands in my hair" as we Dutch say, for my current menu system works perfectly, but is a pain in the a*s to update! So, i would appreciate it greatly, if you can suggest how to automate this in php: how to let the php generate the html menu code basing on a flat menu input file with TABS indented.
OLD SITUATION
<ul>
<!-- about 100 of these <li>....</li> lines -->
<li><a href="carrot.php"><p class="mnu" style="background-position:0 -820px"><? echo __("carrot juice") ?></p></a></li>
<!-- lots of data, with only little bit thats really the menu itself-->
</ul
a javascript file reads a ul/li structure as input to build menu of format
in that ul/li, the items with a hyperlink and sprite-bg position represent webpages, (inside LI)
while items without hyperlink and sprite-bg are just headers of that menusection, (inside H6)
to highlight the current page in the menu, the javascript menumaker uses an id number.
this number corresponds to the consequtive li that is a webpage, skips h6 headers correctly.
these h6 headers are only there for when importing sections of the same menu as submenu.
non-li headers are not shown in menu, nore counted by the javascript menu for their ID.
to know which page should be shown, i have to count from ID 0, the li items till finding the current webpage in the li structure and then manually put it in each webpage!
BUT: changing an item in li order, means stupidly re-counting their entire li again!
each webpage has an icon (= sprite bg-position numer), which is also used in the webpage.
INTENDED RESULT
I dream of, once setting what the current webpage is (e.g carrot.php) the menu system automatically "finds" and "counts" the li's and returns the id nr (for proper highlight of main menu); generates the entire menu html, and depending on which headings are set for submenu, (e.g. meals, drinks) generates those submenu (entire section below each given header); ginally adds h5 highlight inside the li of that submenu item.
For the menu, i wish an easily readable, simple plain txt menu that is indented with tabs, (each tab is one depth for example) and further tabs follow for url and sprite position of icon.
MY DREAM MENU-MANAGEMENT FILE
|>TAB SEPARATED/INDENTED FLATMENU FILE |MUST BE CALCULATED BY PHP:
|>MENUTEXT============URL=============SPRITE=====|ID===TAG==================
|>about "#" -520 |00 li
|> INFORMATION |—— h6
|> physical state "physical.php" -920 |01 li
|> mental health "mental.php" -10 |02 li
|>
|>apetite "#" -1290 |03 li
|> meals "#" -600 |04 li
|> COLD MEAL |—— h6
|> egg salade "salad.php" -1040 |05 li
|> salmon fish "salmon.php" -540 |06 li
|> HOT MEAL |—— h6
|> spare ribs "spareribs.php" -120 |07 li
|> di macaroni "macaroni.php" -870 |08 li
|>
|> drinks "#" -230 |09 li
|> JUCY DRINK |—— h6
|> carrot juice "carrot.php" -820 |10 li
|> mango hive "mango.php" -270 |11 li
DESIRED CHRONOLOGY
php outputs the entire ul/li html so the javascript can show the menu: webpage items go inside li tags, and header items go inside h6 tags, e.g. <h6>JUCY DRINK</h6>
Each website page has a url filename [eg: salad.php]. Based on this given fact, the php menu generator detects the pagename, gives the IDnr of the position of that page according to the li-item nr and sets variable for javascript to highlight current menu item.
the menu items below the specified headers are loaded as submenu
in which the current page.php is wrapped inside h5 to highlight current page in submenu: e.g. (<li><h5><a href="carrot.php"><p>..etc..</p></h5></li>
Question
Which methods / steps / (chronological)ways are there for doing this? I am no good in php programming, but am learning it so please dont write any code without a line of comment why I should use that method etc. Where do I start? If I am unclear in my question, please ask. Thanks. Much appreciated!!
Concrete Task List from the provided Comments/Answers, sofar:
(RobertB) First, get some PHP code working that can read through a tab-delimited file and put the data into an appropriate data structure. NOW WORKING AT THIS