Jump to content

switch case returns partial results - logic issue?


learningcurve
Go to solution Solved by mac_gyver,

Recommended Posts

I have an order process page that uses a switch statement.  I am getting no errors and it is partially working. Case 1,2 and 3 work fine. Cases 4-7 only return itemcode1 and itemprice1.  It is the correct code and amount, but it doesn't return itemcode/price 2 and/or 3.  I know the data is being posted from the order page, so it has to be some flaw in how the switch is working. I am fairly new to php so would appreciate any pointers.  The switch starts on line 155.

<?php
include_once("db.php");
include_once("globals.php");
error_reporting(E_PARSE);
if (isset($_POST['submit'])) {
	extract($_REQUEST);
		if($id_subcount==2){
			$discount_rate=DISCOUNT_2Journals;
		
		} elseif($id_subcount==3){
			$discount_rate=DISCOUNT_3Journals;
		}else{
			$discount_rate="";
		
		}

	$firstname = $_POST['firstname'];
	$lastname = $_POST['lastname'];
	$contact_name=$lastname.", ".$firstname;

	
	
	$emails = array();
	
	// The receipt header is only sent to the person that placed the order
	$receiptHeader = <<<EndOfText
Your order from the Journal has been received and we will begin processing it once we have received your payment. Please allow 7-10 days for you payment to be processed and for your order to be carried out.
If your order includes online access, you will recieve an email containing instructions upon the completion of your order.
Thank you and enjoy!

Below is the information we received from your submission. If you notice any errors, please contact us immediately so we can correct them.
<br/><br/><br/>

EndOfText;

if ($JECT === 'JECTindividual_print') {
	$JECTorder = 'JECT Individual Print Subscription';
} elseif ($JECT === 'JECTindividual_electronic'){
	$JECTorder = 'JECT Individual Electronic Subscription';	
} elseif ($JECT === 'JECTinstitutional_electronic'){
	$JECTorder = 'JECT Insitutional Subscription';
} elseif ($JECT === 'JECTindividual_combo'){
	$JECTorder = 'JECT Individual Combo Subscription';
} elseif ($JECT === 'JECTinstitutional_print'){
	$JECTorder = 'JECT Institutional Print Subscription';
} else {
	$JECTorder = 'JECT subscription';
}

if ($JCTL === 'JCTLindividual_print') {
	$JCTLorder = 'JCTL Individual Print Subscription';
} elseif ($JCTL === 'JCTLindividual_electronic'){
	$JCTLorder = 'JCTL Individual Electronic Subscription';	
} elseif ($JCTL === 'JCTLinstitutional_electronic'){
	$JCTLorder = 'JCTL Insitutional Subscription';
} elseif ($JCTL === 'JCTLindividual_combo'){
	$JCTLorder = 'JCTL Individual Combo Subscription';
} elseif ($JCTL === 'JCTLinstitutional_print'){
	$JCTLorder = 'JCTL Institutional Print Subscription';
} else {
	$JCTLorder = 'JCTL subscription';
}

if ($LCJ === 'LCJindividual_print') {
	$LCJorder = 'LCJ Individual Print Subscription';
} elseif ($LCJ === 'LCJindividual_electronic'){
	$LCJorder = 'LCJ Individual Electronic Subscription';	
} elseif ($LCJ === 'LCJinstitutional_electronic'){
	$LCJorder = 'LCJ Insitutional Subscription';
} elseif ($LCJ === 'LCJindividual_combo'){
	$LCJorder = 'LCJ Individual Combo Subscription';
} elseif ($LCJ === 'LCJinstitutional_print'){
	$LCJorder = 'LCJ Institutional Print Subscription';
} else {
	$LCJorder = 'LCJ subscription';
}
	$receipt = $contact_name."\n";
	if ($_POST['department']!='') $receipt .= $_POST['department']."\n";
	if ($_POST['institution']!='') $receipt .= $_POST['institution']."\n";
	$receipt .= $_POST['address']."\n";
	$receipt .= $_POST['city'].', '.$_POST['state'].' '.$_POST['zip']."\n";
	$receipt .= $_POST['country']."\n";
	$receipt .= $_POST['phone']."\n";
	$receipt .= $_POST['email']."\n\n";
	$order_details="";
	$order = '<table border="1"><tr><th>Item</th><th>Base Price<br />(Single Year)</th><th>Your Price<br />(Single Year)</th>Length</th><th>Renewal?</th></tr>';
	if($JECT && $JECT!="no"){
		if($JECTrenewal){ $rene1="Yes";} else { $rene1="No";} 
		$order_details .=$JECTorder."\n";
		$order .= "<tr><td>".$JECTorder."</td><td>".$itemprice1."</td><td>".$jectprice_hidden."</td><td>".$JECTsub."</td><td>".$rene1."</td></tr>";
	}
	if($JCTL && $JCTL!="no"){
		if($JCTLrenewal){ $rene2="Yes";} else { $rene2="No";} 
		$order_details .=$JCTLorder."\n";
		$order .= "<tr><td>".$JCTLorder."</td><td>".$itemprice2."</td><td>".$jctlprice_hidden."</td><td>".$JCTLsub."</td><td>".$rene2."</td></tr>";
	}
	if($LCJ && $LCJ!="no"){
		$order_details .=$LCJorder."\n";
		if($LCJrenewal){ $rene3="Yes";} else { $rene3="No";} 
		$order .= "<tr><td>".$LCJorder."</td><td>".$itemprice3."</td><td>".$lcjprice_hidden."</td><td>".$LCJsub."</td><td>".$rene3."</td></tr>";
	}
	$order .="</table>";
	$receipt .= $order;
	$receipt .= "\n\nSubtotal: ".$subtotal;
	$receipt .= "\n\nDiscount: ".$discount."%";
	$receipt .= "\n\nGrand total: ".$total;
	$receipt .= "\n\nPaying by: ".$payment_method;
	
	$message=$receiptHeader.$receipt;
	if($JCTLrenewal){ $JCTLrenewal="Yes";} else $JCTLrenewal="No";
	if($LCJrenewal){ $LCJrenewal="Yes";} else $LCJrenewal="No";
	if($JECTrenewal){ $JECTrenewal="Yes";} else $JECTrenewal="No";
	$sql_insert="INSERT INTO orders SET type='$subscription_type',order_details='$order_details',subtotal='$subtotal',discount='$discount'
	,grand_total='$total',payment_method='$payment_method',contact_name='$contact_name',email='$email',net_addresses='$net_addresses',
	department='$department',phone='$phone',address='$address',city='$city',state='$state',zip='$zip',country='$country',tech_contact='$tech_contact',
	tc_phone='$tc_phone',tc_email='$tc_email',tier='$tier',ip='".$_SERVER['REMOTE_ADDR']."',processed_date='".date("Y-m-d H:i:s")."',
	jectprice='$jectprice_hidden',jctlprice='$jctlprice_hidden',lcjprice='$lcjprice_hidden',jectrenewal='$JECTrenewal',jctlrenewal='$JCTLrenewal',lcjrenewal='$LCJrenewal',
	jctllength='$JCTLsub',jectlength='$JECTsub',lcjlength='$LCJsub' ";
	
	//echo $sql_insert;exit;
	$result=mysql_query($sql_insert);
	$ids=mysql_insert_id();
	$headers  = "MIME-Version: 1.0" . "\r\n";
	$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
	$headers .= "From: lisa@gmail.com <lisa@gmail.com>" . "\r\n";
	$subject="Your order has been received";
	$subject1="CELTUA Journal order for $contact_name";
	@mail($email, $subject, $message, $headers);
	
	@mail("lisa@gmail.com", $subject1, $message, $headers);
	
	if ($_POST['payment_method']!='Credit card (online)') {
		header("Location: order_thankyou.php");
	} else {

include 'header.php';
?>
<h1>Journal Order, continued</h1>
<h2>Step 2: Make online payment</h2>

<p>You will now be taken to a secure payment site to enter your credit card information and complete your online payment.</p>

<p><strong>Please have your credit card ready.</strong></p>

<p>Please note that if you remain on any one page of the payment site for more than about 10 minutes, even if you are working on it, it will close and send you to our "thank you" page <strong>without</strong> accepting your payment. If this happens, or if you have any other problems or questions, please contact:</p>

<p>Circulation Manager</br>
Monroe, OH 45050</br>
United States</br></br>

<form method="post" action="https://commerce.cashnet.com/404Handler/pageredirpost.aspx?virtual=CELTJP">
	<p>
	
<?php
switch(true)
{
	case (($JECT && $JECT!="no") && ($JCTL && $JCTL!="yes") && ($LCJ && $LCJ!="yes")):
		$itemcode1=$JECTitemCode;
		$itemprice1=$jectprice_hidden;
		?>
		<input type='hidden' name='itemcode1' value=<?php echo $itemcode1;  ?>>
		<input type='hidden' name='amount1' value="<?php echo round($itemprice1,2); ?>">
		<?php
		break;
	
	case (($JECT && $JECT!="yes") && ($JCTL && $JCTL!="no") && ($LCJ && $LCJ!="yes")):
		$itemcode1=$JCTLitemCode;
		$itemprice1=$jctlprice_hidden;
		?>
		<input type='hidden' name='itemcode1' value=<?php echo $itemcode1;  ?>>
		<input type='hidden' name='amount1' value="<?php echo round($itemprice1,2); ?>">
		<?php
		break;
	case (($JECT && $JECT!="yes") && ($JCTL && $JCTL!="yes") && ($LCJ && $LCJ!="no")):
		$itemcode1=$LCJitemCode;
		$itemprice1=$lcjprice_hidden;
		?>
		<input type='hidden' name='itemcode1' value=<?php echo $itemcode1;  ?>>
		<input type='hidden' name='amount1' value="<?php echo round($itemprice1,2); ?>">
		<?php
		break;
	case (($JECT && $JECT!="no") && ($JCTL && $JCTL!="no") && ($LCJ && $LCJ!="yes")):
		$itemcode1=$JECTitemCode;
		$itemprice1=$jectprice_hidden;
		$itemcode2=$JCTLitemCode;
		$itemprice2=$jctlprice_hidden;
		?>
		<input type='hidden' name='itemcode1' value=<?php echo $itemcode1; ?>>
		<input type='hidden' name='amount1' value=<?php echo round($itemprice1,2); ?>">
		<input type='hidden' name='itemcode2' value=<?php echo $itemcode2; ?>>
		<input type='hidden' name='amount2' value=<?php echo round($itemprice2,2); ?>">
		<?php
                break;
	case (($JECT && $JECT!="no") && ($JCTL && $JCTL!="yes") && ($LCJ && $LCJ!="no")):
		$itemcode1=$JECTitemCode;
		$itemprice1=$jectprice_hidden;
		$itemcode2=$LCJitemCode;
                $itemprice2=$lcjprice_hidden;
		?>
		<input type='hidden' name='itemcode1' value=<?php echo $itemcode1; ?>>
		<input type='hidden' name='amount1' value=<?php echo round($itemprice1,2); ?>">
		<input type='hidden' name='itemcode2' value=<?php echo $itemcode2; ?>>
		<input type='hidden' name='amount2' value=<?php echo round($itemprice2,2); ?>">
		<?php
		break;
	case (($JECT && $JECT!="yes") && ($JCTL && $JCTL!="no") && ($LCJ && $LCJ!="no")):
		$itemcode1=$JCTLitemCode;
		$itemprice1=$jctlprice_hidden;
		$itemcode2=$LCJitemCode;
                $itemprice2=$lcjprice_hidden;
		?>
		<input type='hidden' name='itemcode1' value=<?php echo $itemcode1; ?>>
		<input type='hidden' name='amount1' value=<?php echo round($itemprice1,2); ?>">
		<input type='hidden' name='itemcode2' value=<?php echo $itemcode2; ?>>
		<input type='hidden' name='amount2' value=<?php echo round($itemprice2,2); ?>">
		<?php
		break;
	case (($JECT && $JECT!="no") && ($JCTL && $JCTL!="no") && ($LCJ && $LCJ!="no")):
		$itemcode1=$JECTitemCode;
		$itemprice1=$jectprice_hidden;
		$itemcode2=$JCTLitemCode;
		$itemprice2=$jctlprice_hidden;
		$itemcode3=$LCJitemCode;
                $itemprice3=$lcjprice_hidden;
		?>
		<input type='hidden' name='itemcode1' value=<?php echo $itemcode1; ?>>
		<input type='hidden' name='amount1' value=<?php echo round($itemprice1,2); ?>">
		<input type='hidden' name='itemcode2' value=<?php echo $itemcode2; ?>>
		<input type='hidden' name='amount2' value=<?php echo round($itemprice2,2); ?>">
		<input type='hidden' name='itemcode3' value=<?php echo $itemcode3; ?>>
		<input type='hidden' name='amount3' value=<?php echo round($itemprice3,2); ?>">
		<?php
		break;
}
?>
		
		
		<input type="hidden" name="ref1type1" value="ID">
		<input type="hidden" name="ref1val1" value="<?php echo $ids; ?>">
		<input type="hidden" name="ref2type1" value="STORE">
		<input type="hidden" name="ref2val1" value="journals">
		<input type="hidden" name="ref3type1" value="NAMEFIRST">
		<input type="hidden" name="ref3val1" value="<?php echo $firstname; ?>">
		<input type="hidden" name="ref4type1" value="NAMELAST">
		<input type="hidden" name="ref4val1" value="<?php echo $lastname; ?>">
			
		<input type="submit" value="Make online payment">
	</p>
</form>
<?php
include 'footer.php';
}
}
?>
Link to comment
Share on other sites

