Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have table in mysql like below in that colums and data are
id refid name userdefined
1 0 A
2 0 B
3 0 C
5 1 A1
6 5 AA
7 5 AB
8 6 a1
9 6 a2
10 1 A2
11 1 A3
12 2 B1
13 2 B2
14 3 C1
15 3 C2

what i want is using php recursive function add the ul li elements and li ul elements dynamically how to create the data like this.please help me thanks in advance.

  • A

    • A1

      • AA

        • a1
        • a2

      • AB

    • A2
    • A3

  • B

    • B1
    • B2

  • C

    • C1
    • C2



What I have tried:

i have tried this function it is not working as expected .it is adding empty ul empty for each child element inside how to prevent that . please help me thanks in advance.
PHP
<?php

mysql_connect('localhost','root','');

mysql_select_db("test");

function submenu($parentid=0){
    $sql=mysql_query("SELECT * FROM test WHERE refid=".$parentid);
    {
      //echo '<ul>';
            while($row=mysql_fetch_array($sql))
            {
              if(mysql_num_rows($sql)>0)
              {
                echo '<ul>';
              $sql1=mysql_query("SELECT * FROM test WHERE refid=".$row['id']);
              if(mysql_num_rows($sql1)>0 && $row['refid']==0)
              {

              //echo '<li><a href="'.$row['userdefined'].'">'.$row['name'].'</a>';
                echo '<li class="limain">'.$row['name'];
                 //echo '<li><a href="'.$row['userdefined'].'">'.$row['name'].'</a>';
              submenu($row['id']);
              echo '</li>';
              //echo '<ul>';
            }
              else{
               //echo '<ul>';
               echo '<li class="lichild"><a href="'.$row['userdefined'].'">'.$row['name'].'</a>';
               submenu($row['id']);
               echo '</li>';
            }
          }

          echo '</ul>';
      }

    }
  }

submenu();

 ?>
Posted
Updated 13-Nov-17 17:29pm
v3

1 solution

function submenu($parentid = 0)
{
    $sql = mysql_query("SELECT * FROM test WHERE refid=" . $parentid);

    echo '<ul>';

    while($row = mysql_fetch_assoc($sql))
    {
        if($row['refid'] == 0)
        {
            echo '<li>';
            echo $row['name'];
            submenu($row['id']);
            echo '</li>';
        }
        else
        {
            echo '<li>';
            echo "<a href=\"{$row['userdefined']}\">{$row['name']}</a>";
            submenu($row['id']);
            echo '</li>';
        }
    }

    echo '</ul>';
}

submenu();

Though I recommend you moving away from these mysql_functions and converting your code to PDO as those functions are deprecated since 5.5.0 and are completely removed in PHP 7.0.0 (according to the PHP manual)
 
Share this answer
 
v4
Comments
Member 13153537 14-Nov-17 0:56am    
thanks for your help and support.but i want to add class names for each parent element
and child element and each grand child elements using that function .how to do that please help me thanks in advance.
EZW 14-Nov-17 22:46pm    
You mean design each child UL differently in CSS?
Member 13153537 15-Nov-17 0:05am    
yes by using css if i open the page first time it will show only parent elements like
A
B
C

then i click on A it should show child elements of A not grand child elements
again if i click A child elements like A1 it should show A1 child elements how to do that.please give me any example.I am sorry i am asking every small doubt also because i am fresher.thanks in advance.
EZW 15-Nov-17 22:50pm    
https://www.cssscript.com/pure-css-vertical-multi-level-navigation-menu/

They have what you want. Mess around with it to make it yours (Those "has-sub" aren't required. You can have a menu with that CSS without those classes)
Member 13153537 15-Nov-17 23:37pm    
thanks bro for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900