Jump to content

User form failing miserably...Involves Basic Math


idontknowphp

Recommended Posts

  :-[

 

Basically this is a pretty straight forward application. The values are all best on a set of conditional statements held in a function and when the user presses submit, it SHOULD calculate the form....However, this is not the case, for some reason it keeps returning 0. I have tried and tried and tried....to no avail. I have attached the files so you can sort through a little easier as there are a good amount of lines.

 

 

Thanks!

 

[attachment deleted by admin]

Ok, that is fine....Here is the code:

 

index.php - HTML for the user form (not styled, just the framework)

<? require('functions.php'); ?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" name="ezShipForm">
  <!-- Begin Business & Contact Info --->
  <input name="company" type="text" placeholder="Company"><br>
  <input name="address" type="text" placeholder="Address"><br>
  <input name="city" type="text" placeholder="City"><br>
  <select name="state">
    <option value="0">State</option>
    <?php echo generateStateOptions($stateList); ?>
  </select><br>
  <input name="zipcode" type="text" placeholder="Zip Code"><br>
  <input name="contactPhone" type="text" placeholder="Contact Phone"><br>
  <input name="contactFax" type="text" placeholder="Contact Fax"><br>
  <input name="contactName" type="text" placeholder="Contact Name"><br>
  <input name="contactEmail" type="text" placeholder="Contact E-mail"><br>
  <!-- End Business & Contact Info ---> 
  <!-- Begin Calculations --->
  <input name="fuelPrice" type="text" placeholder="Today's Fuel Rate">
  http://www.eia.gov/oog/info/wohdp/diesel.asp<br>
  <select name="trailerType">
    <option value="Flat Bed">Flatbed</option>
    <option value="Step Deck">Step Deck</option>
<option value="Van">Van</option>
    <option value="Refer">Refer</option>
    <option value="RGN/Lo-Boy">RGN/Lo-Boy</option>
  </select>  <br>
  <input name="pickUpDate" type="text" placeholder="MM/DD/YYYY"><br>
  <input name="startingCity" type="text" placeholder="Starting City"><br>
  <select name="startingState">
    <option value="0">State</option>
    <?php echo generateStateOptions($stateList); ?>
  </select><br>
  <input name="destinationCity" type="text" placeholder="Destination City"><br>
  <select name="destinationState">
    <option value="0">State</option>
    <?php echo generateStateOptions($stateList); ?>
  </select><br>
  <input name="loadedMiles" type="text" placeholder="Loaded Miles">http://www.truckmiles.com/<br>
  <select name="loadType">
    <option value="Full">Full</option>
    <option value="Partial">Partial</option>
  </select><br>
  <select name="loadSize">
    <option value="Yes">Oversized</option>
    <option value="No">Not Oversized</option>
  </select><br>
  <input name="loadLength" type="text" placeholder="Load Length">Ex: 120in = 10ft<br>
  <input name="loadWeight" type="text" placeholder="Weight">Ex: 10500lbs = 10.5<br>
  <input name="loadWidth" type="text" placeholder="Load Width">in inches<br>
  <input name="loadHeight" type="text" placeholder="Load Height">in inches<br>
  <select name="tarped">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select><br>
  <select name="twicCardReq">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select><br>
  <input name="permits" type="text" value="0">If yes, How many states?<br>
  <input name="pilotCars" type="text" value="0">If yes, How many?<br>
  <input name="loadDescription" type="text" placeholder="Brief Load or Product Description"><br>
  <input name="comments" type="text" placeholder="Other Comments"><br>
  <input name="total" type="text"><br>
  <input name="submit" type="submit" value="Submit"> 
  <input name="overWidth102" type="hidden">
  <input name="overWidth120" type="hidden">
  <input name="overWidth144" type="hidden">
  <input name="overWidth168" type="hidden">
  <input name="overLength52" type="hidden">
  <input name="overLength56" type="hidden">
  <input name="overLength58" type="hidden">
  <input name="overLength60" type="hidden">
  <input name="overLength62" type="hidden">
  
</form>
<?php 
if (isset($_POST['submit']))
{
	//Call the 'performCalculation' Function To Get Total Coat
	performCalculation();
}
?>
</body>
</html>

 

functions.php

<?php
function performCalculation()
{
//Declare User Form Variables
$company = $_POST['company']; 
$address = $_POST['address']; 
$city = $_POST['city']; 
$state = $_POST['state']; 
$zipcode = $_POST['zipcode']; 
$contactPhone = $_POST['contactPhone']; 
$contactFax = $_POST['contactFax']; 
$contactName = $_POST['contactName']; 
$contactEmail = $_POST['contactEmail']; 
$fuelPrice = $_POST['fuelPrice']; 
$trailerType = $_POST['trailerType']; 
$pickUpDate = $_POST['pickUpDate']; 
$startingCity = $_POST['startingCity']; 
$startingState = $_POST['startingState']; 
$destinationCity = $_POST['destinationCity']; 
$destinationState = $_POST['destinationState']; 
$loadedMiles = $_POST['loadedMiles']; 
$loadType = $_POST['loadType']; 
$loadSize = $_POST['loadSize']; 
$loadLength = $_POST['loadLength']; 
$loadWeight = $_POST['loadWeight']; 
$loadWidth = $_POST['loadWidth']; 
$loadHeight = $_POST['loadHeight']; 
$tarped = $_POST['tarped']; 
$twicCardReq = $_POST['twicCardReq']; 
$permits = $_POST['permits']; 
$pilotCars = $_POST['pilotCars']; 
$loadDescription = $_POST['loadDescription']; 
$comments = $_POST['comments']; 
$overWidth102 = $_POST['overWidth102']; 
$overWidth120 = $_POST['overWidth120']; 
$overWidth144 = $_POST['overWidth144']; 
$overWidth168 = $_POST['overWidth168']; 
$overLength52 = $_POST['overLength52']; 
$overLength56 = $_POST['overLength56']; 
$overLength58 = $_POST['overLength58']; 
$overLength60 = $_POST['overLength60']; 
$overLength62 = $_POST['overLength62']; 
$total = $_POST['total'];

//Declare Calculation Variables
$mainRate = ($fuelPrice * 0.166 + 1.06) * 1.25;
$mileRate = $loadedMiles * $mainRate;
$permitRate = $permits * 50;
$pilotCarRate = ($pilotCars * 1.25 * $loadedMiles);
$flatBedRate = $subTotal;
$overSizeRate = $overWidth102 + $overWidth120 + $overWidth144 + 
				$overWidth168 + $overLength52 + $overLength56 + 
				$overLength58 + $overLength60 + $overLength62;


//Surcharge Calculation
if ($loadedMiles < 500) { 
	$surcharge = (500 - $loadedMiles) * .25 * $mainRate;
}

//Load Rate Calculation
if ($loadLength > $loadWeight) {	
	$loadRate = $loadLength * $loadedMiles * ($mainRate / 34);
} else {
	$loadRate = $loadWeight * $loadedMiles * ($mainRate / 34);
}

//Tarped Rate Calculation
if ($tarped == "Yes") {
	$tarpRate = 100;
} else {
	$tarpRate = 0;
}

//Sub Total Calculation
if ($loadSize == "Full") {
	$subTotal = $loadedmiles + $surcharge + $tarpRate + $permitRate + $pilotCarRate; 
} else {
	$subTotal = $surcharge + $loadRate + $tarpRate + $permitRate;
}

//Step Deck Rate Calculation
if ($trailerType == "Step Deck") {
	$trailerRate = $subTotal * .02;	
} 

//Van Rate Calculation
if ($trailerType == "Van") {
	$trailerRate = $subTotal * .02;	
}

//Refer Rate Calculation
if ($trailerType == "Refer") {
	$trailerRate = $subTotal * .04;	
}

//RGN/Lo-Boy Rate Calculation
if ($trailerType == "RGN/Lo-Boy") {
	$trailerRate = $subTotal * .10;	
}

//Over Width Rate Calculations
if ($loadWidth > 102) {
	$overWidthRate = $loadedMiles * .2;
}
if ($loadWidth > 120) {
	$overWidthRate = $loadedMiles * .1;
}
if ($loadWidth > 144) {
	$overWidthRate = $loadedMiles * .1;
}
if ($loadWidth > 168) {
	$overWidthRate = $loadedMiles * .1;
}

//Over Length Rate Calculations
if ($loadLength > 52) {
	$overWidthRate = $loadedMiles * .14;
}
if ($loadLength > 56) {
	$overWidthRate = $loadedMiles * .07;
}
if ($loadLength > 58) {
	$overWidthRate = $loadedMiles * .07;
}
if ($loadLength > 60) {
	$overWidthRate = $loadedMiles * .07;
}
if ($loadLength > 62) {
	$overWidthRate = $loadedMiles * .07;
}

//	Total Rate Calculation
if ($loadedMiles !== 0) {
	$total = $trailerRate + $overSizeRate;
	echo $total;
} else {
	$total = 0;
}


}



//Generate List of States 
$stateList = array ('alabama'=>"AL",
'Alaska'=>"AK",
'Arizona'=>"AZ",
'Arkansas'=>"AR",
'California'=>"CA",
'Colorado'=>"CO",
'Connecticut'=>"CT",
'Delaware'=>"DE",
'District Of Columbia'=>"DC",
'Florida'=>"FL",
'Georgia'=>"GA",
'Hawaii'=>"HI",
'Idaho'=>"ID",
'Illinois'=>"IL",
'Indiana'=>"IN",
'Iowa'=>"IA",
'Kansas'=>"KS",
'Kentucky'=>"KY",
'Louisiana'=>"LA",
'Maine'=>"ME",
'Maryland'=>"MD",
'Massachusetts'=>"MA",
'Michigan'=>"MI",
'Minnesota'=>"MN",
'Mississippi'=>"MS",
'Missouri'=>"MO",
'Montana'=>"MT",
'Nebraska'=>"NE",
'Nevada'=>"NV",
'New Hampshire'=>"NH",
'New Jersey'=>"NJ",
'New Mexico'=>"NM",
'New York'=>"NY",
'North Carolina'=>"NC",
'North Dakota'=>"ND",
'Ohio'=>"OH",
'Oklahoma'=>"OK",
'Oregon'=>"OR",
'Pennsylvania'=>"PA",
'Rhode Island'=>"RI",
'South Carolina'=>"SC",
'South Dakota'=>"SD",
'Tennessee'=>"TN",
'Texas'=>"TX",
'Utah'=>"UT",
'Vermont'=>"VT",
'Virginia'=>"VA",
'Washington'=>"WA",
'West Virginia'=>"WV",
'Wisconsin'=>"WI",
'Wyoming'=>"WY");

//Generate State Options for User Forms
function generateStateOptions($array)
{
	$string = '';
	foreach($array as $k => $v)
{
	$string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n";
}
	return $string;
}


//End of file
?>

It's returning 0 because you're not actually returning any value from your performCalculation function.  Functions have their own, unique scope - what happens within them won't be 'seen' by the rest of the script unless you return a value from the function, and assign that return value to a variable in the parent scope, like so:

 

function performCalculation()
{
   // bunch of code

   return $total;
}

// main script

$myTotal = performCalculation();

Though that made sense, I am a little confused in how i will make it all work together as a whole and how to properly call the function, can you either give me a few more hints or point me toward some literature so i can figure this out?

With the exception of the superglobals ($_GET, $_POST, $_COOKIE, $_SERVER, etc.), you would need to pass the values of the variables to the function for them to be available for use within the function.

 

$var_one = 10;
$var_two = 20;

// wrong way
function MULTIPLY() {
$result = ($var_one * $var_two );
return $result;
}
echo MULTIPLY();  // Produces 'Undefined Variable' notices. Variable values are not available within the function.

// right way
function MULTIPLY_PROPERLY( $first_argument, $second_argument ) {
$result = ( $first_argument * $second_argument );
return $result;
}
echo MULTIPLY_PROPERLY( $var_one, $var_two ); // Returns 200 because now the values of the variables are passed to the function as arguments 

 

Well, though you explained it nicely, I am having a lot of problems....The main one being I am trying something that is quite difficult for my skill level. I just don;t understand how to make it all work together in my application....I have tried everything and I think all i have done is ruin the code.

I reverted back to the original code....for starters.

 

Well, this is based off of an excel file that I am trying to convert over to a php application...

 

I attached it in a zip so you can see it working...

 

That excel file will answer all the questions....I already converted all of the formulas into PHP conditionals, I just don;t know how to make the form functional...

 

Again, once you see the form in action it will make plenty of sense

 

Thanks again!

 

[attachment deleted by admin]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.