PDA

View Full Version : Help with if else's


zoobie
07-16-2003, 07:12 AM
I'm just about finished but my if/else statements below in bold need a little help...else if...somethin' like that. TIA

<?php

$my_email = 'my-primary-email@home.com';

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);

// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_gross = $_POST['payment_gross'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];



if (!$fp) {
echo "<html><body bgcolor='#ffffff'><br><br><div align='center'>SERVER ERROR - Try again later</div></body></html>";
}

else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
if (strcmp($receiver_email,$my_email) == 0) {
echo "ERROR 904 - Receiver email mismatch";
}

if (strcmp('Completed',$payment_status) == 0) {
echo "ERROR 707 - Payment not completed";
}

if (strcmp('0.03',$payment_gross) == 0) {
echo "ERROR 803 - Price has been changed";
}

//if everything has passed, go ahead and show the echo below

else
echo "<html><body style='background-color:black;overflow:auto'><br><br><div align='center'><form name='fred' action='mail.php' method='post'><button style='border:1px solid white;width:200px;height:35px;background-color:black;font:bold 20px tahoma;color:lime;cursor:pointer' onclick='this.fred.submit();'>SEND IT!</button></form></div></body></html>";
}

else if (strcmp ($res, "INVALID") == 0) {
echo "ERROR 007 - There is something wrong with your Paypal account.<br>Payment denied, failed, pending, or unconfirmed.";
}
}
fclose ($fp);
}
?>

raf
07-16-2003, 07:59 AM
I don't know if this is a 'good' or 'acceptable' method, but when i need to do a lot of checks, on different variabels, i create an errorvariabel. Like

if (your condition that is true if there is an error) {
$error .= "Please fill in a username<br />"
}
if (your condition that is true if there is an error) {
$error .= "Please fill in a password<br />";
}
if (your condition that is true if there is an error) {
$error .= "Please fill in a compagnuname<br />";
}
if (your condition that is true if there is an error) {
$error .= "Please fill in a birthday<br />";
}
...
if strlen($error) > 0 {
echo $error;
}

These are simple if's, buth you can combine that with if-then-elses and switches. I find it just easier to code and read then nested if then elses that go 4-5 levels deep.

Ökii
07-16-2003, 09:47 AM
yup - the parser on yours is having difficulty deciding what the else{} references (logically it will only fire if the last if returns false and ignore all prior)

as mentioned above, you will need to either string test an error string or set a var in each if, then test that

$err=false;
if() { echo 'a'; $err=true; }
if() { echo 'b'; $err=true; }
etc
if($err == false) {echo 'yay no error';}

zoobie
07-16-2003, 05:22 PM
Umm...I just threw that together missing brackets and all...I was hoping someone could see what I was trying to accomplish and straighten it all out...:rolleyes:

brothercake
07-16-2003, 05:31 PM
wouldn't a switch statement be more useful here?

raf
07-16-2003, 07:33 PM
Umm...I just threw that together missing brackets and all...I was hoping someone could see what I was trying to accomplish and straighten it all out...
Nice. Actually though someone would read, edit and debug it?
wouldn't a switch statement be more useful here?
I don't see how you could do this with a switch:confused:

brothercake
07-16-2003, 09:39 PM
i just meant, a switch instead of multiple if statements

zoobie
07-16-2003, 10:30 PM
Originally posted by raf
Nice. Actually though someone would read, edit and debug it?


Sorry...I thought this was the CodingForums...:rolleyes:

raf
07-16-2003, 10:44 PM
zoobie:
Sorry...I thought this was the CodingForums...
It is, but if i see such un layed out code, i just look at structure and see what you try to do + try to provide an altrnative that might be a better and working codingstyle. Not copy and paste your code and start to indent it and look for forgotten brackets or ; that every debugger would catch. (Apparently noone else bothered looking at it closer ...)
i just meant, a switch instead of multiple if statements
Switch cant be used in zoobies code or my example cause that can only be used if the actions depend of the value of 1 variable. No ? (if it can be used, i guess i made a few hundred lines of wasted code)

zoobie
07-17-2003, 12:10 AM
Ok...I did this...but I think it's missing some curly brackets...I don't think it's going to work anyway...

Do I actually need that ==0 bit and is this valid code?
Thanks

if (strcmp ($res, "VERIFIED") == 0) {

if (strcmp($receiver_email,$my_email) == 0)
{
echo "ERROR 904 - Receiver email mismatch";

if (strcmp('Completed',$payment_status) == 0)
{
echo "ERROR 707 - Payment not completed";
if (strcmp('0.03',$payment_gross) == 0)
{
echo "ERROR 803 - Price has been changed";
}
}
}
//if everything above has passed, go ahead and show the echo below

else
{
echo "<html><body style='background- color:black;overflow:auto'><br><br><div align='center'><form name='fred' action='mail.php' method='post'><button style='border:1px solid white;width:200px;height:35px;background-color:black;font:bold 20px tahoma;color:lime;cursor:pointer' onclick='this.fred.submit();'>SEND IT!</button></form></div></body></html>";
}

if (strcmp ($res, "INVALID") == 0)
{
echo "ERROR 007 - There is something wrong with your Paypal account.<br>Payment denied, failed, pending, or unconfirmed.";
}

Jason
07-17-2003, 12:26 AM
if (strcmp ($res, "VERIFIED") == 0) {
if (strcmp($receiver_email,$my_email) == 0){
echo "ERROR 904 - Receiver email mismatch";
if (strcmp('Completed',$payment_status) == 0){
echo "ERROR 707 - Payment not completed";
if (strcmp('0.03',$payment_gross) == 0){
echo "ERROR 803 - Price has been changed";
}
}
}
}
//if everything above has passed, go ahead and show the echo below

else
{
echo "<html><body style='background- color:black;overflow:auto'><br><br><div align='center'><form name='fred' action='mail.php' method='post'><button style='border:1px solid white;width:200px;height:35px;background-color:black;font:bold 20px tahoma;color:lime;cursor:pointer' onclick='this.fred.submit();'>SEND IT!</button></form></div></body></html>";
}

if (strcmp ($res, "INVALID") == 0){
echo "ERROR 007 - There is something wrong with your Paypal account.<br>Payment denied, failed, pending, or unconfirmed.";
}


there is the correct number of curly braces...and yes you need the '0's


Jason

zoobie
07-17-2003, 02:33 AM
Alright Jason...Thanks...Of course, it doesn't work. Although the payment goes thru ok, it doesn't create my dynamic page but rather just shows a regular Paypal page...

I must be psychic...:D