Forum Moderators: coopster

Message Too Old, No Replies

How to use switch statements in PHP

How to use switch statements in PHP

         

Mitch888

6:40 am on Feb 10, 2003 (gmt 0)

10+ Year Member



Hi,
I successfully submitted a form to a credit card processing server and captured the results in a text file called ccdata.txt. I read the results from the ccdata.txt into an array.

The value of array[3] contains a number (300=approved, 301=approved with warning, 105&106=declined)

At the bottom of my code nothing gets displayed. Can someone help me with the switch/case thing or tell me what am I missing. Thank you

$res = `/usr/bin/curl -m 120 -d "$data" '$url' `;

$fp = fopen("ccdata.txt","w");
if(!$fp) {
print "error! The file could not be opened";
exit;
}
fwrite($fp, $res);
fclose($fp);
$fp = fopen('ccdata.txt', 'r');
if ($fp) {
$array = explode("\n", fread($fp, filesize('ccdata.txt')));

echo "Merchant messages:<br>";
echo "status: $array[2]<BR>";
echo "result: $array[3]<BR>";

switch ($array[3]){
case "300":
$Approved = true;
$Merr = false;
$MMessage = "Transaction Sucessfull";
break;
case "301":
$Approved = true;
$Merr = false;
$MMessage = "Transaction Sucessfull with warning";
break;
case "105":
$Approved = false;
$Merr = true;
$MMessage = "Declined";
break;
case "106":
$Approved = false;
$Merr = true;
$MMessage = "Declined";
break;
default:
$Approved = false;
$Merr = true;

}

if($Merr==true){

echo "result: $array[4]<BR>";
}else{
echo “declined”;
}

}

Note: After the above code excutes, the ccdata.txt file contains the following:

-----ccdata.txt starts here------
<?xml version="1.0" encoding="UTF-8"?>
- <response>
<status>2</status>
<result>300</result>
<msg>Approved</msg>
<error>no errors</error>
</response>
-----ccdata.txt ends here------

NOTE2:
$res variable gets populated from the result of [ $res = `/usr/bin/curl -m 120 -d "$data" '$url' `; ]
$url = "path to credit card processing server";
$data = "url.encode KEY=VALUE etc.";
$Merr = "some pre determend merchant error";

andreasfriedrich

10:10 am on Feb 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your switch() [php.net] statement works fine. I´d remove the formatted quotation marks from your
echo "declined";
statement.

Why do you want to display a declined message when $Merr is false? In the switch() [php.net] statement you set $Merr to false when the transaction was successful, yet you echo() [php.net] a declined message :o.

Andreas

Mitch888

3:44 pm on Feb 10, 2003 (gmt 0)

10+ Year Member



Yes. I got it to work thanks.
Why I display a declined message when $Merr is false? That was just testing. I was experimenting with all kind of possibilities and echoing the results to see if my statements work or not.

One question, is there anyway to capture the merchant result into an array without having to write it to ccdata.txt and reading it from ccdata.txt?

I am new to php and jumping from ASP windows to unix php is quite a thrill. PHP is a beautiful versatile language and free.

At my work, we are looking at the possibility to switch from win2000server asp to linux php due to the overwhelming cost of licensing and the cost of Microsoft products. So far, with my poor green php coding. The performance already beats Microsoft sql on win 2000 server.