Page 1 of 1

Got a problem - trying to calculate the php code !

Posted: Wed Jun 16, 2010 1:25 pm
by canadian_angel
The thing is I need to write a single script that allows the traveler to calculate how long it will take to travel a specified number of miles based on speed, number of stops, and weather conditions. I need to include code that requires the user to enter numeric values for miles and number of stops. I need to use a radio button that allows users to select good or bad weather. The predetermined miles per hour is 50mph. This is what I have so far, now I am stumped.

<form action="passenger_train.php" method="post">

Speed: <input type="radio" name="speed[]" value="50mph" /> 50 Mph
<br />
<p>Weather Conditions:</p>
<input type="radio" name="weather[]" value="wet" /> Wet
<input type="radio" name="weather[]" value="dry" /> Dry
<br />
Miles: <input type="text" name="miles" size="5" />
<br />
Stops: <input type="text" name="stops" size="5" />
<br />

<input type="submit" name="submit" value="Calculate" />
</form>

<?php
for($i = 0, $x = $nums['x_val'];

?>

Re: Got a problem - trying to calculate the php code !

Posted: Wed Jun 16, 2010 1:29 pm
by AbraCadaver
You don't need the arrays speed[], weather[] etc. in the form. As for the PHP, why the loop? Just do your calculation. Or is the math what you are having problems with?

Re: Got a problem - trying to calculate the php code !

Posted: Wed Jun 16, 2010 1:33 pm
by internet-solution
Another pointer - your form will post the form values to passenger_train.php. So you will have to put the calculation steps in passenger_train.php.

Re: Got a problem - trying to calculate the php code !

Posted: Wed Jun 16, 2010 1:55 pm
by canadian_angel
Yes, its the math part! I never was good at it and now I have to put it into code so that this form works.
I am cofused!

Re: Got a problem - trying to calculate the php code !

Posted: Wed Jun 16, 2010 2:00 pm
by AbraCadaver
canadian_angel wrote:Yes, its the math part! I never was good at it and now I have to put it into code so that this form works.
I am cofused!
Well if you travel 50 miles in 1 hour then you divide the distance (miles) by the speed to get the time. You then need to add the time for your stops so you multiply the stops by the duration of the stop. I hope this is an elementary or junior high school assignment.

passenger_train.php

Code: Select all

$duration = .5;  // stop duration in hours
$travel_time = ($_POST['miles'] / $_POST['speed']) + ($_POST['stops'] * $duration);
echo "Travel Time is $travel_time hrs.";

Re: Got a problem - trying to calculate the php code !

Posted: Wed Jun 16, 2010 2:10 pm
by canadian_angel
Thanks soooooo very much and yes it is!

So confused ! PHP CODE! I can't figure out whats missing.

Posted: Thu Jun 17, 2010 4:19 pm
by canadian_angel
Ok so this is the full problem, and I am sooooooo confused! I have to write php code for this- A passenger train averages a speed of 50 mph. However, each time the train stops another five minutes is added to the train’s traveling time. In addition, during bad weather the train can only average a speed of 40 mph. Write a script that allows the traveller to calculate how long it will take to travel a specified number of miles based on speed, number of stops, and weather conditions. I have to include code that requires the user to enter numeric values for miles and number of stops. And use a radio button that allows users to select good or bad weather,and this is what i have so far.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>Passenger Train</title>
</head>
<body>
<!-- Script 6.3 - passenger_train.php -->

Fill out form to determine time to required destination: <br />

<form action="passenger_train.php" method="post">
Speed: <input type="radio" value="50mph" />50 Mph
<br />
Weather Conditions:
<input type="radio" value="wet" />Wet
<input type="radio" value="dry" />Dry
<br />
Miles: <input type="text" name="miles" size="5" />
<br />
Stops: <input type="text" name="stops" size="5" />
<br />
<input type="submit" name="submit" value="Calculate" />
</form>
</body>
</html>
<?php // in case register_globals is disabled
$miles = $_POST['miles'];
$stops = $_POST['stops'];
$weather_type = $_POST['weather_type'];
$stops = $stops + ((10/60)*5);
$stops = ($miles + $stops + $weather) / 50;
$weather_type = 'wet' || 'dry';

// Validate the number of stops.
if (empty ($stops)) {
print '<p>Please enter the number of stops you wish to travel:</p>';
}
// Validate the number of miles.
if (empty ($miles)) {
}
print '<p>Please enter the number of miles you are traveling:</p>';

?>

Re: Got a problem - trying to calculate the php code !

Posted: Thu Jun 17, 2010 9:42 pm
by AbraCadaver
OK, so you have all the code. What you need to do is adjust the stop time ($duration) and have a radio button for the weather good/bad that send either 50 or 40.