I'm trying to make a price calculator for my friend's car shipping company. It seemed like a simple thing to do but with my skills I'm having some problems. The calculator takes the distance from zipcode 1 to zipcode 2, and mutplies the total miles by .32 to get the base price. No problems here.

Then I need to see if both zip codes are part of a large metro area, if not there's a 30% increase in price. I got this working properly too.

The next step is to get the customer car type and additional costs depending on the car category. SS for small sedan, MS for a medium Sedan and so on.

I used a fairly plain JS to get the conditional drop down and assign the right values. When the form is submitted I seem to get the right calculation for the additional car cost - (7% for a medium sedan) but when I try to add this to the total price it doesn't calculate.

I'm also having a problem adding a checkbox value to the total. I get the correct value when the checkbox is checked but I need to sum all of them up.

Would anyone please be able to provide with some insight on how to add the total price for all the variables together?

Thank you for reading my post.

the code I have is below, You can see it not working at http://autotransbroker.com/calculator/calc.php :(

<html>
<head>
<script type="text/javascript" language="JavaScript"> function dropdownlist(listindex)
{
document.calculator.carmodel.options.length = 0;
switch (listindex)
{

case "acura" :
document.calculator.carmodel.options[0]=new Option("CL","MS");
document.calculator.carmodel.options[1]=new Option("Integra","MS");
document.calculator.carmodel.options[2]=new Option("10X","MSU");
document.calculator.carmodel.options[3]=new Option("JSX","SS");
document.calculator.carmodel.options[4]=new Option("ROX","SSU");
document.calculator.carmodel.options[5]=new Option("RL","MS");
document.calculator.carmodel.options[6]=new Option("RSX","MS");
document.calculator.carmodel.options[7]=new Option("SLX","LSU");
document.calculator.carmodel.options[8]=new Option("TL","MS");
document.calculator.carmodel.options[9]=new Option("TSX","MS");
document.calculator.carmodel.options[10]=new Option("Vigor","MS");
document.calculator.carmodel.options[11]=new Option("ZDX","SSU");

break;

case "alfa_romeo" :
document.calculator.carmodel.options[0]=new Option("164","MS");
document.calculator.carmodel.options[1]=new Option("8c","SS");
document.calculator.carmodel.options[2]=new Option("Spider","SS");

break;

case "Books" :
document.calculator.carmodel.options[0]=new Option("Select Sub-Category","");
document.calculator.carmodel.options[1]=new Option("College Books","College Books");
document.calculator.carmodel.options[2]=new Option("Engineering","Engineering");
document.calculator.carmodel.options[3]=new Option("Magazines","Magazines");
document.calculator.carmodel.options[4]=new Option("Medicine","Medicine");
document.calculator.carmodel.options[5]=new Option("References","References");

break;

}
return true;
}
</script>

</head>
<body>

<?php
include 'zips.php';
$zip1 = $_POST["zip1"];
$zip2 = $_POST["zip2"];
$car = $_POST["carmodel"];
$url = "origins=$zip1&destinations=$zip2&mode=driving&units=imperial&sensor=false";
$price = '0';
$area = '0';
$carprice = '0';



function curl_request($sURL,$sQueryString=null)
{
        $cURL=curl_init();
        curl_setopt($cURL,CURLOPT_URL,$sURL.'?'.$sQueryString);
        curl_setopt($cURL,CURLOPT_RETURNTRANSFER, TRUE);
        $cResponse=trim(curl_exec($cURL));
        curl_close($cURL);
        return $cResponse;
}
 
$sResponse=curl_request('http://maps.googleapis.com/maps/api/distancematrix/json', $url);
$oJSON=json_decode($sResponse);
if ($oJSON->status=='OK')
        $fDistanceInMiles=(float)preg_replace('/[^\d\.]/','',$oJSON->rows[0]->elements[0]->distance->text);
else
        $fDistanceInMiles=0;
 

?>
<br />

<br />


<?php 

     if (in_array($zip1, $zipcoderadius) && in_array($zip2, $zipcoderadius)) {
         $price = $fDistanceInMiles * .32;
		 $area = 0;
		 $carprice = '';
		 $total = $price + $area + $carcondition + $carprice;
		 
	 }
    else
	{
          $price = $fDistanceInMiles * .32;
		  $area = ($price / 10) * 3;
		  $carprice = '';
		  $total = $price + $area + $carcondition + $carprice;
	}
	
?>

