I've a working code and display well. Now I want to integrate into html code but I don't know how to add a custom div into it.

Here's my php

  function hasChild($parent_id)
  {
    $sql = "SELECT COUNT(*) as count FROM w_category WHERE ParentID = '" . $parent_id . "'";
    $qry = mysql_query($sql);
    $rs = mysql_fetch_array($qry);
    return $rs['count'];
  }

  function CategoryTree($list,$parent,$append)
  {

    $list = '<a href="dd">'.$parent['Name'].'</a>';

    if (hasChild($parent['ID'])) // check if the id has a child
    {
      $append++;
      $list .= "<ul class='child child".$append."'>";
      $sql = "SELECT * FROM w_category WHERE ParentID = '" . $parent['ID'] . "'";
      $qry = mysql_query($sql);
      $child = mysql_fetch_array($qry);
      do{
        $list .= CategoryTree($list,$child,$append);
      }while($child = mysql_fetch_array($qry));
      $list .= "</ul>";
    }
    return $list;
  }
  function CategoryList()
  {
    $list = "";

    $sql = "SELECT * FROM w_category WHERE (ParentID = 0 OR ParentID IS NULL)";
    $qry = mysql_query($sql);
    $parent = mysql_fetch_array($qry);
    $mainlist = '
                        <ul class="nav">';

    do{
      $mainlist .= CategoryTree($list,$parent,$append = 0);
    }while($parent = mysql_fetch_array($qry));
    $list .= '</ul>
    ';
    return $mainlist;
  }

Here is my html:

<ul class="primary-nav">
            <li><a class="active-page" href="index.html">Home</a></li>
            <li><a href="#">Pages</a>
              <div class="submenu_wrapper column4">
                <ul>                  
                  <li><a href="index.html">Homepage A</a></li>
                  <li><a href="index2.html">Homepage B</a></li>
                  <li><a href="index3.html">Homepage C</a></li>
                  <li><a href="boxed.html">Boxed Layout</a></li>
                  <li><a href="background-slideshow.html">Background Slideshow</a></li>
                  <li><a href="category-grid.html">Category Grid</a></li>
                </ul>
            </div>
</ul>

my code is working well for ul and li tag but I want to add

<div class="submenu_wrapper column4"> and </div> once there is sub-category.

Please help me.

Hello, okay I'll fix this to make it exactly match the HTML code, but I am interested in who gave you this code ;) the recursion is smart ...
I will assume that the categoryList() is the function to call to display the navigation menu

function hasChild($parent_id)
  {
    $sql = "SELECT COUNT(*) as count FROM w_category WHERE ParentID = '" . $parent_id . "'";
    $qry = mysql_query($sql);
    $rs = mysql_fetch_array($qry);
    return $rs['count'];
  }

  function CategoryTree($list,$parent,$append)
  {

    // here you should have a url for each link that doesn't have children
    // or if you don't have url as attribute in the table, just keep it index.html for testing
    $list = '<li><a href="$parent['url']" class="active-page">'.$parent['Name'].'</a></li>';

    if (hasChild($parent['ID'])) // check if the id has a child
    {
      $list = '<li><a href="#">'.$parent['Name'].'</a> 
                   <div class="submenu_wrapper column4">
                       <ul class='child child".++$append."'>';

      $sql = "SELECT * FROM w_category WHERE ParentID = '" . $parent['ID'] . "'";
      $qry = mysql_query($sql);
      $child = mysql_fetch_array($qry);
      do{
        $list .= CategoryTree($list,$child,$append);
      }while($child = mysql_fetch_array($qry));
      $list .= "</ul>
              </div>
          </li>";
    }
    return $list;
  }
  function CategoryList()
  {
    $list = "";

    $sql = "SELECT * FROM w_category WHERE (ParentID = 0 OR ParentID IS NULL)";
    $qry = mysql_query($sql);
    $parent = mysql_fetch_array($qry);
    $mainlist = '<ul class="primary-nav">';

    do{
      $mainlist .= CategoryTree($list,$parent,$append = 0);
    }while($parent = mysql_fetch_array($qry));
    $list .= '</ul>
    ';
    return $mainlist;
  }

