|
Posted by robert on 05/15/06 23:57
"Chris" <designerNOSPAM@centurytel.net> wrote in message
news:e4anr5$ntg$1@news01.intel.com...
| I've been working on a basic Parent/Child Menu where if you click on the
| Parent link the Child set of links appears, then when clicked again, it
| disappears. All the parent/child elements are propagated from a MySQL
| database, and there is only 1 set of children to each parent. I have seen
| dozens of Tree menus, but the links are all hard-coded, and have found a
few
| other references for it, but it seems that you can't really do it solely
in
| PHP, but with Javascript/CSS and an OnClick event.
not true...you can do it *all* in php but the screen will have to refresh
after a menu item (parent or child) is clicked. the most common method (and
most convenient for the user) is to have php spit out the html for the
display and have javascript do the collapse and expansion of those
items...assuming the browser has javascript enabled.
if, like you say, no child has children then i would have a self-joined
table house both parent and child records and code similar to the following
(this is pseudo and hasn't been tested):
========= php
$menus = array();
$records = ' your db statement to return results as assoc array here
foreach ($record as $record)
{
$menus[$record['PARENT']][$record['CHILD']] = $record['URL'];
}
$menuTree = '';
foreach ($menus as $parent => $chilren)
{
$id = md5(uniqid(rand()));
$menuTree .= '<div id="' . $id . '" onclick="toggleDisplay(' . $id . ')">'
.. "\r\n";
foreach ($children as $child => $url)
{
$menuTree .= '<a href="' . $child . ">' . $url . '</a><br/>' . "\r\n";
}
$menuTree .= '</div>' . "\r\n";
}
echo $menuTree;
========= javascript
function toggleDisplay(id)
{
el = document.getElementById(id);
if (!el){ return false; }
el.style.display = el.style.display == 'none' ? 'block' : 'none';
return false;
}
hth,
me
Navigation:
[Reply to this message]
|