Jump to content

[SOLVED] Recursion: How to display data hierarchically from a table


sbrinley

Recommended Posts

About a week ago I posted something similar but I don't think I asked the question correctly. I've come very close to a solution but I need a little help to get the rest of the way (or maybe a different set of eyes)

 

This is what I have so far:

 

A table that looks like this:

 

Parent		      Child
                  A00030002AXA
A00030002AXA		R50210802AXA
A00030002AXA		R50231502AXA
A00030002AXA		R50241502AXA
R50210802AXA		R50210002AXA
R50231502AXA		R50230002AXA
R50241502AXA		R50240002AXA

This is the PHP script I'm using. Which has been modified by search through various other solutions and take in part from a tutorial from another website.

 

function display_children($parent, $level) { 
   // retrieve all children of $parent 
   $result = mysql_query('SELECT child FROM bom '. 
                          'WHERE parent="'.$parent.'";'); 

   // display each child 
   while ($row = mysql_fetch_array($result)) { 
       // indent and display the title of this child 
       echo str_repeat('  ',$level).$row['child']."<br>"; 

       // call this function again to display this 
       // child's children 
       display_children($row['child'], $level+1); 
   } 
} 

display_children('',0);

 

What is displayed is this

 

A00320002AXA
R50210002AXA
R50210002AXX
R50230302AXA
R50230002AXA
R50230002AXX
R50240302AXA
R50240002AXA
R50240002AXX

 

I would like to arrange the data so it looks like this

 

A00320002AXA
   R50210002AXA
     R50210002AXX
   R50230302AXA
     R50230002AXA
       R50230002AXX
   R50240302AXA
     R50240002AXA

 

Does anyone have any suggestions or can you point me in the right direction? Thanks !!!

Link to comment
Share on other sites

It doesn't matter how many spaces you string together. If you're trying to display it in a web browser, then it's the web browser that displays 10,20,100,1000 spaces as a single space. Only if you do a view source will you see multiple spaces.

 

Try using a   for your indent instead of a space.

Link to comment
Share on other sites

Thanks for the replies. I think more in lines of what I meant about the spacing is to create <ul> items. I put in the spaces as just an example.

 

Here is what I'm talking about:

 

<ul>A00320002AXA
   <li><ul>R50210002AXA</li>
     <li>R50210002AXX</li></ul>
   <li><ul>R50230302AXA</li>
     <li><ul>R50230002AXA</li>
       <li>R50230002AXX</li></ul></ul>
   <li><ul>R50240302AXA</li>
     <li>R50240002AXA</li></ul>
</ul>

 

I'm wondering how to insert the unordered list item tags into the loop.

Link to comment
Share on other sites

Thanks for the replies. I think more in lines of what I meant about the spacing is to create <ul> items. I put in the spaces as just an example.

 

Here is what I'm talking about:

 

<ul>A00320002AXA
   <li><ul>R50210002AXA</li>
     <li>R50210002AXX</li></ul>
   <li><ul>R50230302AXA</li>
     <li><ul>R50230002AXA</li>
       <li>R50230002AXX</li></ul></ul>
   <li><ul>R50240302AXA</li>
     <li>R50240002AXA</li></ul>
</ul>

 

I'm wondering how to insert the unordered list item tags into the loop.

 

Alot of overlapping tags in that html there.

Why not just use  

Link to comment
Share on other sites

Hi

 

Probably something like this:-

 

<?php
function display_children($parent) { 
// retrieve all children of $parent 
$result = mysql_query('SELECT child FROM bom WHERE parent="'.$parent.'";'); 

// display each child 
$FirstTime = true;
while ($row = mysql_fetch_array($result)) 
{ 
if ($FirstTime)
{
	echo "<ul>";
	$FirstTime = false;
}
// indent and display the title of this child 
echo "<li>".$row['child']."</li>"; 

// call this function again to display this 
// child's children 
display_children($row['child']); 
} 
if (!$FirstTime) echo "</ul>";
} 

display_children('');
?>

Link to comment
Share on other sites

Hi

 

Probably something like this:-

 

<?php
function display_children($parent) { 
// retrieve all children of $parent 
$result = mysql_query('SELECT child FROM bom WHERE parent="'.$parent.'";'); 

// display each child 
$FirstTime = true;
while ($row = mysql_fetch_array($result)) 
{ 
if ($FirstTime)
{
	echo "<ul>";
	$FirstTime = false;
}
// indent and display the title of this child 
echo "<li>".$row['child']."</li>"; 

// call this function again to display this 
// child's children 
display_children($row['child']); 
} 
if (!$FirstTime) echo "</ul>";
} 

display_children('');
?>

 

 

You are awesome that's exactly what I'm looking for!

 

Also thanks roopurt18 that is very helpful as well. i'll do some checking into that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.