<?php 
$carprice = '';
if ($car == SS) {
     $carprice = ($price / 100) * 7;
} elseif ($car == MS) {
    $carprice = ($price / 100) * 7;
} elseif ($car == LS) {
    $carprice = ($price / 100) * 13;
}
  elseif ($car == SSU) {
    $carprice = ($price / 100) * 13;
}
  elseif ($car == MSU) {
    $carprice = ($price / 100) * 16;
}
  elseif ($car == LSU) {
    $carprice = ($price / 100) * 20;
}
  elseif ($car == TWODLB) {
    $carprice = ($price / 100) * 20;
}
  elseif ($car == TWODSB) {
    $carprice = ($price / 100) * 20;
}
  elseif ($car == FOURDSB) {
    $carprice = ($price / 100) * 30;
}
  elseif ($car == FOURDLB) {
    $carprice = ($price / 100) * 30;
}
  elseif ($car == DUALY) {
    $carprice = ($price / 100) * 60;
}
  elseif ($car == SV) {
    $carprice = ($price / 100) * 60;
}
  elseif ($car == LV) {
    $carprice = ($price / 100) * 60;
}
  elseif ($car == EIGHTPASS) {
    $carprice = ($price / 100) * 60;
}
 elseif ($car == TWELVEPASS) {
    $carprice = ($price / 100) * 60;
}
 elseif ($car == L) {
    $carprice = ($price / 100) * 50;
}
 elseif ($car == LCL) {
    $carprice = ($price / 100) * 60;
}
 elseif ($car == SCL) {
    $carprice = ($price / 100) * 20;
}
 elseif ($car == MV) {
    $carprice = ($price / 100) * 20;
}
?>


<?php
if (isset($_POST['notrunning'])) {
 
   $carcondition = ($price / 100) * 15;
} 
?>

<table>
<tr>
<td>
<?php echo "Starting Zip: $zip1"; ?>
</td>
</tr>
<tr>
<td>
<?php echo "Destination Zip: $zip2"; ?>
</td>
</tr>
<tr>
<td>
<?php echo "Distance in Miles: $fDistanceInMiles" ?>
</td>
</tr>
<tr>
<td>
<?php echo "Estimated Price: $ $price" ?>

</td>
</tr>
<tr>
<td>
<?php echo "adding percent to price from area $area " ?>

</td>
</tr>

<tr>
<td>
<?php echo "Vehicle Selected : $car " ?>

</td>
</tr>

</tr>
<tr>
<td>
<?php echo "Car Price: $carprice " ?>

</td>
</tr>
<tr>
<td>
<?php echo "Car Condition: $carcondition " ?>

</td>
</tr>
<tr>
<td>
<?php echo "Total Price : $total " ?>

</td>
</tr>

</table>


<form id="calculator" name="calculator" action="calc.php" method="post">
<table>
<tr><td>
Pick up Zip Code: <input type="text" name="zip1" /><br />
</td>
<td>
Destination Zip Code: <input type="text" name="zip2" />
<br />
</td>
</tr>

<tr>
<td>Category :</td>
<td><select name="carmake" id="carmake" onChange="javascript: dropdownlist(this.options[this.selectedIndex].value);">
<option value="">Select Make</option>
<option value="acura">Acura</option>
<option value="alfa_romeo">Alfa Romeo</option>
</select></td>
</tr>

<tr>
<td>Sub Category :
</td>
<td><script type="text/javascript" language="JavaScript">
document.write('<select name="carmodel"><option value="">Select Sub-Category</option></select>')
</script>
<noscript><select name="carmodel" id="carmodel" >
<option value="">Select Sub-Category</option>
</select>
</noscript></td>
</tr>
<tr><td>
This vehicle does not run:
<input type="checkbox" name="notrunning" value="notrunning">
</td></tr>
<tr>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>
</body>
</html>

Recommended Answers

All 2 Replies

You are opening and closing the PHP tags quite a bit, This is not necessary for most of your code. The script is going to be processed from top of the file to bottom.

It appears that you are calculating the $total before you calculate the $carprice or $carcondition. In fact you are clearing $carprice right before you calculate the $total.

Also, since you obviously want $carprice to hold a number why are you clearing it with: $carprice = ''; instead of $carprice =0;? Syntacically PHP will allow this, but semantically you should initalize a variable with the data type it was meant to contain.

Clean up the first section and move it after the the last section.

// Take this section of code, clean it up and ....
<?php       
if (in_array($zip1, $zipcoderadius) && in_array($zip2, $zipcoderadius)) {
         $price = $fDistanceInMiles * .32;
	$area = 0;
	$total = $price + $area + $carcondition + $carprice;
} else {
         $price = $fDistanceInMiles * .32;
	$area = ($price / 10) * 3;
	$total = $price + $area + $carcondition + $carprice;	
} 
?>

// ... move it after this section

<?php
if (isset($_POST['notrunning'])) {
    $carcondition = ($price / 100) * 15;
} 
?>

Thank you for the quick reply, I did clean up the code as you suggest and moved the calculation to the bottom of the script and it works as intended now.

I'll try and clean up the opening/closing php tags too. I know if't not necessary but it helps me keep track of the code a bit better, I just don't have the experience to sort it all out in my head.

Thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.