Ganska standard låter det som, börja med att hämta alla med cat_father_id=0 sedan för varje träff hämtar du alla med cat_father_id = cat_id i resultaten.
Kanske inte så snygg kod, men den här har jag på ett par sidor.
Kod:
echo getCategories(0); #skriv ut menyn
function getCategories($parent_id) {
$result = "";
$sqlStr = "SELECT description, cat_id FROM menu WHERE cat_father_id = '$parent_id' ORDER BY priority, description ASC;";
$items_query = mysql_query($sqlStr);
*while($itemInfo = mysql_fetch_row ($items_query)) {
* *$query = "SELECT cat_id FROM menu WHERE cat_father_id = '".$itemInfo[1]."' LIMIT 1;";
* *$query_result_handle = mysql_query ($query) or print "Could not make query $query";
* *$has_me_as_parent[$parentId] = mysql_num_rows ($query_result_handle);
* *
* *if ($parentId == 0) { $result .= "\t\t\t<ul>\n"; }
* *$result .= "\t\t\t\t<li>";
* *$result .= "<a href=\"/blabla/".$itemInfo[1]."\">$itemInfo[0]</a>";
* *if ($has_me_as_parent[$parentId] == 1) { $result .= "\t\t\t\t\t<ul>\n"; }
* *if ($has_me_as_parent[$parentId] == 1) { $result .= getCategories($itemInfo[1]); }
* *if ($has_me_as_parent[$parentId] == 1) { $result .= "</ul>"; }
* *
* *$result .= "\t\t\t\t</li>\n";
* *if ($parentId == 0) { $result .= "\t\t\t</ul>\n"; }
*}
return $result;
}