Page 1 of 1

Save results created by loop into a variable.

Posted: Wed Jan 07, 2009 10:12 am
by mcwdes
I found this sample code a couple of places. I'm having a hard time seeing where the results are applied to a variable. All I get is echoed/printed results. Is there a $var in this code that holds the values that I'm seeing printed after running in a browser?

Code: Select all

 
$agents = array();
If ($result = mysql_query("SELECT h.title, h.address, a.agentname 
                                FROM homes h, agents a WHERE
                                h.owner=a.id AND a.id=$aid"))
{       while ($array = mysql_fetch_array($result))
        {       $agents[$array["agentname"]][] = $array;
        }
}
 
foreach($agents as $agent=>$addresses)
{       echo("$agent<br>");
        foreach($addresses as $address)
        {       echo("{$address["title"]}, {$address["address"]}");
        }
}
 

Re: Save results created by loop into a variable.

Posted: Wed Jan 07, 2009 10:18 am
by papa
It fetches values from a database and stores it in an array.

Re: Save results created by loop into a variable.

Posted: Wed Jan 07, 2009 10:30 am
by mcwdes
Hey papa,
Yes... I understand that part... and can do that with much more simplified code. I found this code while looking for a way to have a simple variable, not an array, be equal to the results of a loop statement. I know its possible, just can't figure it out.

Re: Save results created by loop into a variable.

Posted: Wed Jan 07, 2009 11:49 am
by mcwdes
:offtopic: To all... the topic code was an attempt to store structured code including query results in a $var in order to fwrite to a file. Silly me, turns out I can loop the fwrite function. Sorry if I wasted anyones time with this.