alecks Posted March 1, 2007 Share Posted March 1, 2007 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. while ($result = mysql_fetch_array($query)){ include("post.inc"); } and the contents of post.inc are <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 <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 $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: 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 Quote Link to comment https://forums.phpfreaks.com/topic/40627-putting-results-of-a-function-into-a-variable/ Share on other sites More sharing options...
MadTechie Posted March 1, 2007 Share Posted March 1, 2007 Personally i could have a function ie function displaypost($foo) { echo "<h1>"; echo $foo; echo "</h1>"; } and use while ($result = mysql_fetch_array($query)) { displaypost($result['row_that_was_fetched']) } Quote Link to comment https://forums.phpfreaks.com/topic/40627-putting-results-of-a-function-into-a-variable/#findComment-196514 Share on other sites More sharing options...
hukadeeze Posted March 1, 2007 Share Posted March 1, 2007 I'm thinking: //The Call $loopresults = functionname($query); And the function: //The Function function functionname($query) { return while ($result = mysql_fetch_array($query)) { include("post.inc"); } } But I'm not sure if you can return the results of a loop in this fashion. If it does work, then the results will be stored in the variable $loopresults. Quote Link to comment https://forums.phpfreaks.com/topic/40627-putting-results-of-a-function-into-a-variable/#findComment-196576 Share on other sites More sharing options...
btherl Posted March 1, 2007 Share Posted March 1, 2007 Try this: ob_start(); while ($result = mysql_fetch_array($query)){ include("post.inc"); } $string = ob_get_flush(); ob_start() will start buffering output, and ob_get_flush() will grab the buffered output and put it in a variable. Quote Link to comment https://forums.phpfreaks.com/topic/40627-putting-results-of-a-function-into-a-variable/#findComment-196631 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.