maxdean10 Posted February 13, 2011 Share Posted February 13, 2011 I am trying to do the following. Except I know that 'return' is not the right method to use, as it stops the script, so what ends up happening is only one row is returned, instead of the three that are there. With return, the data is being passed without being immediately printed, and I end up with the data (but not all of it, because the script stops) in correct place in the page. If I replace return () with echo(), it works fine, in terms of returning the correct data. However, with the way things are setup, if I use echo, the results print at the head of my page. I am using function CreateSideMenu to establish the values for content, and then another function, later on the index.php page, actually creates the page. So what I need is to have something, similar to return (), that passes the information on, but does not immediately print it. Do I make sense? see code below: function CreateSideMenu () { // open CreateSideMenu function include ('/Users/max/Sites/rdbase-llc/hidden/defin/kinnect01.php'); $query = "SELECT content_element_title, content_element_short_text FROM content_main"; $result = mysql_query ($query, $dbc); while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) { return ("<p>" . $row[content_element_title] . "</h2>\n<p>" . $row[content_element_short_text] . "</p>"); } Thanks ahead of time. Link to comment https://forums.phpfreaks.com/topic/227576-alternatives-to-return-method-so-i-dont-print-result/ Share on other sites More sharing options...
kenrbnsn Posted February 13, 2011 Share Posted February 13, 2011 You need to collect all the responses and then return the result: <?php function CreateSideMenu () { // open CreateSideMenu function include ('/Users/max/Sites/rdbase-llc/hidden/defin/kinnect01.php'); $query = "SELECT content_element_title, content_element_short_text FROM content_main"; $result = mysql_query ($query, $dbc); $tmp = array(); while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) { $tmp[] = "<p>{$row['content_element_title']}</h2>\n<p>{$row['content_element_short_text']}</p>"; } return (implode("\n",$tmp) . "\n"); } ?> Ken Link to comment https://forums.phpfreaks.com/topic/227576-alternatives-to-return-method-so-i-dont-print-result/#findComment-1173831 Share on other sites More sharing options...
maxdean10 Posted February 13, 2011 Author Share Posted February 13, 2011 Thank you kenrbnsn. I am so rusty with php that I had completely forgotten about several elements of your solution. It works great now! Link to comment https://forums.phpfreaks.com/topic/227576-alternatives-to-return-method-so-i-dont-print-result/#findComment-1173836 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.