Hi
I am almost near to where I want to be but need a final push from someone out there please because my brain is now dead!
I have created this recursive function which loops through my database table and displays my navigation options. All is working fine except for when I have a 3rd tier (or more).
An example of where things are going wrong would be as follows..
These URL's work in my current function:
mydomain.com/products
mydomain.com/products/level-one-product
But when 'level-one-product' has a child, my function is showing the URL incorrectly as such:
mydomain.com/products/level-two-product
and as you can see it is removing the /level-one-product part of the URL.
Any help would be greatly appreciated and here is my function:
function build_navigation($parent, $urlarray) {
$qry = "SELECT * FROM sections WHERE childof = '$parent'";
$result = mysql_query($qry);
$count = mysql_num_rows($result);
if ($count > 0) {
while ($row = mysql_fetch_array($result)) {
$sectionid = $row['sectionid'];
$title = $row['title'];
$url = $row['url']; // e.g. about-us or contact-us etc
$childof = $row['childof']; // the Parent record sectionid
if ($childof > 0) {
$path = $urlarray[0] ."/". $url;
} else {
$path = $url;
}
if (in_array($url, $urlarray)) { // if it's a live option
echo "<li><a class=\"active\" href=\"/$path\" title=\"title\">LIVE: $title</a></li>";
build_navigation($sectionid, $urlarray);
} else {
echo "<li><a href=\"/$path\" title=\"title\">$title</a></li>";
}
}
mysql_free_result($result);
}
}
// call the function first time and set the parentid to be 0
// so I only display the parent options only
// $urlarray contains the exploded $_SERVER['REQUEST_URI']
build_navigation(0, $urlarray);