Click here to Skip to main content
16,011,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have table in that id,refid,name columns in my table
my data like 1 0 A
2 0 B
3 0 C
4 1 A1
5 1 A2
6 2 B1
7 2 B2
8 3 C3
9 3 c4
10 3 c5
11 2 C7
12 2 C8
13 11 c9
14 11 c10

my output should be A
--A1
--A2
B
--B1
--B2
C
--C3
--C4
--C5
upto this it is working fine but if i add subitems inside another subitems how to check and print the values please help me thanks in advance

What I have tried:

i tried this but not getting the answer please help me.
PHP
<pre><?php

$con = new mysqli("localhost", "root", "", "test");

$topLevelItems = "SELECT id,refid,name FROM `test` WHERE refid = 0";
$res=mysqli_query($con,$topLevelItems);

foreach($res as $item) {
 echo '<a href="...">$item["name"]</a>';
 $subItems ="SELECT id, name FROM `test` WHERE refid='$item['id']'";
 $res2=mysqli_query($con,$subItems)
 foreach($res2 as $subItem) {
    echo '<a href="...">$subItem["name"]</a>';
 }
}



 ?>
Posted
Updated 8-Nov-17 1:26am
v2

1 solution

i have got solution thanks.

$resultMainMenu = mysql_query("SELECT * FROM test WHERE refid=0 ORDER BY name ASC") or die(mysql_error());
while($row = mysql_fetch_array($resultMainMenu)){
    echo $row['name'] . '<br />'; // echo main menu
    $resultSubmenu = mysql_query("SELECT * FROM test WHERE refid=" . $row['id'] ." ORDER BY name ASC") or die(mysql_error());
    if(mysql_num_rows($resultSubmenu) >= 1){
        while($rowSub = mysql_fetch_array($resultSubmenu)){
            echo ' -- ' . $rowSub['name'] . '<br />'; // echo sub menu
        }
    }
}
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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