View Single Post

   
  #2 (permalink)  
Old 02-28-2008, 09:19 AM
strawberry
 
Posts: n/a
Default Re: building indentation in hierarchical data


strawberry wrote:
> I'm trying to improve my ability to represent hierarchical data.
>
> Mike Hillyer's article
> (dev.mysql.com/tech-resources/articles/hierarchical-data.html) on
> hierarchical data describes a process of indenting nested set results
> based upon their depth within the data tree. The point here is that the
> indentation is constructed in MySQL.
>
> I was just wondering if anyone had managed something similar using the
> adjacency list model (ALM) and (excuse my ignorance here) a trigger,
> stored procedure or a bit of php - but again, building the indentation
> in MySQL.
>
> On a related point, Gijs Van Tulder's article
> (http://www.sitepoint.com/article/hie...-data-database) purports to
> build the indentation in php (using the ALM) but, unless I'm missing
> something, the list he builds does not seem to be indented. Am I
> missing something?
>
> Anyone get what I'm talking about? Any more info about either the php
> or mysql approach greatly appreciated.


Well, here's Gijs's solution modified to provide the indentation (I
think) he intended...

// $parent is the parent of the children we want to see
// $level is increased when we go deeper into the tree,
// used to display a nice indented tree
function display_children($parent, $level) {
// retrieve all children of $parent
$query = 'SELECT title FROM tree WHERE parent="'.$parent.'";';
//echo $query;
$result = mysql_query($query);

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

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

Reply With Quote