Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8821999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:59:26+00:00 2026-06-14T05:59:26+00:00

I was trying to show tree data with JQuery expand and collapse . For

  • 0

I was trying to show tree data with JQuery expand and collapse.

For example my database 
Parent A has AA, AB, AC children
Parent AA has AAA, AAB, AAC
Parent AC has ACA, ACB, ACD
etc.

So I am using Recursion to show all the data from the database.

But I also need to declare the JQuery, so I am using the same Recursion code to generate the script

What I want to ask is, is there any way to use the Recursion one time to generate both things?, the data show inside the <body> tag, and JQuery declare at <head> tag.

Because I feel it will waste the memory to do same thing 2 times.

This is my <body> code

   <?   
      $temp = "";                
      $parent = "";
      $level = 0;
      loadFolders($parent, $level, $temp);

      function loadFolders($parent, $level, $temp){
        $getLevelSql = mysql_query("select * from folders where parent='$parent' and level='$level'");
        $level++;
        while ($getLevelSqlRow = mysql_fetch_array($getLevelSql)){
          $parentID = $getLevelSqlRow['id'];
          $child = $getLevelSqlRow['child'];
          $checkChild = mysql_query("select * from folders where parent='$parentID' and level='$level' ");
          if (mysql_num_rows($checkChild)){
          ?>
            <div class="level1 redFont" id="levelID<?echo $getLevelSqlRow['id'];?>">
              <?echo "$child";?>
            </div>  
            <div class="level2" id="childID<?echo $getLevelSqlRow['id'];?>">
          <?
            loadFolders($parentID, $level, $temp);
          ?>
            </div>
          <?  
          } else {
          ?>
            <div><?echo "$child";?></div>  
          <?
          }
        }
      }
    ?>

This is my <head> code

<script> 
    $(document).ready(function(){

    <?
      include "config.php";
      $temp = "";                
      $parent = "";
      $level = 0;
      loadFolder($parent, $level, $temp);

      function loadFolder($parent, $level, $temp){
        $getLevelSql = mysql_query("select * from folders where parent='$parent' and level='$level'");
        $level++;
        while ($getLevelSqlRow = mysql_fetch_array($getLevelSql)){
          $parentID = $getLevelSqlRow['id'];
          $child = $getLevelSqlRow['child'];
          $checkChild = mysql_query("select * from folders where parent='$parentID' and level='$level' ");
          if (mysql_num_rows($checkChild)){
          ?>
            $("#levelID<?echo $getLevelSqlRow['id'];?>").click(function(){
            $("#childID<?echo $getLevelSqlRow['id'];?>").slideToggle("slow");
          });
          <?
            loadFolder($parentID, $level, $temp);
          ?>
          <?  
          }
        }
      }
    ?>

    });
  </script>

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:59:27+00:00Added an answer on June 14, 2026 at 5:59 am

    I haven’t tested this as I haven’t got data to do it but basically you really just need to use selectors in your jquery code to find the sub section from any of the click point.

    I would nest the child element to make it easier. Something like this:

       <?   
      $temp = "";                
      $parent = "";
      $level = 0;
      loadFolders($parent, $level, $temp);
    
      function loadFolders($parent, $level, $temp){
        $getLevelSql = mysql_query("select * from folders where parent='$parent' and level='$level'");
        $level++;
        while ($getLevelSqlRow = mysql_fetch_array($getLevelSql)){
          $parentID = $getLevelSqlRow['id'];
          $child = $getLevelSqlRow['child'];
          $checkChild = mysql_query("select * from folders where parent='$parentID' and level='$level' ");
          if (mysql_num_rows($checkChild)){
            // ** notice I've added item class and nested the children inside
          ?>
            <div class="level1 redFont item" id="levelID<?echo $getLevelSqlRow['id'];?>">
              <?echo "$child";?>
              <div class="children" id="childID<?echo $getLevelSqlRow['id'];?>">
            </div>  
    
          <?
            loadFolders($parentID, $level, $temp);
          ?>
            </div>
          <?  
          } else {
          ?>
            <div><?echo "$child";?></div>  
          <?
          }
        }
      }
    ?>
    

    Then your jquery becomes something like this:

    $(document).ready(function(){
    
        $('div.item').click(function(){
            $(this).find('>div.children').slideToggle("slow");
        });
    
    });
    

    No need to loop twice and generate a hundred handlers..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get QTreeView (using an underlying QFileSystemModel) to show a directory tree.
I am trying to create a Jquery Tree plugin for my current project. In
I'm trying to define the Show function for the polymorphic Tree type. Could anyone
Im trying to show a jQuery menu that is built from and ajax call
I am trying to figure out children processes of a given parent from ps
I have some hierarchical data I am manipulating with JQuery, and I am trying
Trying to show a label only when a certain item in a combo is
I'm trying to show/hide( or simply put - filter ) my markers( of houses
I'm trying to show a UIAlertView before actually deleting a cell from a UITableView
I'm trying to show a div when a user clicks in an input field,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.