return variable from function
Posted: Sun Dec 13, 2009 11:32 am
I have a function that echos off the messages a person has in the database like this:
in another file I have all the html and I include messages.php and call the function messages like this:
what I want to do is take actions depending on the number of messages from the inbox like this
but I can't seem to access the $totalmessages variable from here. I know I can do it without using a function, but I'm calling the function from more that one place, so I wouldn't have to have the same script in multiple places in case I change my database. Any help would be appreciated. Thanks.
Code: Select all
// messages.php
function messages($user) {
$sql = "SELECT* FROM messages WHERE user='$user'";
$result = mysql_query($sql) or die(mysql_error());
$i = 0;
while ($data = mysql_fetch_array($result, MYSQL_ASSOC)) {
// a bunch of stuff happens
$i = $i + 1;
}
$totalmessages = $i;
}
Code: Select all
// inbox.php
// a bunch of stuff
include('messages.php');
echo '<div id="messages">';
messages($_COOKIE['username']);
echo '</div>';
Code: Select all
// inbox.php
// a bunch of stuff
include('messages.php');
echo '<div id="messages">';
messages($_COOKIE['username']);
echo '</div>';
if ($totalmessages > 10) {
// do something
}