Got a problem - trying to calculate the php code !

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
canadian_angel
Forum Commoner
Posts: 31
Joined: Fri Jun 11, 2010 3:13 pm

Got a problem - trying to calculate the php code !

Post 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'];

?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

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

Post 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.
canadian_angel
Forum Commoner
Posts: 31
Joined: Fri Jun 11, 2010 3:13 pm

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

Post 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!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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.";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
canadian_angel
Forum Commoner
Posts: 31
Joined: Fri Jun 11, 2010 3:13 pm

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

Post by canadian_angel »

Thanks soooooo very much and yes it is!
canadian_angel
Forum Commoner
Posts: 31
Joined: Fri Jun 11, 2010 3:13 pm

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

Post 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>';

?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply