tags:

views:

207

answers:

2

I am creating dynamic menus and submenus in php.... Can't know where to start.... I have written all my queries but need suggestion to iterate the result set with Ul and li elements with it.... There may are may not be submenus for submenu and there may or may not be submenus for menus.....

Menu1
  submenu1
     submenu11
     submenu12
  submenu2
     submenu21
Menu2
  submenu1
     submenu11
     submenu12

Any suggestion....

EDIT:

Here is what i am doing,

<?php

      $con=mysql_connect("localhost","root","");
      $db=mysql_select_db("testt");

      echo '<ul id="nav">';
      $res=mysql_query("SELECT * FROM menu WHERE PARENT_ID=0");
      while($row=mysql_fetch_array($res))
      {
            echo '<li class="top"><a href="#" class="top_link"><span>'.$row['MENU_NAME'].'</span></a></li>';
            $res1=mysql_query("SELECT * FROM menu WHERE PARENT_ID=1 AND CHILD_ID=".$row['ID']."");
            echo '<ul class="sub">';
            while($row1=mysql_fetch_array($res1))
              {
                 echo '<li><a href="#" class="fly">'.$row1['MENU_NAME'].'</li>';
                 $res2=mysql_query("SELECT * FROM menu WHERE PARENT_ID=2 AND CHILD_ID=".$row1['ID']."");
                 echo '<ul>';
                 while($row2=mysql_fetch_array($res2))
                    {
                        echo '<li><a href="#">'.$row2['MENU_NAME'].'</li>';
                    }
                  echo '</ul>';
              }
              echo '</ul>';
      }
      echo '</ul>';





      ?>

Here is my table,

alt text
My CSS Class doesn't seem to get applied

+1  A: 

Edit: Posted before you clarified your question. Here's a general recursive menu structure.

<?php
function print_menu($menu)
{
  echo "<ul>\n";
  foreach ($menu as $submenu)
  {
    echo "<li>\n";
    echo "<span>{$submenu['name']}</span>\n";
    if (!empty($submenu['children']))
      print_menu($submenu['children']);
    echo "</li>\n";
  }
  echo "</ul>\n";
}

print_menu(
  array(
    array(
      'name' => 'Menu 1',
      'children' => array(
        array(
          'name' => 'Submenu 11',
          'children' => array(
            array('name' => 'Submenu 111'),
            array('name' => 'Submenu 112')
          )
        ),
      )
    ),
    array(
      'name' => 'Menu 2'
    )
  )
);
?>
konforce
+1  A: 

This is how i do:

The css file:

ul.makeMenu, ul.makeMenu ul {
    background-color:#000000;
    cursor:default;
    margin:0px 0px 0px 0px;
    padding:0px 0px 0px 0px;
    width: 180px;
}
ul.makeMenu li {
    background-image:url(../images/marker_rosu.gif);
    background-position:5px center;
    background-repeat:no-repeat;
    color:#D4020B;/*#FEBF01;*/
    list-style-type:none;
    position:relative;
    margin:0px 0px 0px 0px;
    padding:0px 5px 0px 15px;
    line-height:24px;
    text-align:left;
}
ul.makeMenu li>ul {
    display:none;
    position:absolute;
    top:0px;
    left:180px;
}
ul.makeMenu li:hover {
    background-image:url(../images/marker_negru.gif);
    background-position:5px center;
    background-repeat:no-repeat;
    background-color:#D4020B;/*#FEBF01;*/
    color:#FFFFFF;
}
ul.makeMenu {
    display:block;
}
ul.makeMenu li:hover>ul {
    display:block;
}
ul.makeMenu li a {
    color:#D4020B;/*#FEBF01;*/
    display:block;
    width:100%;
    text-decoration:none;
}
ul.makeMenu li a:hover {
    color:#000000;
}
ul.makeMenu li:hover>a {
    color:#000000;
}

the sql file for products or anything else category table:

CREATE TABLE `category` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `idParrent` int(10) unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

and the function that create menu:

function getAFP ($id)
    {
        global $conx;
        $sql = sprintf("SELECT * FROM category WHERE idParrent = %d;", quote_smart($id));
        $rs = mysql_query($sql, $conx);
        $arrayResult = array();
        if (mysql_num_rows($rs)) {
            while ($row = mysql_fetch_array($rs)) {
                array_push($arrayResult, $row);
            }
            return $arrayResult;
        } else {
            return 0;
        }
    }

function makeListaCategory ($id)
    {
        $listaCategorii = array();
        $categorii = getAFP($id);
        if (! $categorii) {
            return 0;
        } else {
            foreach ($categorii as $categorie) {
                array_push($listaCategorii, $categorie[2] . "|" . $categorie[0]);
                $subcategorii = makeListaCategory($categorie[0]);
                if ($subcategorii) {
                    foreach ($subcategorii as $subcategorie) {
                        $valori = explode("|", $subcategorie);
                        array_push($listaCategorii, $categorie[2] . ">" . $valori[0] . "|" . $valori[1]);
                    }
                }
            }
        }
        return $listaCategorii;
    }

I hope this will help you!

D.Martin