Jump to content

if statements in forms


shadowayex

Recommended Posts

EDIT: I meant to type functions in the title.I'm not sure if it's the problem or not, but I'm starting to think that I if statements don't work inside of functions. I have a function I want to use on my site that shows user alerts. It looks like:

function userInfo() { if($user == "Guest") { /*----Guest Code-----*/?>  Please <a href="index.php?mode=login">log in</a> to see alerts. No account? <a href="index.php?mode=signup">Sign up</a>, it's free!<?php } else {//----User Alerts //----Messages  $msgs = mysql_query("SELECT * FROM messages WHERE Receiver = '$user' AND ReadStat = 'unread'");  $msgs = mysql_num_rows($msgs);  if($msgs == 1) {?>   You have <a href="messages.php">1 message</a>.<?php  }  elseif($msgs > 1) {?>   You have <a href="messages.php"><?php echo $msgs; ?> messages</a>.<?php   } }}

If I try to have the statement write something plain, it works. If I put the code that is inside the function on the page, it works. When I combine them together, it does not. Why?

Link to comment
Share on other sites

If I try to have the statement write something plain, it works. If I put the code that is inside the function on the page, it works. When I combine them together, it does not. Why?
What do "have the statement write something plain" and "put the code that is inside the function on the page" mean? They seem very vague to me...
Link to comment
Share on other sites

What do "have the statement write something plain" and "put the code that is inside the function on the page" mean? They seem very vague to me...
<?php  if($user == "Guest") { /*----Guest Code----*/?>  Please <a href="index.php?mode=login">log in</a> to see alerts. No account? <a href="index.php?mode=signup">Sign up</a>, it's free!<?php } else { /*----User Alerts----   ----Messages----*/  $msgs = mysql_query("SELECT * FROM messages WHERE Receiver = '$user' AND ReadStat = 'unread'");  $msgs = mysql_num_rows($msgs);  if($msgs == 1) {?>   You have <a href="messages.php">1 message</a>.<?php  }  elseif($msgs > 1) {?>   You have <a href="messages.php"><?php echo $msgs; ?> messages</a>.<?php   } }?>

It will work fine. But I don't want to put the code in there. I want to put the code in the function and call the function. But it won't display anything, not even an error.

Link to comment
Share on other sites

function userInfo() {if($user == "Guest") { /*----Guest Code-----*/Where is the variable: $user comming from?If $user is coming from outside the function, then it is a global variable therefore you must use

function userInfo(){global $user;/* rest of your function*/}

Link to comment
Share on other sites

To make sure the script is executing, echo stuff in each case after the ?> <?php output. Does that work?EDIT: Thanks, Silver. :)

Link to comment
Share on other sites

To make sure the script is executing, echo stuff in each case after the ?> <?php output. Does that work?EDIT: Thanks, Silver. :)
Yeah, I just tested it. It works. I think the only problem was that you didn't declare global $user in your function. That caused PHP to automatically set the variable $user to null.This is what happened.
if ($user == "Guest")// Nope the $user does not equal "Guest" because it is null.else // User alerts// mysql_query("SELECT * FROM messages WHERE Receiver = '$user' AND ReadStat = 'unread'")// mysql_num_rows = 0 because Receiver doesn't exist. Receiver = null because $user = null which causes $msgs = 0<?php if($msgs == 1) {?>You have <a href="messages.php">1 message</a>.<?php}elseif($msgs > 1) {?>You have <a href="messages.php"><?php echo $msgs; ?> messages</a>.<?php}// $msgs = 0, You only have if ($msgs == 1) and elseif ($msgs > 1). You don't have an elseif ($msgs == 0) therefore there was nothing to print!?>

You don't display any message when $user has 0 messages.

Link to comment
Share on other sites

In addition, it would be much easier to parse your code, both for you and others, if you would separate out the php logic from the html. Run your scripts so that messages you want to send to the users are assigned to variables, then generate your html code and insert those messages into the code. This will let you parse your php code for errors more easily while also creating your html code more effectively.As mentioned in previous posts, paying attention to scope is important. If you don't declare variables as "global," they will not be available in functions if they were generated outside of the function.--Kenoli

Link to comment
Share on other sites

Ok, I understand everything but the parsing ordeal. As you can see, I started closing the PHP when I had HTML coming up, wrote the HTML out like usual, then opening the PHP back up because I figured that'd be the best way. I'm not understanding what you're saying with the variables and what not. Can you give me an example?Oh and the user variable was set way at the beginning of the code, looks like this:

if(isset($_SESSION['user'])) { $user = $_SESSION['user'];}else { $user = "Guest";}

Do I have to add the word global in front of that as well?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...