Jump to content

Help with an "array_sum" not adding correctly


DBookatay

Recommended Posts

I am making a price sheet for extended warranties that my company sells and am having a problem with the math.

There is a base price of $1380, plus certain surcharges and certain exclusions. The exclusions are working properly, but something funky is happening with the surcharges...

 

This link more clearly explains the problem I am having:

http://www.carcityofdanbury.com/New/01/Resources/printable_list_Admin.php

 

Here is the code, then I'll explain the problem

// TMU
if ($row['TMU'] == "x") {
$extended = 'N/A';

// Salvage Title
} elseif ($row['cleantitle'] != "x") {
$extended = 'N/A';

// 150,000+ miles
} elseif ($row['mileage'] > "150000") {
$extended = 'N/A';

// Snowplow
} elseif ($row['ftr003'] == "x") {
$extended = 'N/A';

// Category 3
} elseif ($row['make'] == "Jaguar" || $row['make'] == "Land Rover") {
$extended = 'N/A';

// 10+ cyls
} elseif ($row['engine_cylinders'] > "8") {
$extended = 'N/A';

// Older than 13 years
} elseif ($year < date("Y")-12) {
$extended = 'N/A';

// Qualifies for warranty
} else {

if ($row['mileage'] > "99999" && $row['mileage'] < "125000") {$surcharge[] = 200;}
if ($row['mileage'] > "125000" && $row['mileage'] < "150001") {$surcharge[] = 400;}
if ($row['drivetrain'] == "AWD" || $row['drivetrain'] == "4x4") {$surcharge[] = 150;}
if ($row['fuel'] == "Diesel") {$surcharge[] = 100;}
if ($row['make'] == "Audi" || $row['make'] == "BMW" || $row['make'] == "Cadillac" || $row['make'] == "Infiniti" || $row['make'] == "Lexus" || $row['make'] == "Lincoln" || $row['make'] == "Mercedes-Benz" || $row['make'] == "Mini" || $row['make'] == "Saab" || $row['make'] == "Volvo") {$surcharge[] = 550;}
if (date("Y")-11 == $year) {$surcharge[] = 50;}
if (date("Y")-12 == $year) {$surcharge[] = 100;} 
if (date("Y")-13 == $year) {$surcharge[] = 200;}

if ($surcharge) {$surcharges = array_sum($surcharge);}
$extended = '$'.number_format(1380 + $surcharges);

}

 

The 9th vehicle on the list: 08 Hyundai Sante Fe SE the warranty price is correct, but the vehicle after, the 08 Honda Accord EX should be 1,380, yet its adding the surcharges from the the Hyundai, and it keeps repeating down the list.

 

Where is the error?

Link to comment
Share on other sites

You need to initialize $surcharge to an empty array at the start of each new car -

 

$surcharge = array();

 

 

Where? If I do this:

$surcharge = array();
if ($surcharge) {$surcharges = array_sum($surcharge);}
$extended = '$'.number_format(1380 + $surcharges);

 

it no longer calculates the surcharges, if I do this:

if ($surcharge) {$surcharges = array_sum($surcharge);}
$surcharge = array();
$extended = '$'.number_format(1380 + $surcharges);

I get the same problem as I posted.

Link to comment
Share on other sites

You'd want to define $surcharge above the if condition. You'll also want to change if ($surcharge) to if (!empty($surcharge)) to make sure there were actually item(s) added to the array.

 

I'm sorry, I am not understand this... If I do this:

$surcharge = array();
if (!empty($surcharge)){$surcharges = array_sum($surcharge);}
$extended = '$'.number_format(1380 + $surcharges);

none of the surchages get added...

 

Link to comment
Share on other sites

Here you go.

// TMU
if ($row['TMU'] == 'x')
$extended = 'N/A';
// Salvage Title
elseif ($row['cleantitle'] != 'x')
$extended = 'N/A';
// 150,000+ miles
elseif ($row['mileage'] > 150000)
$extended = 'N/A';
// Snowplow
elseif ($row['ftr003'] == 'x')
$extended = 'N/A';
// Category 3
elseif ($row['make'] == 'Jaguar' || $row['make'] == 'Land Rover')
$extended = 'N/A';
// 10+ cyls
elseif ($row['engine_cylinders'] > 
$extended = 'N/A';
// Older than 13 years
elseif ($year < date("Y")-12)
$extended = 'N/A';
// Qualifies for warranty
else
{
$surcharge = 0;
switch ($row['mileage'])
{
	case ($row['mileage'] > 99999):
		$surcharge += 200;
	break;
	case ($row['mileage'] > 99999 && $row['mileage'] < 125000)
		$surcharge += 200;
	break;
	case ($row['mileage'] > 125000 && $row['mileage'] < 150001)
		$surcharge += 400;
	break;
}
if ($row['drivetrain'] == 'AWD' || $row['drivetrain'] == '4x4')
	$surcharge += 150;
if ($row['fuel'] == 'Diesel')
	$surcharge += 100;
switch ($row['make'])
{
	case 'Audi':
	case 'BMW':
	case 'Cadillac':
	case 'Infiniti':
	case 'Lexus':
	case 'Lincoln':
	case 'Mercedes-Benz':
	case 'Mini':
	case 'Saab':
	case 'Volvo':
		$surcharge += 550;
	break;
}
if (date('Y') - 11 == $year)
	$surcharge += 50;
elseif (date('Y') - 12 == $year)
	$surcharge += 100;
elseif (date('Y') - 13 == $year)
	$surcharge += 200;

$extended = '$' . number_format(1380 + $surcharges);

}

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.