Page 1 of 1

displaying results

Posted: Tue Feb 19, 2013 10:15 pm
by se123
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:

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" />&nbsp;
	&nbsp;<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>

Re: displaying results

Posted: Thu Feb 21, 2013 7:16 pm
by mecha_godzilla
Hi,

Just a couple of points:

1. There's no displayDistance() function in your code - did you mean that you need help to create a new function called displayDistance() or do you already have that function in another part of your code?

2. If you have been given "instructions" in order to solve this problem, that suggests that this is some kind of class assignment. If this is the case then the majority of the forum users here probably won't want to answer your question because they would be completing your assignment for you - this is nothing personal of course, because it's ultimately in your best interest for you to find out why your script doesn't work.

That said, I don't want to be unhelpful so I'll just say that the best approach to any problem is to break it down into individual components and get them working in isolation before trying to combine them i.e.

a. Display a form
b. Return back to the same page when you submit the form
c. Capture the form values somewhere in your script
d. Validate those values
e. Display an error message if the validation checks aren't passed
f. Do something with the values if they validate correctly
g. Display the results

At the moment your script can do a) and b) so the next steps are for you to work out why your form values aren't being captured correctly.

HTH,

Mecha Godzilla