Jump to content
Larry Ullman's Book Forums

Leaderboard

Popular Content

Showing content with the highest reputation since 09/29/20 in all areas

  1. Ah! Kudos for figuring it out and thanks for sharing what the cause was!
    1 point
  2. Thanks for the nice words. Yeesh on this error message, though! That command creates four keys (or indexes): on user_id, on username, on email, and on the combination of username and pass. None of those seems to be 100 bytes! My hunch is it's the login key that's causing the problem. Try removing that from the command (and delete the comma after `UNIQUE (email)` and try it again.
    1 point
  3. I'm not sure I'm following exactly but just to confirm, is your server properly set up to send emails?
    1 point
  4. Thanks for the nice words! Yes, as a general security rule you don't want user-uploaded files in the web directory. It would make sense to create a new folder for each user and store their uploads in their own directory. Some OSes have limits on the number of files or folders than can be in a directory, so that's a problem you'll need to worry about should you get to a high level. To show, say, an image in the browser, you'd set the HTML src to something like image.php?id=X. The image.php script would identify the image to be served and output it. I forget if there's an example of that in this particular book but it's not that complicated.
    1 point
  5. Hi All, Here is my solution to Pursue exercises in CH3. I combined 4 questions into one script. If you could take a look at my code and correct if I am wrong, I would be reaaaaly appreciated! Here are the questions on the book: # 1. Change calculator.php so that it uses a constant in lieu of the hard-coded average speed of 65. # 2. Better yet, modify calculator.php so that the user can enter the average speed or select it from a list # of options. # 3. Update the output of calculator.php so that it displays the number of days and hours the trip will take, when the number of hours is greater than 24. # 4. As a more advanced trick, rewrite calculator.php so that the create_radio( ) function call is only in # the script once, but still creates three radio buttons. Hint: Use a loop. Here is my solution script: ( I highlighted the code for the solution ) <?php # Script 3.10 - calculator.php #5 - Pursue # 1. Change calculator.php so that it uses a constant in lieu of the hard-coded average speed of 65. # 2. Better yet, modify calculator.php so that the user can enter the average speed or select it from a list # of options. # 3. Update the output of calculator.php so that it displays the number of days and hours the trip will take, # when the number of hours is greater than 24. # 4. As a more advanced trick, rewrite calculator.php so that the create_radio( ) function call is only in # the script once, but still creates three radio buttons. Hint: Use a loop. # # // 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. // This function calculates the cost of the trip. // The function takes three arguments: the distance, the fuel efficiency, and the price per gallon. // The function returns the total cost. function calculate_trip_cost($miles, $mpg, $ppg) { // Get the number of gallons: $gallons = $miles / $mpg; // Get the cost of those gallons: $dollars = $gallons * $ppg; // Return the formatted cost: return number_format($dollars, 2); } // End of calculate_trip_cost() function. $page_title = 'Trip Cost Calculator'; include ('includes/header.html'); // PURSUE 1 - Define the constant for average speed: define ('AVERAGE_SPEED', 65); // Check for form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Minimal form validation: if (isset($_POST['distance'], $_POST['gallon_price'], $_POST['efficiency'], $_POST['average_speed']) && is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($_POST['efficiency']) && is_numeric($_POST['average_speed'])) { // Calculate the results: $cost = calculate_trip_cost($_POST['distance'], $_POST['efficiency'], $_POST['gallon_price']); //$hours = $_POST['distance'] / AVERAGE_SPEED; $hours = $_POST['distance'] / $_POST['average_speed']; // PURSUE 3 - Calculate days and modulus $days = $hours / 24; $days_modulus = $days % 24; // PURSUE 3 - Print the results, using Days and Hours if ( $hours <= 24) { 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 $' . $cost . '. If you drive at an average of ' . $_POST['average_speed'] . ' miles per hour, the trip will take approximately ' . number_format($hours, 2) . ' hours.</p>'; } else { 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 $' . $cost . '. If you drive at an average of ' . $_POST['average_speed'] . ' miles per hour, the trip will take approximately ' . number_format($days, 2) . ' days and ' . $days_modulus . ' hours.</p>'; } } else { // Invalid submitted values. echo '<h1>Error!</h1> <p class="error">Please enter a valid distance, price per gallon, fuel efficiency and average speed.</p>'; } } // End of main submission IF. // Leave the PHP section and create the HTML form: ?> <h1>Trip Cost Calculator</h1> <form action="" 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 // PURSUE 4 the create_radio( ) function call is only in the script once // Step 1: create an array to hole the three radio button values: $radio_values = array('3.00', '3.50', '4.00', '5.00'); // Step 2: use a foreach loop to loop through the array: foreach ($radio_values as $radio_value) { create_radio($radio_value); } //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> <!-- // PURSUE 2 - Let user enter average speed: (Select from a list of options is not implemented here) --> <p>Average Speed: <input type="text" name="average_speed" value="<?php if (isset($_POST['average_speed'])) echo $_POST['average_speed']; ?>"></p> <p><input type="submit" name="submit" value="Calculate!" /></p> </form> <?php include ('includes/footer.html'); ?>
    1 point
  6. Two primary issues, one minor: - (Minor) You call the function with echo but the function itself prints, so this is redundant. - You really should use global variables in a function, for the most part. Functions should be passed the data they need.
    1 point
  7. I'm not sure I follow exactly what you're asking but I'll try to answer as best as I can... First, a constant would be better than using a hardcoded number--65--that has no context and can easily be lost within the code. The constant allows you to "label" the value and makes it easier and more obvious to change down the line. Second, the next prompt suggests taking the average speed as user input instead of using the hardcoded number or a constant. So you'd just repeat what the calculator does with distance but for the average speed.
    1 point
  8. Thanks for the nice words! It is appreciated! Unfortunately I'm not sure what's going on with that error message. It looks like it's suggesting that there's a problem with order_contents.print_id as a PRIMARY KEY but it's not a PRIMARY KEY and there's nothing in the SQL you posted that makes it a PRIMARY KEY. I've not seen this before. I'd double-check that the created database in phpMyAdmin matches the description put forth in the book. Thanks again!
    1 point
  9. Thanks Larry, I got it; that way these two variables $days and $days_modulus: $days = $hours / 24; $days_modulus = $days % 24; are not needed, which makes the code concise. I moved them into the 'else' conditional: } else { // $hours > 24 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 $' . $cost . '. If you drive at an average of ' . $_POST['average_speed'] . ' miles per hour, the trip will take approximately ' . number_format( $hours / 24, 2) . ' days and ' . ($hours / 24) % 24 . ' hours.</p>'; ------------- Here is the updated full script: <?php # Script 3.10 - calculator.php #5 - Pursue # 1. Change calculator.php so that it uses a constant in lieu of the hard-coded average speed of 65. # 2. Better yet, modify calculator.php so that the user can enter the average speed or select it from a list # of options. # 3. Update the output of calculator.php so that it displays the number of days and hours the trip will take, # when the number of hours is greater than 24. # 4. As a more advanced trick, rewrite calculator.php so that the create_radio( ) function call is only in # the script once, but still creates three radio buttons. Hint: Use a loop. # # // 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. // This function calculates the cost of the trip. // The function takes three arguments: the distance, the fuel efficiency, and the price per gallon. // The function returns the total cost. function calculate_trip_cost($miles, $mpg, $ppg) { // Get the number of gallons: $gallons = $miles / $mpg; // Get the cost of those gallons: $dollars = $gallons * $ppg; // Return the formatted cost: return number_format($dollars, 2); } // End of calculate_trip_cost() function. $page_title = 'Trip Cost Calculator'; include ('includes/header.html'); // PURSUE 1 - Define the constant for average speed: define ('AVERAGE_SPEED', 65); // Check for form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Minimal form validation: if (isset($_POST['distance'], $_POST['gallon_price'], $_POST['efficiency'], $_POST['average_speed']) && is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($_POST['efficiency']) && is_numeric($_POST['average_speed'])) { // Calculate the results: $cost = calculate_trip_cost($_POST['distance'], $_POST['efficiency'], $_POST['gallon_price']); //$hours = $_POST['distance'] / AVERAGE_SPEED; $hours = $_POST['distance'] / $_POST['average_speed']; // PURSUE 3 - Output displays the number of days and hours the trip will take if ( $hours <= 24) { 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 $' . $cost . '. If you drive at an average of ' . $_POST['average_speed'] . ' miles per hour, the trip will take approximately ' . number_format($hours, 2) . ' hours.</p>'; } else { // $hours > 24 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 $' . $cost . '. If you drive at an average of ' . $_POST['average_speed'] . ' miles per hour, the trip will take approximately ' . number_format( $hours / 24, 2) . ' days and ' . ($hours / 24) % 24 . ' hours.</p>'; } } else { // Invalid submitted values. echo '<h1>Error!</h1> <p class="error">Please enter a valid distance, price per gallon, fuel efficiency and average speed.</p>'; } } // End of main submission IF. // Leave the PHP section and create the HTML form: ?> <h1>Trip Cost Calculator</h1> <form action="" 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 // PURSUE 4 - the create_radio( ) function call is only in the script once // Step 1: create an array to hold the three/four/... radio button values: $radio_values = array('3.00', '3.50', '4.00', '5.00'); // Step 2: use a foreach loop to loop through the array: foreach ($radio_values as $radio_value) { create_radio($radio_value); } //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> <!-- // PURSUE 2 - Let user enter average speed: (Select from a list of options is not implemented here) --> <p>Average Speed: <input type="text" name="average_speed" value="<?php if (isset($_POST['average_speed'])) echo $_POST['average_speed']; ?>"></p> <p><input type="submit" name="submit" value="Calculate!" /></p> </form> <?php include ('includes/footer.html'); ?>
    1 point
×
×
  • Create New...