Ajax to query before collapsable gets expanded

This seems like more of an Ajax question but I found this code
http://www.dynamicdrive.com/dynamici...view/index.htm

And I’m using it to query a database to sort of do some drill down analysis. In essence every time the expandable plus sign is clicked it displays data that meets all parents in a select/where query, if that makes sense.

I’ve found that this is cumbersome because I’m ultimately querying the database 4/5 times in nested while/do statements (in PHP) before the page can really load. My thought is that there’s got to be a way to display all collapsed top level information right away and then when the user clicks to expand a section it then runs the query. In theory it sounds good but my problem is I think I need to know javascript/ajax in order to do that.

I found this at wc3:

which seems to do what I’m looking for but I can’t figure out how to incorporate the function into this collapse/expand model. Any help is truly appreciated. Thanks in advance!

The link you provided doesn’t work.

Show us your code to better assist you. Doing 4/5 nested loops to query a database is most likely due to bad implementation.

Hmm, okay I don’t know why it put the …'s in there. The link is broken up below (only way I could get it to work)
http://www.dynamicdrive.com/
dynamicindex1/
treeview/
index.htm

my code so far is:

<?php
require("connect.php");
$connectmysql = mysql_connect($host,$user,$password) or die ($cantconnectmysqlmessage);
$db = mysql_select_db($database,$connectmysql) or die ($cantconnectdatabasemessage);
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Remarks</title>
<link href="../HighVisibility/dstyles.css" rel="stylesheet" type="text/css" />
 <link rel="stylesheet" href="../JQueryTree/jquery.treeview.css" />
 <script type="text/javascript" src="../JQueryTree/jquery.min.js"></script>
 <script src="../JQueryTree/lib/jquery.cookie.js" type="text/javascript"></script>
 <script src="../JQueryTree/jquery.treeview.js" type="text/javascript"></script>
 <script type="text/javascript" src="../JQueryTree/demo/demo.js"></script>
</head>
<body LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">
<div id='PageTop'>
  <div id='NavLogo'></div>
</div>
<div id='PageTopBottom1'></div>
<div id='PageTopBottom2'></div>
<div id='PageTopBottom3'></div>
<div style='position: absolute; top: 145px; left: 10px; font-family:Tahoma,Arial,Helvetica,Geneva,sans-serif; font-size:small;'>
<?php
 echo "<ul id='navigation'>\
";
$Disp1Query = "SELECT DISTINCT GROUPCD, (ROUND(((sum(fl)/sum(tl))*100),2)) as GSFILL, GRPDESC FROM tblpartpfil GROUP BY GROUPCD ORDER BY GSFILL ASC limit 3";
$Disp1Result = mysql_query($Disp1Query) or die(mysql_error().'<br />'.$Disp1Query);
while ($Row1 = mysql_fetch_assoc($Disp1Result))
{
 if($Row1['GROUPCD']==""){
  $GroupTitle = "<table border='0' bordercolor='#000000' cellpadding='0' cellspacing='0'><tr><td width='30'>00</td><td width='568'>BLANK</td><td width='55' align='right'>".$Row1['GSFILL']."&#37;</td></tr></table>\
";
 }else{
  $GroupTitle = "<table border='0' bordercolor='#000000' cellpadding='0' cellspacing='0'><tr><td width='30'>".$Row1['GROUPCD']."</td><td width='568'>".$Row1['GRPDESC']."</td><td width='55' align='right'>".$Row1['GSFILL']."%</td></tr></table>\
";
 }
    echo "<li>".$GroupTitle."\
";
    $Disp2Query = "SELECT DISTINCT NOUNCD, (ROUND(((sum(fl)/sum(tl))*100),2)) as NSFILL, NOUNDESC FROM tblpartpfil WHERE GROUPCD = '".$Row1['GROUPCD']."' GROUP BY NOUNCD ORDER BY NSFILL ASC";
 $Disp2Result = mysql_query($Disp2Query) or die(mysql_error().'<br />'.$Disp2Query);
 while ($Row2 = mysql_fetch_assoc($Disp2Result))
 {
  if($Row2['NOUNCD']==""){
   $NounTitle = "<table border='0' bordercolor='#000000' cellpadding='0' cellspacing='0'><tr><td width='40'>0000</td><td width='542'>BLANK</td><td width='55' align='right'>".$Row2['NSFILL']."%</td></tr></table>\
";
  }else{
   $NounTitle = "<table border='0' bordercolor='#000000' cellpadding='0' cellspacing='0'><tr><td width='40'>".$Row2['NOUNCD']."</td><td width='542'>".$Row2['NOUNDESC']."</td><td width='55' align='right'>".$Row2['NSFILL']."%</td></tr></table>\
";
  }
  echo "<ul style='margin:0px; padding:0px;'>\
";
     echo "<li>".$NounTitle."\
";
      $Disp3Query = "SELECT DISTINCT ADJCD, (ROUND(((sum(fl)/sum(tl))*100),2)) as ASFILL, ADJDESC FROM tblpartpfil WHERE GROUPCD = '".$Row1['GROUPCD']."' AND NOUNCD = '".$Row2['NOUNCD']."' GROUP BY ADJCD ORDER BY ASFILL ASC";
   $Disp3Result = mysql_query($Disp3Query) or die(mysql_error().'<br />'.$Disp3Query);
   while ($Row3 = mysql_fetch_assoc($Disp3Result))
   {
    if($Row3['ADJCD']==""){
     $AdjTitle = "<table border='0' bordercolor='#000000' cellpadding='0' cellspacing='0'><tr><td width='30'>0000</td><td width='536'>/BLANK</td><td width='55' align='right'>".$Row3['ASFILL']."%</td></tr></table>\
";
    }else{
     $AdjTitle = "<table border='0' bordercolor='#000000' cellpadding='0' cellspacing='0'><tr><td width='30'>".$Row3['ADJCD']."</td><td width='536'>".$Row3['ADJDESC']."</td><td width='55' align='right'>".$Row3['ASFILL']."%</td></tr></table>\
";
    }
    echo "<ul>\
";
       echo "<li>".$AdjTitle."\
";
     $Disp4Query = "SELECT DISTINCT a.NAV_PN, (ROUND(((sum(a.fl)/sum(a.tl))*100),2)) as NAV_PN_SFILL, b.DESCR FROM tblpartpfil a LEFT JOIN tblvisiblepsdbsumoneweekpfil b ON a.NAV_PN=b.NAV_PN WHERE a.GROUPCD = '".$Row1['GROUPCD']."' AND a.NOUNCD = '".$Row2['NOUNCD']."' AND a.ADJCD = '".$Row3['ADJCD']."' GROUP BY a.NAV_PN ORDER BY NAV_PN_SFILL ASC";
     $Disp4Result = mysql_query($Disp4Query) or die(mysql_error().'<br />'.$Disp4Query);
     while ($Row4 = mysql_fetch_assoc($Disp4Result))
     {
      $NAV_PN_Info = "<table border='0' bordercolor='#000000' cellpadding='0' cellspacing='0'><tr><td width='100'>".$Row4['NAV_PN']."</td><td width='450'>".$Row4['DESCR']."</td><td width='55' align='right'>".$Row4['NAV_PN_SFILL']."%</td></tr></table>\
";
      echo "<ul>\
";
         echo "<li>".$NAV_PN_Info."</li>\
";
         echo "</ul>\
";
     }    echo "</li>\
";
       echo "</ul>\
";
   }
     echo "</li>\
";
     echo "</ul>\
";
 }
    echo "</li>\
";
}
echo "</ul>\
";
?>
</div>
</body>
</html>