Solved

Need help with a switch case statement .in php

Posted on 2009-04-13
7
212 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 47
Checking if varaible is empty 6 38
Checking CSRF token within a function 36 74
Checkout Page Input Field not aligned 1 20
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

813 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now