Forum Moderators: coopster

Message Too Old, No Replies

echo vs return inside functions help :)

echo, return, functions

         

rodriguez1804

11:01 pm on Aug 17, 2009 (gmt 0)

10+ Year Member



Hey all - again,

I am having problems using the echo statement inside a function. Here are the details:

I have the following function under a file named "functions.php":


function retrieveComments() //function to retrieve user comments
{
$i = 0;
$value ='';
$page = (is_numeric($_GET['page'])) ? $_GET['page'] : 1; //used for pagination
$limit = 15;
$from = ($limit * $page) - $limit;

$dbConn = new dbConnection();

//connect
$dbConn->connect();

$sql="SELECT username,comments,timeStamp,id FROM comments ORDER BY id DESC LIMIT $from, $limit";
$result=mysql_query($sql) or die(mysql_error());

//calculate total pages
$total_entries = mysql_num_rows(mysql_query("SELECT * FROM comments"));
$total_pages = ceil($total_entries / $limit);

//temporarily put data into array and keep printing it out until there is no more
while($info=mysql_fetch_array($result))
{
$comment = $info['comments'];
$username = $info['username'];
$timeStamp = $info['timeStamp'];
$postID = $info['id'];
$lettermyusername = substr($username, 0,1);
$replies = $dbConn->userReplyCount($postID);
$counter = $i;

$myProfilePic2 = "<img src='/users/".$lettermyusername."/".$username."/".$username.".ProfilePic.".$dbConn->getUserPicExtension($username)."' height=100 width=100 alt='User picture'/>";

$this->value .=' //put everything inside "value" variable
<table id="userPost">
<tr><td id="activeUserPic">'.$myProfilePic2.'</td><td>Space</td><td valign="top" width=500px colspan=2><a href="commentPageTemplate.php?username='.$username.'&postID='.$postID.'&userPic='.$myProfilePic2.'&counter='.$counter.'&comment='.$comment.'&time='.$timeStamp.'&lettermyusername='.$lettermyusername.'">'.$comment.'</a></td></tr>
</table>
<table id="userPostDetails">
<tr><td valign="top" width="100px" class="smallTxt">Author: <em>'.$username.'</em></td><td align="left" valign="top" class="smallTxt">Date Posted:
'.$timeStamp.'</td><td
width=60px align="center" valign="top" class="smallTxt">Views: '.$dbConn->getCounter($postID).'</td><td valign="top" class="smallTxt" align="center">Replies: '.$replies.'</td>
</td></tr>
</table><div class="spacerV11"></div>';

}

//show pages and links
echo "<div align='center'>";
for ($i = 1; $i <= $total_pages; $i++){
$output = ($page == $i) ? $i : '<a href="?page='.$i.'">'.$i.'</a>';

echo $output." "; ***THE PAGE NUMBERS FROM HERE SHOW UP LIKE THEY ARE SUPPOSED TO, BUT AT VERY TOP OF PAGE! I WANT THEM TO SHOW UP AT BOTTOM OF ALL COMMENTS?
}

return $this->value;
}

Then I use the function like this in "index.html":
[code]
include_once ("generatePage.php");
include_once ("dbFunctions.php");

$webPage = new generateHTML();
$dbConn = new dbConnection();

$webPage->addHeader('title', 'PageTitle');
$webPage->addLeftColumn('');

$webPage->addContent('
'.$dbConn->retrieveComments().' *****THIS CALLS ON THE FUNCTION DEFINED ABOVE, ALL SHOWS WELL, EXCEPT FOR THE PAGE NUMBERS, WHICH SHOW AT TOP MOST PART OF PAGE, OUTSIDE OF ALL ELEMENTS?*********
');

$webPage->addFooter(date('Y'), 'SiteSolutions Inc.');
echo $webPage->getPage(); //displays the whole page to browser ?>

What should I do so that the page numbers are displayed inside the content area of the page instead of outside. It just makes it look horrible! If you have any example on how to do this, that would be spamtastic!

eelixduppy

3:24 am on Aug 19, 2009 (gmt 0)



What does the addContent method do? It seems like the echo from the page number is going to be echo'ed before the function returns and the content is added. You should be able to use output buffering here to do what you want, but this is a really hacky attempt at making this work. My personal opinion is to never echo things out in a function. Period. It's really best to keep the content and the processing separate if at all possible. So for instance, if you need to return more than one thing -- which looks like the case here -- instead of returning one and echoing out the other, return them both in some sort of structure (an array would work) and then process them:

return array('content' => $this->value, 'page' => $output);

Of course I wouldn't format those values beforehand with the HTML, but afterward, once they are returned.

See what you can come up with.

rodriguez1804

4:08 am on Aug 19, 2009 (gmt 0)

10+ Year Member



You are just awesome eelixduppy! I never thought about putting the contents into an array. I like this idea and will try to implement it. lol, I ll be back if I happen to have any further questions.

By the way, the addContent method simply generates the html layout and takes in some data to be displayed in the middle of the page, in this case, the comments. Thanks again!