This is code snippet for php recursive function that works with mysql and suckerfish, for instance if we have database with table named menu
========================================= id | parent_id | name | link ========================================= 1 | 0 | home | index.php 2 | 0 | about us | about_us.php 3 | 2 | carreer | career.php 4 | 2 | jobs | jobs.php ==========================================
and we want to use jquery suckerfish menu to display it.
The sql query and display the menu
$query = "SELECT id, name, parent_id, link FROM `menu`";
$result = mysql_query($query) or die( $query);
while ($row = mysql_fetch_object($result)) {
$data[$row->parent_id][] = $row;
}
echo get_menu($site_url,$data);
The php recursive function
function get_menu( &$site_url, $data, $parent = 0) {
$i = 1;
$html = '';
if (@$data[$parent]) {
if($parent > 0)$html = " - ";
$i++;
foreach ($data[$parent] as $v) {
$child = get_menu( $site_url, $data, $v->id);
$html .= "
- "; if($parent > 0) $html .= ''.$v->name.''; else $html .= ''.$v->name.''; if ($child) { $i--; $html .= $child; } $html .= ' '; } if($parent > 0)$html .=''; $html .= "