Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

General Improvement and AOL E-mail Problems

Status
Not open for further replies.

thegentleman

IS-IT--Management
Apr 4, 2001
65
GB
Hi,

I'm very new to PHP and have created a simple function that calculates an estimate based on the weight and quantity of a series of packages. The form that it is submitted from will have 1 to 10 packages. Each package can have on of three options which are then used to assign a value to that package. A running total is then created and sent back to an asp page also e-mailed to both the user and the adminstrator of the website.

Discounts are also applied of the quantity of each type of package exceed 1. The function works fine but in a bid to enhance my PHP skills I want to simplify it.

Any ideas?

Oh - during testing it was reviled that the e-mail that is sent out is fine except in AOL - where the numbers and layout are all wrong. Any way to avoid this?

Thanks to all,

~tg

<?php

$total = 0;
$low = 0;
$mid = 0;
$high = 0;
$saving = 0;

function calculate(&$var)
{

global $total;
global $low;
global $mid;
global $high;

switch ($var) {

case 1:
$total = $total + 8.95;
$low = $low + 1;
break;

case 2:
$total = $total + 10.95;
$mid = $mid + 1;
break;

case 3:
$total = $total + 12.95;
$high = $high + 1;
break;
}

}

switch ($pnumber) {

case 1:
calculate($weight1);
break;

case 2:
calculate($weight1);
calculate($weight2);
break;

case 3:
calculate($weight1);
calculate($weight2);
calculate($weight3);
break;

case 4:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
break;

case 5:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
break;

case 6:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
break;

case 7:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
calculate($weight7);
break;

case 8:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
calculate($weight7);
calculate($weight8);
break;

case 9:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
calculate($weight7);
calculate($weight8);
calculate($weight9);
break;

case 10:
calculate($weight1);
calculate($weight2);
calculate($weight3);
calculate($weight4);
calculate($weight5);
calculate($weight6);
calculate($weight7);
calculate($weight8);
calculate($weight9);
calculate($weight10);
break;

}

$inlow = $low - 1;
IF ($inlow > 0) {
$saving = $saving + $inlow;
}

$inmid = $mid - 1;
IF ($inmid > 0) {
$inmid = $inmid * 2;
$saving = $saving + $inmid;
}

$inhigh = $high - 1;
IF ($inhigh > 0) {
$inhigh = $inhigh * 3;
$saving = $saving + $inhigh;
}

$reference = rand(1000000,9999999);

$message = &quot;
Dear $name,

Please find below the details of your estimate:
&quot;;

IF ($low > 0) {
$lowtotal = $low * 8.95;
$message = &quot;$message
$low Parcels less than 10kg = £$lowtotal&quot;;
}

IF ($mid > 0) {
$midtotal = $mid * 10.95;
$message = &quot;$message
$mid Parcels between 10kg and 20kg = £$midtotal&quot;;
}

IF ($high > 0) {
$hightotal = $high * 12.95;
$message = &quot;$message
$high Parcels between 20kg and 30kg = £$hightotal&quot;;
}

switch ($destination) {

case 12:
$total = $total + 4;
$message = &quot;$message
Before 12 noon next day = £4.00&quot;;
break;

case 10:
$total = $total + 6;
$message = &quot;$message
Before 10am next day = £6.00&quot;;
break;
}

$message = &quot;$message
-----------------------------------------------
Subtotal = £$total&quot;;

$total = $total - $saving;

$message = &quot;$message
Discount = £$saving&quot;;

$message = &quot;$message
-----------------------------------------------
Total = £$total
-----------------------------------------------&quot;;

$message = &quot;$message

&quot;;

mail(&quot;xxx@xxx.com&quot;, &quot;On-line estimate REF: $reference&quot;, $message, &quot;From: $email&quot;);
mail($email, &quot;On-line estimate REF: $reference&quot;, $message, &quot;From: xxx@xxx.com&quot;);

header( &quot;Location: estimate.asp?step=3&estimate=$total&email=$email&name=$name&quot; );

?>
 
My first comment: Arrays. Arrays are your friend, use them.

There is also a trick you may not know about in PHP's use of form fields....

If you have an HTML page structured as:
Code:
<html><body>
<form method=post action=test_arrayinput.php>
   <input type=text name=&quot;foo[]&quot;>
   <input type=text name=&quot;foo[]&quot;>
   <input type=text name=&quot;foo[]&quot;>
   <input type=submit>
</form>
</body></html>

Then in test_arrayinput.php, $_POST will have the following structure:

Code:
Array
(
   [foo] => Array
   (
      [0] => a
      [1] => b
      [2] => c
   )
)

or, if you have register_globals turned on, $foo will have the structure:

Code:
Array
(
     [0] => a
     [1] => b
     [2] => c
)


Naming form fields &quot;somename[]&quot; will cause PHP to interpret that set of fields as elements of an array.

If you have your weights in an array, you don't need a switch statement to run through them all -- you can hand your calculate function the entire array, and put the logic there. You now also have the ability to loop through the array elements using for-loops and the like. Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks sleipnir214. I new arrays were the way to go I just don't know how to create and use them in PHP. Thanks again for your help.

Do you have any clue as to why the e-mail is wrong for AOL users?

~tg
 
thegentleman:

Dude, you're on your own with AOL's email system. The only suggestion I can make is to send the email as HTML rather than plaintext.

Perhaps if you describe the problem in greater detail, someone else can give you some ideas. Want the best answers? Ask the best questions: TANSTAAFL!
 
Ok, well in the above function the e-mail that is created looks like this in any e-mail program other than AOL:

-----------------------------------------------
Dear name,

Please find below the details of your estimate for delivery:

1 Parcels less than 10kg = £8.95
-----------------------------------------------
Subtotal = £8.95
Discount = £0
-----------------------------------------------
Total = £8.95
-----------------------------------------------

In AOL, the exact same e-mail looks like this:

-----------------------------------------------
Dear name,

Please find below the details of your estimate for delivery:

1 Parcels less than 10kg = 5
-----------------------------------------------
Subtotal = 5
Discount = -----------------------------------------------
Total = 5
-----------------------------------------------

I haven't changed the layout of the e-mails in any way, the above is exactly how it is displayed. Any ideas why? I'm a little loath to send this e-mail out as HTML.

~tg
 
I've never used AOL personally. What is the user interface for reading mail?

I'm wondering if it is possible to view the raw text of the message to see whether it's a display problem or whether AOL is performing some kind of transform on the data in-flight.

Just to ask a stupid random question, have you tried the email with a space between the pound-sign and the numbers in your currency amounts? Want the best answers? Ask the best questions: TANSTAAFL!
 
An intersting thought! I will give that a go and get back to you!

Thanks again.

~tg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top