Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need help with a switch case statement .in php

Posted on 2009-04-13
7
Medium Priority
?
219 Views
Last Modified: 2013-12-12
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
<?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);
?>

Open in new window

0
Comment
Question by:matthewdacruz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 1

Expert Comment

by:joshbenner
ID: 24129489
On line 56 in your included code, you have an equal sign between the case statement and the value being cased. Your statement line should rather resemble this:

case 'Personal Loan':

See switch/case statement block implemented starting near line 10 for an example of a well-formed switch block.
0
 

Author Comment

by:matthewdacruz
ID: 24129543
HI Josh Benner

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;
	
}

Open in new window

0
 
LVL 1

Expert Comment

by:joshbenner
ID: 24129593
Ah, you are also missing a semicolon at the end of line 64.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:matthewdacruz
ID: 24129633
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?
0
 
LVL 1

Expert Comment

by:joshbenner
ID: 24129761
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:
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;
}

Open in new window

0
 
LVL 1

Accepted Solution

by:
joshbenner earned 2000 total points
ID: 24129770
And actually, evaluating less than or greater than with string expressions won't cut it. You may want to evaluate that on the database side or convert those dates to integer time expressions using something like:

$startDateUnix = strtotime($startDate);
$jan1990 = strtotime('1990/01/01');

then you can do this:

if ($startDateUnix >= $jan1990) ...
0
 

Author Closing Comment

by:matthewdacruz
ID: 31569485
Thanks Josh, Learnt a tremendous amount from you today. i appreciate the assistance
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question