0

I want to show menu like this

the incorrect menu

This menu shoul look like correct menu

and should be dynamic. This means if we add new categories or subcategories so design should be same.

// HERE IS MY CODE , I AM USING PHP

      <ul class="dropdown-menu mega-dropdown-menu row">

        <li class="col-sm-4">
          <ul>
            <?php
            $sql = mysql_query("SELECT * FROM categories WHERE parent_id = '0' ");
            while($res = mysql_fetch_array($sql))
            {
            echo '<li class="dropdown-header">'.$res['cat_name'].'</li>';
            $sql1 = mysql_query("SELECT * FROM categories WHERE parent_id = '".$res['cat_Id']."' ");
            while($res1 = mysql_fetch_array($sql1))
            {
            echo '<li><a href="products.php?subCatId='.$res1['cat_Id'].'">'.$res1['cat_name'].'</a></li>';
            }

            }
            ?>

          </ul>
        </li>

      </ul>
Greg
  • 5,862
  • 1
  • 25
  • 52
  • you should read these.. [why shouldn't I use mysql functions in PHP](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1) && [how can I prevent sql injection in php?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – treyBake Dec 13 '19 at 11:40
  • Are you use bootstrap? You want to display 3 column menu? – Mukesh Singh Thakur Dec 13 '19 at 11:45
  • yes i am using bootstrap , please see the links on the question it has two menu images of incorrected and corrected menu – Saad Chaudhary Dec 13 '19 at 11:48
  • https://i.stack.imgur.com/WpYl9.png i want to show like this menu and using bootstrap 3 – Saad Chaudhary Dec 13 '19 at 11:50

2 Answers2

0

Your menu function :

function getMenuTree($parent_id) 
{
    global $con;
    $menu = "";
    $sqlquery = " SELECT * FROM categories WHERE parent_id = '$parent_id'";
    $res=mysql_query($con,$sqlquery);
    while($row=mysql_fetch_array($res)) 
    {
           $menu .="<li><a href='products.php?subCatId='.$row['cat_Id'].''>".$row['cat_name']."</a>";

           $menu .= "<ul>".getMenuTree($row['cat_Id'])."</ul>";

           $menu .= "</li>";

    }

    return $menu;
}

You can call the function where you want:

getMenuTree('0');
Piyush Shukla
  • 244
  • 1
  • 11
0

Please try below code. I think it will work for you.

I have added <div class="col-sm-4"> after the first while loop start, and close </div> before the first while loop end. this class divide categories in 3 part.

 <ul class="dropdown-menu mega-dropdown-menu row">
        <li class="col-sm-4">
          <ul>          
            <?php
                $sql = mysql_query("SELECT * FROM categories WHERE parent_id = '0' ");
                while($res = mysql_fetch_array($sql))
                {
                    // add below line
                    echo '<div class="col-sm-4">';

                    echo '<li class="dropdown-header">'.$res['cat_name'].'</li>';
                    $sql1 = mysql_query("SELECT * FROM categories WHERE parent_id = '".$res['cat_Id']."' ");

                    while($res1 = mysql_fetch_array($sql1))
                    {
                        echo '<li><a href="products.php?subCatId='.$res1['cat_Id'].'">'.$res1['cat_name'].'</a></li>';
                    }

                    // add below line
                    echo "</div>";
                }
            ?>
          </ul>
        </li>
    </ul>
Mukesh Singh Thakur
  • 1,335
  • 2
  • 10
  • 23