Visa ett inlägg
Oläst 2008-08-25, 17:40 #2
najks avatar
najk najk är inte uppkopplad
Har WN som tidsfördriv
 
Reg.datum: Oct 2005
Inlägg: 1 224
najk najk är inte uppkopplad
Har WN som tidsfördriv
najks avatar
 
Reg.datum: Oct 2005
Inlägg: 1 224
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;
}
najk är inte uppkopplad   Svara med citatSvara med citat