hi, i m having problems displaying values in a tree menu from the database. I ll try explaining wat i did so that that ll help to solve my problem.

I have two tables in the database one with name 'addcourse' and the other with the name 'subcourse'. what i have to do is compare the values from field name called 'cname' from the addcourse table with the feild name called 'cname from the subcourse table and print the values in the form of a tree menu with the sub course names from a field name called 'subcname'' from the 'subcourse' table. (here the 'cname' values are parent values)

i have tried implementing it but could print the parent values 'cname' but could print only the first values from the 'subcname' table. I am how ever posting the code, help appreciated.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.:: menu ::.</title>
<script type="text/javascript" src="collapsibleMenuMem.js"></script>
<link href="menu.css" rel="stylesheet" type="text/css">


<?php include("configdb.php");

$res=mysql_query("select cname FROM addcourse");

 <table border="0" width="218" bgcolor="#ffffff" cellpadding="0" cellspacing="0" style="background:#ffffff; color:#000000;">
     <td class="menu2"> <?php while($row=mysql_fetch_array($res))
      <div class="main style12" onClick="openClose('MN',mainNum,'a1')"><span id="tick_a1">+ </span><span class="mainLink"> 
	  <?php echo $cname;?>
    <?php  $ress= mysql_query("select * FROM subcourse where cname='$cname' ");
<!-- below is an example of nested sub-links under the first main link. -->
   <?php  while($rows=mysql_fetch_array($ress))
      <div id="a1" class="sublinks1">
      <a href="vedicmaths.php" class="link2 style15"> <?php echo $rows['subcname'] ?> </a><br />
	   <?php }
} ?>

<!-- below is an example of nested sub-links under the second main link. -->


I am even adding the js file for the above code.

var exp = 5;

function newCookie(name,value,days) {
  if (days) {
    var date = new Date();
    var expires = "; expires="+date.toGMTString();
  } else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";

function readCookie(name) {
  var nameSG = name + "=";
  var nuller = '';
  if (document.cookie.indexOf(nameSG) == -1)
  return nuller;
  var ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameSG) == 0) return c.substring(nameSG.length,c.length);
  return null;

function eraseCookie(name) {

if (document.getElementById) {
  document.writeln('<style type="text/css">')
  document.writeln('.main {text-decoration:none; color:#616161; font:bold; cursor:hand; cursor:pointer}')
  document.writeln('span:hover.mainLink {text-decoration:none; color:#086ec3}')
  document.writeln('.sublinks1 {display:none; padding-left:14px}')
  document.writeln('.link2 {text-decoration:none; color:#086ec3}')
  document.writeln('a:hover.link2 {text-decoration:none; color:#616161}')
  document.writeln('</style>') }

    // Below you should add a1, a2 etc. for each main link you wish to include
    // so if you want 3 main links you should add a1, a2, a3 in the format shown
    // enclosed in double quotes
  var mainNum = new Array("1","2","3");

    // Below you should add b1, b2 etc. for each sub link you wish to include
    // under one main link, here the first main link. so if you want 4 sub links you
    // should add b1, b2, b3, b4 in the format shown enclosed in double quotes
  var subNum1 = new Array("1","2");

    // Below, this is for sub links under the second main link. there are 3 sub links
    // in the given example
  var subNum2 = new Array("1","2","3");

function openClose(theName, menuArray, theID) {
  for(var i=0; i < menuArray.length; i++) {
    if (menuArray[i] == theID) {
      if (document.getElementById(theID).style.display == "block") {
        document.getElementById(theID).style.display = "none";
        document.getElementById("tick_"+menuArray[i]).innerHTML = "+";
        eraseCookie(theName); }
      else {
        document.getElementById(theID).style.display = "block";
        document.getElementById("tick_"+menuArray[i]).innerHTML = "-";
        newCookie(theName,menuArray[i],exp); }
    else {
      document.getElementById(menuArray[i]).style.display = "none";
      document.getElementById("tick_"+menuArray[i]).innerHTML = "+";

function memStatus() {
  var num = readCookie("MN");
  if (num) {
    document.getElementById(num).style.display = "block";
    document.getElementById("tick_"+num).innerHTML = "-"; }
  var num1 = readCookie("SB");
  if (num1) {
    document.getElementById(num1).style.display = "block";
    document.getElementById("tick_"+num1).innerHTML = "-"; }

// Multiple onload function created by: Simon Willison
// [url]http://simonwillison.net/2004/May/26/addLoadEvent/[/url]
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {

addLoadEvent(function() {


Recommended Answers

All 3 Replies

Member Avatar

Okay, why do you have multiple accounts here?
On this thread you are posting as krishnareddi. On this other thread (http://www.daniweb.com/forums/thread142367.html) as sarithak -- why?

As I said in your other thread...
Okay, I did NOT test this, so who knows if it will actually work or what bugs, spelling, and syntax errors I have in it ... but this is the idea I would go with and you can check the errors to figure out what I did wrong.


// this is not a good approach for large database tables
$parents = array();
$children = array();

// I'm assuming `cname` is a UNIQUE value across all records ... 
// no duplicate values for `cname`
$result = mysql_query("SELECT * FROM `addcourse`") or die(mysql_error());
while ( $assoc = mysql_fetch_assoc( $result ) ) {
  $parents[$assoc['cname']] = $assoc
mysql_free_result( $result );

$result = mysql_query("SELECT * FROM `subcourse` ORDER BY `cname`") or die(mysql_error());
while ( $assoc = mysql_fetch_assoc( $result ) ) { 
  $children[] = $assoc

foreach ( $children as $child ) {
  $key = $child['cname'];
  if ( array_key_exists( $key, $parents ) ) {
    if ( !array_key_exists( 'children', $parents[$key] ) ) {
      $parents[$key]['children'] = array();
    $parents[$key]['childred'][] = $child;

print "<ul>\n";
foreach ( $parents as $key => $parent ) {
  print "  <li>$key</li>\n"; // cname value
  if ( is_array( $parent['children'] ) {
    print "  <ul>\n";
    foreach ( $parent['children'] as $child ) {
      print "    <li>{$child['subcname']}</li>\n";
    print "  </ul>\n";
print "</ul>\n";



hey langsor, thanx for the help...but the problem is i cud print the values from database but wat i want is that the child menu shud appear only wen i click on the parent menu...so for that reaason i used js file..but nw i cud display the sub menu wen i click on the parent menu for only one category or for one parent menu item. i hope i made my query clear. help appreciated. thanx:)

Member Avatar

If you only want to grab the values for the child parts of the tree when someone clicks on a parent item in the tree ... you have a couple options.

1. You can load all the parent and child values together into the page (like I've done above) and use JavaScript to show/hide the children when the parent is clicked on.
Pros: You can do this with only one page load.
Cons: It requires knowledge of JavaScript and requires most memory to store all database values.

2. You can submit a form when the user clicks on the parent and reload the page with that parent's child values associated with it.
Pros: You don't need any JavaScript
Cons: You must reload the page each time the user selects a parent-item

3. You can use Ajax to query the child elements and load them into the page, when the user clicks on a parent item.
Pros: Most elegant and fluid solution.
Cons: Requires greatest knowledge of JavaScript to implement.

What you can not do, is expect PHP to load the specific child values when the user clicks the parent item -- since the page is already loaded, PHP is done doing what it can do.

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.