0

Hello, I have a function that is working to generate the navigation bar, it grabs the parents and the "childs" and displays them but I have noticed that it is not closing the ul, li tags properly. I will attach a screen shot below.

My code is this:

<?
include 'config.php';
function showMenu($level = 0) {

$result = mysql_query("SELECT * FROM `tbl_structure` WHERE `PARENTID` = ".$level); 
echo "<ul class='nav'>";
    while ($node = mysql_fetch_array($result)) { 
        echo "<li><a href='prodid=".$node['ID']."'>".$node['NAME'];
        $hasChild = mysql_fetch_array(mysql_query("SELECT * FROM `tbl_structure` WHERE `PARENTID` = ".$node['ID'])) != null;
        if ($hasChild) {
            showMenu($node['ID']);
        }
        echo "</a></li>";
    }
echo "</ul>";
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="testcss.css" rel="stylesheet" type="text/css" />
</head>

<body>

<? echo showMenu() ?>

</body>
</html>

Any help would be great!
Thanks

Attachments wrong.PNG 5.85 KB
2
Contributors
2
Replies
19
Views
3 Years
Discussion Span
Last Post by patk570
0

Hai;

Try this

<?php
include("config.php");

function runQry ($qry){
$rows = array();
$result = mysql_query($qry); 
if($result) {
                    while($row = mysql_fetch_object($result)) {
                        $rows[] = $row;
                    }
                } else {
                    $rows = null;
        }
        return $rows;
}   

function getAllMenus ($level=0) {
        $menuList=array();
        $sql="SELECT * FROM `tbl_structure` WHERE `PARENTID` = '$level'";
        $mainMenus=runQry($sql);
        foreach($mainMenus as $mm) {
            array_push($menuList, $mm);
            $subMenu=getAllMenus($mm->ID);  
            if(sizeof($subMenu)>0) {
                array_push($menuList, $subMenu);
            }
        }
        return $menuList;
}   


function generateTree($menuList) {
        $length=sizeof($menuList);

        $tree="<ul>";
        foreach($menuList as $i=>$ml) {
            if(is_array($ml)) {
                $tree=substr($tree, 0, -5);
                $tree.=generateTree($ml);
                $tree.="</li>";
            }
            else {
                $tree.="<li><a href='prodid=".$ml->ID."'>".strtoupper($ml->NAME)."</a></li>";
            }
        }
        $tree.="</ul>";
        return $tree;
    }

?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="testcss.css" rel="stylesheet" type="text/css" />
</head>

<body>

<?php 
echo generateTree(getAllMenus()); 
?>

</body>
</html>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.