Two Trains Passing Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
elbarf
Forum Newbie
Posts: 5
Joined: Mon Oct 04, 2010 3:41 pm

Two Trains Passing Script

Post by elbarf »

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've gotten so far. This is a project in a book, I'm trying to teach myself to get more knowledge with PHP and am failing miserably. Any Takers?

<?php
$DisplayForm = TRUE;

if (isset($_POST['Submit'])) {
$traina = $_POST[''];
$trainb = $_POST[''];

if $traina, $trainb, $distance > 0 {
$DisplayForm = FALSE;
} else {
echo "<p>Cannot divide by 0.</p>\n";
$DisplayForm = TRUE;
}
}

$DistanceA = (($SpeedA / $SpeedB) * $Distance) /
(1 + ($SpeedA / $SpeedB));
$DistanceB = $Distance - $DistanceA;
$TimeA = $DistanceA / $SpeedB;

if ($DisplayForm) {
?>
<form name="TwoTrainsMoving" action="TwoTrains.php"
method="post">
<p>Speed of Train A: <input type="text" name="traina" value="<?php echo $traina; ?>" /> </p>
<p>Speed of Train B: <input type="text" name="trainb" value="<?php echo $trainb; ?>" /> </p>
<p>Distance Between Train A and Train B : <input type="text" name="distace" value="<?php echo $distance; ?>" /> </p>
<p><input type="reset" name="Number" value="Clear Form" />
<input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}

?>
Post Reply