Page 1 of 1

putting the results of a function into a variable

Posted: Wed Feb 28, 2007 7:08 pm
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

Posted: Wed Feb 28, 2007 7:39 pm
by volka
try

Code: Select all

<?php  // post.inc
return '<h1>' . $result['row_that_was_fetched']. '</h1>';
?>

Not quite sure what you want,but try this

Posted: Wed Feb 28, 2007 8:22 pm
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]

Posted: Wed Feb 28, 2007 10:20 pm
by feyd
Red flag on eval()!

Posted: Thu Mar 01, 2007 2:50 am
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();