Jump to content
Larry Ullman's Book Forums

Search the Community

Showing results for tags 'fuel calculator'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Single Editions
    • Modern Javascript: Develop and Design
    • The Yii Book
    • Effortless Flex 4 Development
    • Building a Web Site with Ajax: Visual QuickProject
    • Ruby: Visual QuickStart Guide
    • C++ Programming: Visual QuickStart Guide
    • C Programming: Visual QuickStart Guide
    • Adobe AIR: Visual QuickPro Guide
  • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (5th Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)
    • PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (3rd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (1st Edition)
  • PHP for the Web: Visual QuickStart Guide
    • PHP for the Web: Visual QuickStart Guide (5th Edition)
    • PHP for the Web: Visual QuickStart Guide (4th Edition)
    • PHP for the Web: Visual QuickStart Guide (3rd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (2nd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (1st Edition)
  • Effortless E-commerce with PHP and MySQL
    • Effortless E-Commerce with PHP and MySQL (2nd Edition)
    • Effortless E-Commerce with PHP and MySQL
  • PHP Advanced: Visual QuickPro Guide
    • PHP Advanced and Object-Oriented Programming: Visual QuickPro Guide (3rd Edition)
    • PHP 5 Advanced: Visual QuickPro Guide (2nd Edition)
    • PHP Advanced: Visual QuickPro Guide
  • MySQL: Visual QuickStart Guide
    • MySQL: Visual QuickStart Guide (2nd Edition)
    • MySQL: Visual QuickStart Guide (1st Edition)
  • Other
    • Announcements
    • Newsletter, Blog, and Other Topics
    • Forum Issues
    • Social

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Found 1 result

  1. Hello everyone. I understand most of the code for the fuel calculator in chapter 3 Exercise 9. It is designed to calculate fuel costs and time taken for journey. This is based upon the Distance, the Cost per gallon and the fuel efficiency which are entered before the form is submitted. , It is only one page and most of the code is straightforward. However I haven't yet worked out why the $_POST[name] is not equivalent to 'gallon_price' after the form is submitted, since this is the default value as defined in the function create_radio and no value for the $name is declared anywhere else. If someone could explain what $_POST[$name] equals BEFORE the form is submitted and AFTER the form is submitted and WHY, this would be great. Thank you <?php # Script 3.9 - calculator.php #4 // This function creates a radio button. // The function takes two arguments: the value and the name. // The function also makes the button "sticky". function create_radio($value, $name = 'gallon_price') { // Start the element: echo '<input type="radio" name="' . $name .'" value="' . $value . '"'; // Check for stickiness: if (isset($_POST[$name]) && ($_POST[$name] == $value)) { echo ' checked="checked"'; } // Complete the element: echo " /> $value "; } // End of create_radio() function. $page_title = 'Trip Cost Calculator'; include ('../includes/header.html'); // Check for form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Minimal form validation: if (isset($_POST['distance'], $_POST['gallon_price'], $_POST['efficiency']) && is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($_POST['efficiency']) ) { // Calculate the results: $gallons = $_POST['distance'] / $_POST['efficiency']; $dollars = $gallons * $_POST['gallon_price']; $hours = $_POST['distance']/65; // Print the results: echo '<h1>Total Estimated Cost</h1> <p>The total cost of driving ' . $_POST['distance'] . ' miles, averaging ' . $_POST['efficiency'] . ' miles per gallon, and paying an average of $' . $_POST['gallon_price'] . ' per gallon, is $' . number_format ($dollars, 2) . '. If you drive at an average of 65 miles per hour, the trip will take approximately ' . number_format($hours, 2) . ' hours.</p>'; } else { // Invalid submitted values. echo '<h1>Error!</h1> <p class="error">Please enter a valid distance, price per gallon, and fuel efficiency.</p>'; } } // End of main submission IF. // Leave the PHP section and create the HTML form: ?> <h1>Trip Cost Calculator</h1> <form action="calculator.php" method="post"> <p>Distance (in miles): <input type="text" name="distance" value="<?php if (isset($_POST['distance'])) echo $_POST['distance']; ?>" /></p> <p>Ave. Price Per Gallon: <span class="input"> <?php create_radio('3.00'); create_radio('3.50'); create_radio('4.00'); ?> </span></p> <p>Fuel Efficiency: <select name="efficiency"> <option value="10"<?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '10')) echo ' selected="selected"'; ?>>Terrible</option> <option value="20"<?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '20')) echo ' selected="selected"'; ?>>Decent</option> <option value="30"<?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '30')) echo ' selected="selected"'; ?>>Very Good</option> <option value="50"<?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '50')) echo ' selected="selected"'; ?>>Outstanding</option> </select></p> <p><input type="submit" name="submit" value="Calculate!" /></p> </form> <?php include ('../includes/footer.html'); ?>
×
×
  • Create New...