Save results created by loop into a variable.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcwdes
Forum Newbie
Posts: 3
Joined: Wed Jan 07, 2009 10:00 am

Save results created by loop into a variable.

Post 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"]}");
        }
}
 
Last edited by mcwdes on Wed Jan 07, 2009 11:41 am, edited 2 times in total.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Save results created by loop into a variable.

Post by papa »

It fetches values from a database and stores it in an array.
mcwdes
Forum Newbie
Posts: 3
Joined: Wed Jan 07, 2009 10:00 am

Re: Save results created by loop into a variable.

Post 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.
mcwdes
Forum Newbie
Posts: 3
Joined: Wed Jan 07, 2009 10:00 am

Re: Save results created by loop into a variable.

Post 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.
Post Reply