[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] Rekursion i php



Jan Andersen wrote:
Hej
Jeg er lige begyndt at programmere php, og vil høre om der er nogen
hajer der lige ved hvordan man laver en rekursiv metode i php baseret på
arrays. (det er mit kendskab til arrays i php der kommer lidt til kort
her).
Problemet er at jeg skal hente nogle data fra mysql og udskrive disse
rekursivt. Derfor har jeg gemt dem i et array, for at spare requests til
databasen.
Ex. $array = array();
for ($i=0; $i<$number; $i++) {
$id = mysql_result($result,$i,"id");
$link = mysql_result($result,$i,"link");
$titel = mysql_result($result,$i,"titel");
$tekst = mysql_result($result,$i,"tekst");
$far = mysql_result($result,$i,"far");
$array[$i] = array($id,$titel,$tekst,$link,$far);
}

Dette ville jeg gøre lidt anderledes.


$array=array();
while ($row=mysql_fetch_assoc($result)){
  $array[$row['far']][]=$row;
}

Nu har du et todimensionelt array, som er ordnet efter far-værdien.

Nu vil jeg så gerne have ”selected” de rækker i arrayet der har far = 0
og derefter hente dem der har denne rækkes id som far, etc.

function hent_unger($far){ global $array; if (!isset($array[$far]){ return; } else { echo "<ul>\n"; foreach ($array[$far] as $punkt){ echo "<li>{$punkt['titel']}</li>\n"; hent_unger($punkt['far']); } echo "</ul>"; } }


Dunne funktion kaldes bare med hent_unger(0); og så skulle du få noget i stil med det, jeg tror, du er ude efter, nemlig en nested liste af dine punkter.


Med venlig hilsen

Ole



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:43 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *