putting the results of a function 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
alecks
Forum Newbie
Posts: 5
Joined: Fri Nov 03, 2006 4:49 pm

putting the results of a function into a variable

Post by alecks »

I want to put the outputs of an include function that is in a basic "fetch while there are still rows" while loop into a variable.

ex.

Code: Select all

while ($result = mysql_fetch_array($query)){
		include("post.inc");	
	}
and the contents of post.inc are

Code: Select all

<h1><?php print $result['row_that_was_fetched']; ?></h1>
And say that there were three rows, so the product of this while loop would be

Code: Select all

<h1><?php print $result['row_that_was_fetched']; ?></h1><h1><?php print $result['row_that_was_fetched']; ?></h1><h1><?php print $result['row_that_was_fetched']; ?></h1>
I want to put all of those into a variable, so that

Code: Select all

$variable = <h1><?php print $result['row_that_was_fetched']; ?></h1><h1><?php print $result['row_that_was_fetched']; ?></h1><h1><?php print $result['row_that_was_fetched']; ?></h1>
Is it possible to do this?

I'm kinda thinking this:

Code: Select all

while ($result = mysql_fetch_array($query)){
		$variable .= include("post.inc");	
	}
But I don't think it will work because PHP will interpret it as me wanting to define an anonymous function in $variable. Am I correct or am I just crazy?

Any help you can give is very appreciated ^_^

-alecks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<?php  // post.inc
return '<h1>' . $result['row_that_was_fetched']. '</h1>';
?>
hempheastus
Forum Newbie
Posts: 3
Joined: Wed Feb 28, 2007 7:15 pm

Not quite sure what you want,but try this

Post by hempheastus »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am not sure exactly what you are trying to do.  Are you trying to put the contents of the "mysql_fetch_array" into an array, or are you trying to put the contents of "post.inc"?

anyways what I think you are trying to do is place the fetch data into a string surronded by <h1> tags, so that is what I am going to show you how to do.

Code: Select all

$variable = "";
while ($result = mysql_fetch_array($query))
{ 
         $variable .= "<H1>".$result["row_that_was_fetched"]."</H1>";
}
Now, the only reason I could possibly think you would want to read in the value

Code: Select all

"<H1>".$result["row_that_was_fetched"]."</H1>"
is for easy editing later. So you will need to use fopen in order to get that data if that is where you want to store it. So try this:

First off make the contents of 'post.inc' equal to:

Code: Select all

"<H1>".$result["row_that_was_fetched"]."</H1>";
1) You are inside of a php tag, you can not have html tags just hanging around.
2) You can NOT have those php tags in there since you are already in a php tag. If those have to stay, you will have to do some string manip and parse them out of $fileContent after the file is read in.
3) Second get rid of that print command, that is going to put the output to the screen, but you want the output in a variable. Again if that is required you are going to need to parse that out.

Now using our new post.inc try the code below.

Code: Select all

$inFile = fopen('post.inc', 'r');
$fileContent = fread($inFile, filesize('post.inc'));
fclose($inFile);

//put parser code here if needed.

$variable = "";
while ($result = mysql_fetch_array($query))
{ 
         $variable .= eval("return ".$fileContent);
}
The code above will evaluate the file, and return the parameters as a string.

You are lucky I am feeling generous so I will show you the code to convert A) into B)
A)

Code: Select all

<h1><?php print $result['row_that_was_fetched']; ?></h1>
B)

Code: Select all

"<h1>".$result["row_that_was_fetched"]."</h1>";

Code: Select all

$fileContent = str_replace("print ","",$fileContent);
$fileContent = str_replace("<?php ","",$fileContent);
$fileContent = str_replace(" ?>","",$fileContent);
$fileContent = str_replace(";","",$fileContent);
$fileContent = str_replace('<','"<',$fileContent);
$fileContent = str_replace('>','>"',$fileContent);
$fileContent .= ";";
So put it all together and you will have, starting with the 'post.inc' that you submitted:

Code: Select all

$inFile = fopen('post.inc', 'r');
$fileContent = fread($inFile, filesize('post.inc'));
fclose($inFile);

$fileContent = str_replace("print ","",$fileContent);
$fileContent = str_replace("<?php ","",$fileContent);
$fileContent = str_replace(" ?>","",$fileContent);
$fileContent = str_replace(";","",$fileContent);
$fileContent = str_replace('<','"<',$fileContent);
$fileContent = str_replace('>','>"',$fileContent);
$fileContent .= ";";

$variable = "";
while ($result = mysql_fetch_array($query))
{ 
         $variable .= eval("return ".$fileContent);
}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Red flag on eval()!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Yeah, eval() is a little risky. Why not use the output buffer?

Code: Select all

ob_start();
while ($result = mysql_fetch_array($query)){
    include("post.inc");
}
$variable = ob_get_clean();
Post Reply