[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



On Sun, 21 Dec 2003, Jan Andersen wrote:

> 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);
> }
>  
> 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.

Jeg ville prøve at lægge mine data på en måde så de er nemmere at 
pille ud til sidst. Som jeg forstår dig, så vil det statiske array se 
sådan ud:


$ramdb = array(
	"0" => array(
			array([record]),
			array([record]),
			array([record])
		),
	"1" => ...
);

Nøglen i ovenstående er så "far", fx:
$far = 0;
$farsborn = $ramdb[$far];
echo "Antal børn: ".count($ramdb[$far]);
echo "Antal fædre: ".count($ramdb);

Opbygning:
while ($number--) {
  $list = mysql_fetch_array($result, MYSQL_ASSOC);
  $far = $list[far];
  $ramdb[$far][] = $list;
}

Noget lignende ovenstående skulle kunne bruges.

/hans
-- 
Hamletsgade 4 - 201, DK-2200 København N, Phone: +45 3582 9079
Schou Industries ApS      http://schou.dk/    CVR: 26 13 44 39
--------------------------------------------------------------
I spent probably 20 or 30 hours drawing the penguin stuff, and
I've spent four or five years writing code, and I'm still
known for the penguin a lot more than I am for any of the code.
			-- Larry Ewing, creator of Tux


 
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] *