Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Simple recursive function to print out database tree structure

Simple function for database model ID, name, parent, where ID is unique identifier, name - any needed attributes etc, parent - unique identifier of parent where this element is attached to. I am just using my mysql class, you will have to modify it to yours

<?php
function print_menu($id = 0) 
{
    // get all records from database whose parent is $id
    $result = $query->select_result("*", "table", "where parent = '".$id."'");
    //check if there are any results
    if($result != 0)
    {
        echo "<ul> n";
        while($row = $query->fetch($result))
        {
            //print result and call function to check if it has children
            echo "<li>".$row['name']."</li> n";
            print_menu($row['ID']);
        }
        echo "</ul> n";
    }
}
print_menu();
?>

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus