Solved

Need help with a switch case statement .in php

Posted on 2009-04-13
7
213 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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Installer 5 37
Cookie not unsetting 7 27
Text field validation on text fields used for multiple inserts 7 22
PHP: Best way to scan folders and process files 10 39
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

860 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