Jump to content

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


canadian_angel

Recommended Posts

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

     

      ?>

Link to comment
Share on other sites

<?php

if(isset($_POST['miles']))
{
    $miles = $_POST['miles'];
    $stops = $_POST['stops'];
    $condition = $_POST['condition'];
    $mph = ($condition=='stormy') ?  40 : 50;
    $total_minutes = ($miles/$mph)*60 + (5 * $stops);
    $hours   = floor($total_minutes/60);
    $minutes = floor($total_minutes%60);
    $result =  "To travel {$miles} mile(s) in {$condition} weather with {$stops} stop(s) would take approximately {$hours} hour(s) and {$minutes} minute(s).";
}

?>
<!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="test.php" method="post">
        <br />
        Miles: <input type="text" name="miles" size="5" value="<?php echo $miles; ?>" />
        <br />
        Stops: <input type="text" name="stops" size="5" value="<?php echo $stops; ?>" />
        <br />
        Weather Conditions:
        <input type="radio" name="condition" value="clear" checked="checked" /> Clear
        <input type="radio" name="condition" value="stormy" /> Stormy
        <br /><br />
        <input type="submit" name="submit" value="Calculate" />
        </form>
        <br /><br />
        <?php echo $result; ?>
    </body>
</html>

Link to comment
Share on other sites

I dont know the reasoning behind your mathematics in your script. In fact some of your code would never work because the syntax your trying to use doesnt exist, eg:

 

Incorrect Syntax. In fact since its after uyou do the calculations its completely useless.

$weather_type = 'wet' || 'dry';

 

These Radio tags have no name, no identifier. Every value you use in your form needs one.

<input type="radio" value="wet" />Wet
<input type="radio" value="dry" />Dry

 

These calculations dont make sense to me. 10/60? * 5? You need to brush up on your Maths, this is quite Basic stuff.

$stops = $stops + ((10/60)*5);
$stops = ($miles + $stops + $weather) / 50;

 

This will never be empty as your assigning it a value directly above. This is Illogical, Make sure when you right the code you have example values in your head, and you follow it as you code.

if (empty ($stops)) {

 

- Also, the code youve provided (the PHP part) will always execute, even on first visiting the form (which you dont want, since you want the person to fill out the form first..).

I think you need to run through some basic tutorials out on the net, as you meed to grasp the basic concepts of programming languages and logic systems.

 

Here is an example of what should be done (i dont reccommend handing this into your tutor, you will probably fail if you cannot explain the code fully - ie, if you dont understand what it all means and does).

 

PHP:

<?php

// FIRST: CHECK IF WE SHOULD 'PROCESS' OR 'DISPLAY' FORM

if(!isset($_POST['trainform'])){ // isset() - checks wether the given variable exists.
// Display Form:

?>
<!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 of this train is is 50MPH for Dry conditions and 40MPH for Wet.
        <br />
        Weather Conditions:
          <input type="radio" name="weather" value="good" />Dry
          <input type="radio" name="weather" value="bad" />Wet
          <br />
        Miles to Travel: <input type="text" name="miles" size="5" />
        <br />
        Number of Stops: <input type="text" name="stops" size="5" />
        <br />
        <input type="submit" name="submit" value="Calculate" />
        </form>
    </body>
</html>
<?php

}else{
// Otherwise, Process Form.

// Set this to null here, so if none of the if conditions are triggered it will remain null.
// We can then check if there were errors bu checking the value of this variable.
$error = null;

// Sanitize first - Always first. Save any errors to a string.
// (Note, '.=' Operator Adds the string to whatever is already inside that variable.)
if(!isset($_POST['weather']) || !isset('miles') || !isset('stops')){
	// All fields should technically exist, even if they have no data.
	$error .= 'There was an error either in the Form or in the transmission of the data<BR />';
}
if($_POST['weather'] != "good" && $_POST['weather'] != "bad"){
	// Since this is a radio button selection, it should always equal either good or bad.
	$error .= 'There was an error either in the Form or in the transmission of the data<BR />';
}
if($_POST['miles'] <= 0 || !is_numeric($_POST['miles'])){
	// miles either is less than or equal to 0 (Cant be negative or 0) or is not a number.
	$error .= 'Miles must be a Positive Number greater than 0.<BR />';
}
if($_POST['stops'] < 0 || !is_numeric($_POST['stops']){
	// Stops must not be negative, and must be a number
	$error .= 'Number of Stops must be a Positive Number (Can be 0).<BR />';
}

// Check if there were errors by checking the value of the error variable.
if($error != null){
	exit('<b>There were Errors:</b><br />'.$error);
}else{
	// Form data is Safe.

	// Define variables you can define, first. Easier to look for and change.
	$goodspeed = 50;
	$badspeed = 40;

	// Set Speed according to Weather.
	$speed = ($_POST['weather'] == "good")? $goodspeed : $badspeed;

	// Do the calculations.
	$minutes = ($stops * 5) + ($miles / ($speed / 60));

	// Single quotes are called 'Literal Strings', Variables dont expand automatically so you have to concatenate them inside the string using the . syntax:
	exit('Train took '.$minutes.' Minutes');
}
?>

 

This is probably a lot more advanced than what your teacher/tutor will be expecting. But if you learn stuff like this fast then you will excell in your course.

 

(Got bored and went overkill), Make sure you understand all this :P.

 

-cb-

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.