Solved

Need help with a switch case statement .in php

Posted on 2009-04-13
7
206 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.

760 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

19 Experts available now in Live!

Get 1:1 Help Now