DBookatay Posted January 6, 2012 Share Posted January 6, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/254453-help-with-an-array_sum-not-adding-correctly/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 6, 2012 Share Posted January 6, 2012 You need to initialize $surcharge to an empty array at the start of each new car - $surcharge = array(); Quote Link to comment https://forums.phpfreaks.com/topic/254453-help-with-an-array_sum-not-adding-correctly/#findComment-1304677 Share on other sites More sharing options...
DBookatay Posted January 6, 2012 Author Share Posted January 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254453-help-with-an-array_sum-not-adding-correctly/#findComment-1304679 Share on other sites More sharing options...
Labradoodle-360 Posted January 6, 2012 Share Posted January 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254453-help-with-an-array_sum-not-adding-correctly/#findComment-1304680 Share on other sites More sharing options...
DBookatay Posted January 6, 2012 Author Share Posted January 6, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/254453-help-with-an-array_sum-not-adding-correctly/#findComment-1304683 Share on other sites More sharing options...
Labradoodle-360 Posted January 6, 2012 Share Posted January 6, 2012 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); } Quote Link to comment https://forums.phpfreaks.com/topic/254453-help-with-an-array_sum-not-adding-correctly/#findComment-1304686 Share on other sites More sharing options...
DBookatay Posted January 6, 2012 Author Share Posted January 6, 2012 SOLVED: I'm actually upset at myself because of how simple the answer was.... if (!empty($surcharge)){ $surcharges = array_sum($surcharge); } else { $surcharges = 0; } Quote Link to comment https://forums.phpfreaks.com/topic/254453-help-with-an-array_sum-not-adding-correctly/#findComment-1304747 Share on other sites More sharing options...
Labradoodle-360 Posted January 6, 2012 Share Posted January 6, 2012 I'd personally suggest using the code I posted, I completely rewrote the block for you to be more efficient... Quote Link to comment https://forums.phpfreaks.com/topic/254453-help-with-an-array_sum-not-adding-correctly/#findComment-1304967 Share on other sites More sharing options...
DBookatay Posted January 6, 2012 Author Share Posted January 6, 2012 I'd personally suggest using the code I posted, I completely rewrote the block for you to be more efficient... Your right, I'm sorry. I didnt see that there was more code if I scrolled all the way down... Thanks for the help... Quote Link to comment https://forums.phpfreaks.com/topic/254453-help-with-an-array_sum-not-adding-correctly/#findComment-1304979 Share on other sites More sharing options...
Labradoodle-360 Posted January 6, 2012 Share Posted January 6, 2012 Not a problem, I'm glad you got it all worked out Quote Link to comment https://forums.phpfreaks.com/topic/254453-help-with-an-array_sum-not-adding-correctly/#findComment-1305087 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.