Hope that this fix the prob :)

Member Avatar
diafol

There seems to be a lot of wastage here with many calls to the DB. It should be possible to extract the whole info in one go.

Hi cmps, I've test your code but it's not what I expected.

It displayed like this:

 <ul class="primary-nav"><li><a href="#">Shirt</a> 
                   <div class="submenu_wrapper column4">
                       <ul class="child child1"><li><a href="#">Pant</a> 
                   <div class="submenu_wrapper column4">
                       <ul class="child child2"><li><a href="6" class="active-page">dsa</a></li></ul>
              </div>
          </li><li><a href="5" class="active-page">Shoe</a></li></ul>
              </div>
          </li><li><a href="#">Electronic</a> 
                   <div class="submenu_wrapper column4">
                       <ul class="child child1"><li><a href="4" class="active-page">Aircond</a></li></ul>
              </div>
          </li>

My expectation is to display like this:

Only display "<div class="submenu_wrapper column4">" after parent . Do not repeat it in child category.

I grap this code from internet. :)

Member Avatar
diafol

Here's an old class of mine I modified for your purposes. It's a little rough and ready - you may need to change the array keys (['id'],['ParentID'],['label'],['url']) to suit your keys. You can try it with the typical data from a DB call:

class navig8or{

    private $properties = array();  
    private $nav = '';  

    public function __construct($resourceArray, $activePage)
    {
        $this->properties['parent_content'] = '<li><a%s href="%s">%s</a>%s</li>';
        $this->properties['separator_opentag'] = '<div class="submenu_wrapper column4"><ul>';
        $this->properties['separator_closetag'] = '</ul></div>';
        $this->properties['child_content'] = '<li><a href="%s">%s</a></li>';
        $this->nav = $this->buildArray($resourceArray, $activePage);
    }

    private function buildArray($resourceArray, $activePage)
    {
        $parents = array_filter($resourceArray, array($this, 'getParents'));
        $output = '';

        foreach($parents as $parent)
        {
            $subActivePage = false;
            $childData = '';
            foreach($resourceArray as $child)
            {                   
                if($parent['id'] == $child['ParentID']){
                    if($childData == '') $childData .= $this->properties['separator_opentag'];
                    if($child['url'] == $activePage) $subActivePage = true;
                    $childData .= sprintf($this->properties['child_content'],$child['url'],$child['label']);
                }
            }
            if($childData != '') $childData .= $this->properties['separator_closetag'];

            $class = ($parent['url'] == $activePage || $subActivePage) ? ' class="active-page"' : '';
            $output .= sprintf($this->properties['parent_content'], $class, $parent['url'], $parent['label'], $childData);

        }
        return $output;
    }

    private function getParents($item)
    {
        if($item['ParentID'] === 0) return true;    
    }

    public function printNav()
    {
        return $this->nav;  
    } 

}

You can include the class file witht he code below:

//TYPICAL DATA FROM DB 
$data = array(
    0=> array('id'=>1,'url'=>'index.php','label'=>'Homepage','ParentID'=>0),
    1=> array('id'=>2,'url'=>'about.php','label'=>'About','ParentID'=>0),
    2=> array('id'=>3,'url'=>'page1.php','label'=>'Page 1','ParentID'=>5),
    3=> array('id'=>4,'url'=>'page2.php','label'=>'Page 2','ParentID'=>5),
    4=> array('id'=>5,'url'=>'#','label'=>'Pages','ParentID'=>0),
    5=> array('id'=>6,'url'=>'page3.php','label'=>'Page 3','ParentID'=>5),
    6=> array('id'=>7,'url'=>'page4.php','label'=>'Page 4','ParentID'=>2),
    7=> array('id'=>8,'url'=>'page5.php','label'=>'Page 5','ParentID'=>2),
);

$active = 'page1.php'; // usually get this from the current page

$nav = new navig8or($data, $active);
?>

<!-- IN YOUR PAGE SOMEWHERE -->
<ul class="primary-nav">
    <?php echo $nav->printNav();?>
</ul>