Page 1 of 1

Conditional Statements Troubleshooting

Posted: Tue Jul 12, 2005 11:08 pm
by Majoraslayer
Alright, so I'm off to writing my first useful PHP application. Its a user registration system that uses the same php file to display the user control panel or the registration fields and agreements (yes, its inspired by the way phpBB works). Its the first time I've tried conditional statements on my own...and I've screwed up already. Here's my code:

Code: Select all

<?php
if ( $mode=&quote;profile&quote; )
{
echo &quote;їProfile Control Panel Placeholder]&quote;;
}
if ( $mode=&quote;register&quote; and $coppa<>&quote;agree&quote; )
{
echo &quote;їCoppa Agreement Placeholder]&quote;;
}
if ( $mode=&quote;register&quote; and $coppa=&quote;agree&quote; )
{
echo &quote;їRegistration Form Here]&quote;;
}
?>
And to see what its doing, check out

http://www.zorasdomain.com/usercp.php

Basically, what it is supposed to echo is placeholders to check my conditional statements so I can plug in those elements later. It is supposed to check $mode to decide what the user needs (registration or control panel). Then, if it decides that $mode="register", it is supposed to check if $coppa="yes". If it does, it should display the registration form. If it doesn't, then it should display the COPPA agreement. However, its displaying both (I'm using placeholders for the agreement and the form, if you hadn't caught that above). Does anyone know what I'm doing wrong here? Feel free to educate me a bit, as I'm still a complete PHP noob.

Also, clicking the above link shouldn't display anything, as $mode and $coppa wouldn't be assigned a value. No matter what they equal it seems, both conditions are output.

Posted: Tue Jul 12, 2005 11:14 pm
by hawleyjr
First off use == and not =

Secondly

Code: Select all

if ( $mode="register" ) and ( $coppa<>"agree" )
Should be

Code: Select all

if ( $mode == 'register' && $coppa != 'agree' )

Posted: Tue Jul 12, 2005 11:17 pm
by Majoraslayer
I knew about the second errors; you just saw them before I edited my code. It should now just be what I'm focusing on, and with those problems fixed :oops: .

As for the other, it seems to have fixed my problems. I hadn't thought to check for the same data type as well. Its my first time trying to pass variables between pages, so I didn't know to do that >_<.

Posted: Tue Jul 12, 2005 11:34 pm
by Sphen001
Hi,

The way your code looks right now, it's possible that more that one section will be displayed. If you only want one section to display at any given time, use elseif

Code: Select all

<?php
if ( $mode == "profile" )
{
  echo "[Profile Control Panel Placeholder]";
}
elseif ( $mode == "register" && $coppa != "agree" )
{
  echo "[Coppa Agreement Placeholder]";
}
elseif ( $mode == "register" && $coppa == "agree" )
{
  echo "[Registration Form Here]";
}
?>
Hope this helps :D

Sphen001

Posted: Tue Jul 12, 2005 11:39 pm
by neophyte
Majoraslayer wrote:As for the other, it seems to have fixed my problems. I hadn't thought to check for the same data type as well.
assigment operator =
comparison operator == both sides are same but might not be same type
comparison operator of the same type === both sides are same and same type
Does not equal each other <> or !=

Posted: Tue Jul 12, 2005 11:41 pm
by harrisonad

Code: Select all

if($mode=="profile"){
  echo "[Profile Control Panel Placeholder]";
}elseif($mode=="register"){
  if($coppa!="agree"){
    echo "[Coppa Agreement Placeholder]";
  }elseif($coppa=="agree"){
    echo "[Registration Form Here]";
  }
}
I just removed the redundancies.