Easy function

I have been trying to read about functions but since I’m a beginner it’s still hard for me to grasp. So I tried writing one on my own but when testing it I only get errors.

I have a query going that selects number of rows from database. The following function tells user he has too many messages once he has over 2 in his inbox. But when I try to place maxMessages(); in a different part of my html document within php brackets it gives me an error (“Call to undefined function maxmessages()”)
Or when I raise the number of allowed rows to 5 for example I get another error.
I also tried it the other way around by putting the if statement within the function maxMessages(), but that didn’t work either.

Any help appriciated.

 
if($num_messages>=2)
  {
     function maxMessages()
     {
       echo "you have too many messages in your inbox";
     }
  }
maxMessages();

Try this:-


<?php
function get_number_of_messages($user){
  $messages = sprintf(
    "SELECT id FROM mailbox WHERE to_user='%s' ORDER BY date DESC LIMIT 50",
    mysql_real_escape_string($user)
  );
  return mysql_num_rows($messages);
}
?>
<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <div>
      <?php if(get_number_of_messages($_SESSION['username']) >= 2): ?>
        <p>Too many messages</p>
      <?php endif; ?>
    </div>
    <div>
      <?php if(get_number_of_messages($_SESSION['username']) >= 2): ?>
        <p>Too many messages</p>
      <?php endif; ?>
    </div>
  </body>
</html>

The maxMessages function only gets defined if the variable $num_messages is 2 or more, but you call the function regardless later on in your code.

If $num_messages is 2 or more, you won’t get this error.

Try:-


<?php
function display_max_messages(){
  echo 'Too many messages';
}

if($num_messages >= 2){
  display_max_messages();
}

Thanks Anthony I understood my mistake. But the whole reason I was trying to use functions is so I won’t have to rewrite the whole mysql query again. The thing is I want to echo “Too many messages” in another part of the html document, which I have to open a new set of php tags for.
So now I have this function I learned from you but the echoing part cannot be moved without typing the whole query again… Am I right?

You might want to take a look at this thread, where the concept of functions has been discussed lately: http://www.sitepoint.com/forums/php-34/functions-slightly-confused-how-some-use-them-745230.html

I’m not really sure I follow, but lets look at this:-


<?php
$num_messages = 2;

function display_max_messages_error($num){
  if($num >= 2){
    echo 'Too many messages';
  }
}
?>
<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <div>
      <p>
        <?php display_max_messages_error($num_messages); ?>
      </p>
    </div>
    <div>
      <p>
        <?php display_max_messages_error($num_messages); ?>
      </p>
    </div>
  </body>
</html>

You see, the display_max_messages_error function will only display the error is $num_messages is more than, or equal to two.

Is that what you’re looking for?

I should have provided more information.


$to_user = $_SESSION['username'];

// get the messages from the table.
$get_messages = mysql_query("SELECT id FROM mailbox WHERE to_user='$to_user' ORDER BY date DESC LIMIT 50") or die(mysql_error());
$num_messages = mysql_num_rows($get_messages);

if($num_messages>=2)
echo"Too many messages";

But I want to echo this out above the php code within the html body:


<h1>Your private messages</h1>
<br />
<?php
HERE IS WHERE I WANT TO ECHO THE WARNING
?>
<form name="myform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<table width="97%" border="0" align="center" cellpadding="4">
          <tr>
            <td width="3%" align="right" valign="bottom"><img src="http://www.sitepoint.com/forums/images/red_arrow.jpg" width="16" height="17" " />
</td></tr></table></form>

I am also reading the link the Guido gave me, but nothing so far. I’ll keep going at it and if I’m confusing you too much, I’ll just rewrite the query. It’s not a big deal. I was just eager to use functions, but the link the Mentor gave me said that there is no reason to use functions if I’m only going to use it once in that page.

Here is another idea:


<?php

define('MAILBOX_LIMIT', 2);

function num_of_messg_by_user( $user ) {
	$query = "SELECT id 
				FROM mailbox 
				WHERE to_user = '". $user ."'
				ORDER BY date DESC LIMIT 50";
	$result = mysql_query($query);
	return mysql_num_rows( $result );
}

function mailbox_full( $currNum, $limit ) {
	$stmnt = FALSE;
	if( $currNum >= $limit ) {
		$stmnt = TRUE;
	}	
	return $stmnt;
}

$currUser = mysql_real_escape_string( $_SESSION['username'] );
$numberOfMessg = num_of_messg_by_user( $currUser );

?>
<html>
	<head>
		<title>Mailbox Test</title>
	<head>
	<body>
		<h3>Welcome <?php echo $currUser; ?>!</h3>
		
		<p>The number of messages in your inbox are: <em><?php echo $numberOfMessg; ?></em>
		<?php if( mailbox_full($numberOfMessg, MAILBOX_LIMIT) ) : ?>
			<span class="error">You have too many messages</span>
		<?php endif; ?>		
	</body>
</html>