Have a look at the "View Source" feature of your browser. You are missing double-quotes on several of the "value=" attributes, and you have "closing" double-quotes on some that you never put "openning" double-quotes on.

 

Clean up your HTML and see if that solves your problems.

Link to comment
Share on other sites

sorry to jump in here. but you have too much logic, making it hard for you to see the forest for the trees.

 

the variables $JECT, $JCTL, and $LCJ contain either the string 'yes' (apparently for a renewal), 'no' (don't renew), or one of the listed choices (i.e. add/start a new subscription for 'JECTindividual_print').

 

at the point of your case/switch logic, a !="no" condition means either a 'yes' (renew) or one of the listed choices (add/start). a !="yes" condition means either a 'no' (don't renew) or one of the listed choices (add/start.) so if someone does pick one of the listed choices for the three variables, that will match one of the first three case statements and the rest of case statements are never ran due to the break; statements.

 

if all you are trying to do is add one to three sets of form fields, with incrementing itemcodex, amountx fields, just use a variable $x that gets incremented, then have a set of if(){} logic, one for each of the $JECT, $JCTL, and $LCJ variables, testing for the !="no" condition.

 

[rant] using an array and generic variable names, would have allowed you to have any number of different products with at least 1/3 the amount of code needed to produce the form and process the data.

Link to comment
Share on other sites

Thanks mac_gyver,

I had a laugh at "too much logic".  The notion of too much logic is funny, even though that is exactly the case.  I can't use generics and some of the complications arrive from what I am trying to accomplish.  The part of the code that contains the switch case is submitting to a payment site and it needs specific information in a specific format.  It needs to have the itemcode and the item amount for each thing ordered.  What is causing the monkey wrench for me is that is always has to have an item one.  So if  person orders a single item, that will always be item one.  If they order two items, there will be an item one and an item two.  Obviously then if they order all three, there will be three itemcodes and threeitem prices.  The code I currently have running, for example, always has JECT as itemcode1, JCTL as itemcode2, and LCJ as itemcode3.  But if they don't order JECT, then JCTL has to become itemcode1, etc.  I know most of this is caused by my noobness, so forgive me if I don't understand exactly the advice you are giving.

Link to comment
Share on other sites

  • Solution

try this -

$x = 1; // incrementing field name/number
if($JECT && $JECT!="no"){
    $itemcode1=$JECTitemCode;
    $itemprice1=$jectprice_hidden;
    echo "<input type='hidden' name='itemcode$x' value='$itemcode1'>
    <input type='hidden' name='amount$x' value='".round($itemprice1,2)."'>\n";
    $x++;
}

if($JCTL && $JCTL!="no"){
    $itemcode1=$JCTLitemCode;
    $itemprice1=$jctlprice_hidden;
    echo "<input type='hidden' name='itemcode$x' value='$itemcode1'>
    <input type='hidden' name='amount$x' value='".round($itemprice1,2)."'>\n";
    $x++;
}

if($LCJ && $LCJ!="no"){
    $itemcode1=$LCJitemCode;
    $itemprice1=$lcjprice_hidden;
    echo "<input type='hidden' name='itemcode$x' value='$itemcode1'>
    <input type='hidden' name='amount$x' value='".round($itemprice1,2)."'>\n";
}
Link to comment
Share on other sites

mac_gyver, you are a genius!  I can't thank you enough.  Your original comment got me thinking and I was redoing it and taking out some of the "trees" but was still flailing in the forest.  Your code works like a charm and does exactly what I need.  This has been a great learning experience for me and hopefully improved my skills.   :happy-04:  :happy-04:  :happy-04:

Edited by learningcurve
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.