displaying results
Posted: Tue Feb 19, 2013 10:15 pm
I am new at this and I am trying to display results from some equations. I am thinking that I need to use "function displayDistance" but I am not sure. Could someone please look at this and tell me if I am even on the right track?
My instructions are to: Create an all in one sticky form to solve the common "two trains are moving toward each other" word problem,. The form should have three inputs, all numbers greater than 0: the speed of Train A($SpeedA), the speed of train b($SpeedB) and the distance between the two trains($Distance). For this problem, you will need the following equations:
$DistanceA = (($SpeedA / $SpeedB) * $Distance) /
(1 + ($SpeedA / $SpeedB));
$DistanceB = $Distance - $DistanceA;
$TimeA = $DistanceA / $SpeedB;
In the preceding equations, $DistanceA and $DistanceB are the distances traveled by Trains A and B, respectively.; $TimeA and $TimeB are how long trains and and b traveled, respectively($Time A should equal $TimeB). If $SpeedA or $Speed B is allowed to be 0, php will display a division by zero not allowed" error.
This is what I have so far:
My instructions are to: Create an all in one sticky form to solve the common "two trains are moving toward each other" word problem,. The form should have three inputs, all numbers greater than 0: the speed of Train A($SpeedA), the speed of train b($SpeedB) and the distance between the two trains($Distance). For this problem, you will need the following equations:
$DistanceA = (($SpeedA / $SpeedB) * $Distance) /
(1 + ($SpeedA / $SpeedB));
$DistanceB = $Distance - $DistanceA;
$TimeA = $DistanceA / $SpeedB;
In the preceding equations, $DistanceA and $DistanceB are the distances traveled by Trains A and B, respectively.; $TimeA and $TimeB are how long trains and and b traveled, respectively($Time A should equal $TimeB). If $SpeedA or $Speed B is allowed to be 0, php will display a division by zero not allowed" error.
This is what I have so far:
Code: Select all
<!DOCTYPE>
<html>
<head>
<title>Two Trains</title>
</head>
<body>
<?php
function validateInput($data, $fieldName) {
global $errorCount;
if (empty($data))
{
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
}
else
{
$retval = trim($data);
$retval = stripslashes($retval);
}
return($retval);
}
function validateDistance($data, $fieldName)
{
global $errorCount;
if (empty($data))
{
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
}
else
{
$retval = trim($data);
$retval = stripslashes($retval);
$pattern = "/^[\w-]+(\.[\w-]+)*@"."[\w-]+(\.[\w-]+)*".
"(\.[[a-z]]{2,})$/i";
if (preg_match($pattern, $retval)==0)
{
echo "\"$fieldName\" division by zero not allowed.<br />\n;";
++$errorCount;
}
}
return($retval);
}
function displayForm($SpeedA, $SpeedB, $Distance)
{
?>
<h2 style = "text-align:center">Two Trains</h2>
<form name="Two Trains" action="TwoTrains.php"
method= "post">
<p>SpeedA: <input type="integer" name="SpeedA" value="<?php echo $SpeedA; ?>" /></p>
<p>SpeedB: <input type="integer" name="SpeedB" value="<?php echo $SpeedB; ?>" /></p>
<p>Distance: <input type="integer" name="Distance" value="<?php echo $Distance; ?>" /></p>
<p><input type="reset" value="Clear Form" />
<input type="submit" name="Submit" value="Submit" /></p>
</form>
<?php
}
$ShowForm = TRUE;
$errorCount = 0;
$SpeedA = "";
$SpeedB = "";
$Distance = "";
if (isset($_Post['Submit'])) {
$SpeedA =
validateInput($_POST['SpeedA'], "SpeedA");
$SpeedB =
validateInput($_POST['SpeedB'], "SpeedB");
$Distance =
validateInput($_POST['Distance'], "Distance");
if ($errorCount==0)
$ShowForm = FALSE;
else
$ShowForm = TRUE;
}
if ($ShowForm == TRUE)
{
if ($errorCount>0)
echo "<p>Please re-enter the speeds below.</p>\n";
displayForm($SpeedA, $SpeedB, $Distance);
}
else
{
$DistanceA = (($SpeedA / $SpeedB) * $Distance) /
(1 + ($SpeedA / $SpeedB));
echo "Distance Traveled By Train A = $DistanceA<br />";
$DistanceB = $Distance - $DistanceA;
echo "Distance Traveled By Train B = $DistanceB<br />";
$TimeA = $DistanceA / $SpeedA;
echo "Time Traveled By Train A = $TimeA<br />";
$TimeB = $DistanceB / $SpeedB;
echo "Distance Traveled By Train B = $TimeB<br />";
}
?>
</body>
</html>