matthewdacruz
asked on
Need help with a switch case statement .in php
Hi Experts
I am trying to write a switch statement in php that will echo back different outcomes based on the data that is called from the database.
I have included my code below of the entire page.
The page works and populates the variables when the switch statement is not present.
As soon as I add the switch statement it fails. Could someone please help me with the cases / switch statement.
For the example the following variable values are listed in order below
$LoanType = Personal Loan
$startDate = 1990-02-01
$endDate = 2005-09-10
$loanamount = 10000
$Futureloanamount = 2000
$totalLoan = 12000
$PPITaken = Yes
$ClaimClosed = 1
I am trying to write a switch statement in php that will echo back different outcomes based on the data that is called from the database.
I have included my code below of the entire page.
The page works and populates the variables when the switch statement is not present.
As soon as I add the switch statement it fails. Could someone please help me with the cases / switch statement.
For the example the following variable values are listed in order below
$LoanType = Personal Loan
$startDate = 1990-02-01
$endDate = 2005-09-10
$loanamount = 10000
$Futureloanamount = 2000
$totalLoan = 12000
$PPITaken = Yes
$ClaimClosed = 1
<?php require_once('../../Connections/map.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$colname_rs_scorecard = "50";
if (isset($_GET['claimid'])) {
$colname_rs_scorecard = (get_magic_quotes_gpc()) ? $_GET['claimid'] : addslashes($_GET['claimid']);
}
mysql_select_db($database_map, $map);
$query_rs_scorecard = sprintf("SELECT * FROM map_claims WHERE claimid = %s", GetSQLValueString($colname_rs_scorecard, "int"));
$rs_scorecard = mysql_query($query_rs_scorecard, $map) or die(mysql_error());
$row_rs_scorecard = mysql_fetch_assoc($rs_scorecard);
$totalRows_rs_scorecard = mysql_num_rows($rs_scorecard);
// Define claim type
$LoanType = $row_rs_scorecard['loanType'];
$startDate = $row_rs_scorecard['LoanAgreementStartDate'];
$endDate = $row_rs_scorecard['LoanAgreementEndDate'];
$loanamount = $row_rs_scorecard['AdvanceAmount'];
$Futureloanamount = $row_rs_scorecard['FurtherAdvanceAmount'];
$totalLoan = $row_rs_scorecard['AdvanceAmount'] + $row_rs_scorecard['FurtherAdvanceAmount'];
$PPITaken = $row_rs_scorecard['ASUInsurance'];
$ClaimClosed = $row_rs_scorecard['LoanAgreementCompleted'];
// cases
switch($LoanType){
case = 'Personal Loan':
if(($startDate >= 1990/01/01 ) && ($endDate <= 2007/04/01) && ($loanamount >= 5000) && ($PPITaken == 'Yes') && ($ClaimClosed = 0)){
echo 'Green: High Probability: Open Personal Loan';
}elseif(($startDate >= 1990/01/01 ) && ($endDate <= 2007/04/01) && ($loanamount >= 5000) && ($PPITaken == 'Yes') && ($ClaimClosed = 1)){
echo 'Green: High Probability: Closed Personal Loan';
}elseif(($startDate >= 1990/01/01 ) && ($endDate <= 2007/04/01) && ($loanamount >= 5000) && ($PPITaken == 'No') && ($ClaimClosed = 0)){
echo 'Amber: Probability: Open Personal Loan ';
}elseif(($startDate >= 1990/01/01 ) && ($endDate <= 2007/04/01) && ($loanamount >= 5000) && ($PPITaken == 'No') && ($ClaimClosed = 1)){
echo 'Amber: Probability: Closed Personal Loan'
}elseif(($startDate >= 1990/01/01 ) && ($endDate <= 2007/04/01) && ($loanamount < 5000) && ($PPITaken == 'Yes') && ($ClaimClosed = 0)){
echo 'Amber: Possible to Claim: Open Personal Loan';
}elseif(($startDate >= 1990/01/01 ) && ($endDate <= 2007/04/01) && ($loanamount < 5000) && ($PPITaken == 'Yes') && ($ClaimClosed = 1)){
echo 'Amber: Possible to Claim: Closed Personal Loan';
}else{
echo 'Grey: Low Probablility';
}
break;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p><?php echo $LoanType ?></p>
<p><?php echo $startDate ?>
</p>
<p><?php echo $endDate ?>
</p>
<p><?php echo $loanamount ?>
</p>
<p><?php echo $Futureloanamount ?>
</p>
<p><?php echo $totalLoan ?>
</p>
<?php echo $PPITaken ?>
</body>
</html>
<?php
mysql_free_result($rs_scorecard);
?>
ASKER
HI Josh Benner
I have fixed the bug you pointed out but still get an internal error on the block of code.
I have fixed the bug you pointed out but still get an internal error on the block of code.
switch($LoanType){
case "Personal Loan":
if(($startDate >= 1990/01/01 ) && ($endDate <= 2007/04/01) && ($loanamount >= 5000) && ($PPITaken == 'Yes') && ($ClaimClosed = 0)){
echo 'Green: High Probability: Open Personal Loan';
}elseif(($startDate >= 1990/01/01 ) && ($endDate <= 2007/04/01) && ($loanamount >= 5000) && ($PPITaken == 'Yes') && ($ClaimClosed = 1)){
echo 'Green: High Probability: Closed Personal Loan';
}elseif(($startDate >= 1990/01/01 ) && ($endDate <= 2007/04/01) && ($loanamount >= 5000) && ($PPITaken == 'No') && ($ClaimClosed = 0)){
echo 'Amber: Probability: Open Personal Loan ';
}elseif(($startDate >= 1990/01/01 ) && ($endDate <= 2007/04/01) && ($loanamount >= 5000) && ($PPITaken == 'No') && ($ClaimClosed = 1)){
echo 'Amber: Probability: Closed Personal Loan'
}elseif(($startDate >= 1990/01/01 ) && ($endDate <= 2007/04/01) && ($loanamount < 5000) && ($PPITaken == 'Yes') && ($ClaimClosed = 0)){
echo 'Amber: Possible to Claim: Open Personal Loan';
}elseif(($startDate >= 1990/01/01 ) && ($endDate <= 2007/04/01) && ($loanamount < 5000) && ($PPITaken == 'Yes') && ($ClaimClosed = 1)){
echo 'Amber: Possible to Claim: Closed Personal Loan';
}else{
echo 'Grey: Low Probablility';
}
break;
}
Ah, you are also missing a semicolon at the end of line 64.
ASKER
Hi Josh,
Thanks for that, I have run the case and based on the variable info I should pass for the 2nd if statement and get the ''Green: High Probability: Closed Personal Loan''
What is happening is the code is running all the way through the if statements and giving the last outcome
Grey: Low Probablility
Have I written the ifelse statements correct in your opinion.
Why would it run through the statement if the 2nd if statement proves true?
Thanks for that, I have run the case and based on the variable info I should pass for the 2nd if statement and get the ''Green: High Probability: Closed Personal Loan''
What is happening is the code is running all the way through the if statements and giving the last outcome
Grey: Low Probablility
Have I written the ifelse statements correct in your opinion.
Why would it run through the statement if the 2nd if statement proves true?
Your dates in your if statements are not enclosed in quotes, resulting in those numbers being evaluated as a mathematical expression.
Try something along these lines instead:
Try something along these lines instead:
switch($LoanType) {
case 'Personal Loan':
if (($startDate >= '1990/01/01' ) && ($endDate <= 2007/04/01) && ($loanamount >= 5000) && ($PPITaken == 'Yes') && ($ClaimClosed = 0)){
echo 'Green: High Probability: Open Personal Loan';
} elseif (($startDate >= '1990/01/01' ) && ($endDate <= 2007/04/01) && ($loanamount >= 5000) && ($PPITaken == 'Yes') && ($ClaimClosed = 1)){
echo 'Green: High Probability: Closed Personal Loan';
} elseif (($startDate >= '1990/01/01' ) && ($endDate <= 2007/04/01) && ($loanamount >= 5000) && ($PPITaken == 'No') && ($ClaimClosed = 0)){
echo 'Amber: Probability: Open Personal Loan ';
} elseif (($startDate >= '1990/01/01' ) && ($endDate <= 2007/04/01) && ($loanamount >= 5000) && ($PPITaken == 'No') && ($ClaimClosed = 1)){
echo 'Amber: Probability: Closed Personal Loan';
} elseif (($startDate >= '1990/01/01' ) && ($endDate <= 2007/04/01) && ($loanamount < 5000) && ($PPITaken == 'Yes') && ($ClaimClosed = 0)){
echo 'Amber: Possible to Claim: Open Personal Loan';
} elseif (($startDate >= '1990/01/01' ) && ($endDate <= 2007/04/01) && ($loanamount < 5000) && ($PPITaken == 'Yes') && ($ClaimClosed = 1)){
echo 'Amber: Possible to Claim: Closed Personal Loan';
} else {
echo 'Grey: Low Probablility';
}
break;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Josh, Learnt a tremendous amount from you today. i appreciate the assistance
case 'Personal Loan':
See switch/case statement block implemented starting near line 10 for an example of a well-formed switch block.