Larry Posted September 11, 2011 Share Posted September 11, 2011 This is just a note to say that I haven't officially created any "Review and Pursue" threads in this forum yet as I didn't know what, exactly, readers would want. So feel free to post your questions as you have them and I'll answer them as they come. In time I may then shuffle things around to create an organized "Review and Pursue" system. Thanks for your interest in the book! Link to comment Share on other sites More sharing options...
old.graham Posted January 11, 2012 Share Posted January 11, 2012 I bought your new book recently after the cover on edition2 started to fall apart. You may be pleased to know that the book helped me (and the group I worked with) to gain a very high mark (over 90%) in our final BSc degree project a few years ago. I like the review and pursue sections at the end of each chapter since I quickly find out whether what I have read has actually sunk in, although I tend to start exploring coding possibilities and lose focus on the original questions. That said, although all my php, css etc always works (eventually!), I often wonder if there were better ways of completing the exercises than the methods I have used. Given the nature of coding, there is often no right or wrong way to solve problems, but perhaps worked examples might be useful? Since this would mean a lot more work for you, maybe others would feel able to offer solutions to the questions? Perhaps set up as Answers to Chapter x? Link to comment Share on other sites More sharing options...
Larry Posted January 11, 2012 Author Share Posted January 11, 2012 Thanks for the nice words on the book. It is appreciated. Congrats to you and the group on getting some a high mark on your final project. That's excellent. You are absolutely correct that there are many different ways to do the same thing, some of them better than others (even marginally so). I haven't created answers to any of the prompts, so have nothing to share, but I'm sure others will share as they have answers of their own. Feel free to share yours, too, and I and others will provide feedback. Link to comment Share on other sites More sharing options...
old.graham Posted May 16, 2012 Share Posted May 16, 2012 Sorry to be so long in coming back, but I got waylaid by a project for a client that just grew and grew. Here is the code I wrote in answer to Chapter1 Review and Pursue. Hope this helps. Should be another installment coming up once I put my PHP hat on again. <!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" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Review Chapter 1</title> </head> <body> <?php #review page34 chapter1 $number = 10.111; //some variables $number2 = 20.2; $number3 = 30.3; $total = $number*$number2*$number3; //do things with variables $total1 = number_format($total, 2); //format the result to 2 decimal places define('NAME', 'Muggins'); //some constants define('VALUE', 'Number1'); define('VALUE2', 'Number2'); define('VALUE3', 'Number3'); //bit of HTML: a table echo '<table border="1"> <tr> <td colspan="3" border="1">' . NAME . '</td> <tr> <td>' . VALUE. '</td><td>' . VALUE2 . '</td><td>' . VALUE3 . '</td> </tr> <tr> <td>' . $number . '</td><td>' . $number2 . '</td><td>' . $number3 . '</td> </tr> </table>'; echo "\n<hr /><br />Multiply all the numbers to get a total of: <b>$total1</b>"; echo "\n<hr /><br />" . 'The number rounded up (ceil): <b>' . ceil($total). '</b>'; echo "\n<hr /><br />" . 'The number rounded down (floor): <b>' . floor($total). '</b>'; ?> </body> </html> Link to comment Share on other sites More sharing options...
Larry Posted May 18, 2012 Author Share Posted May 18, 2012 Thanks for sharing that. It looks good. A couple of thoughts: - I'd be a bit more consistent with the variable names. You use $number, then $number2, $number3, but then $total, $total1. Same goes for the constants. - Great use of concatenation, arithmetic, and function calls. - Minor point, but your last two echo statements might as well entirely be in double quotes with the function call concatenated in (visually odd, and potentially parse-error inducing to switch quote types within the same line). - I'd go ahead and apply number_format() inline with the echo, too. Nice job and thanks again for sharing! Link to comment Share on other sites More sharing options...
old.graham Posted June 10, 2012 Share Posted June 10, 2012 Thanks for the comments and for taking the time to reply. The double quotes was just silly of me. I wish I had thought of using the number _format that way. Just good style from a pro. I shall take on board the variable names comments. I have now got some more. The code for the ch 2 review and pursue is getting kind of big for posting here but I shall await your feedback on this. I suppose if it is just copied and pasted into and IDE it will be more readable. I hope this may be of some use to someone. I await your feedback with trepidation. PHP version? <!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" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>PHP Version</title> </head> <body> <?php #script chapter2 review and pursue page 73 phpinfo();//gives all details of php installed echo '<br /> PHP version is ' . PHP_VERSION; //just the version ?> </body> </html> All the rest of the form and handler: pursue_form.html <!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" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>HTML Feedback Form</title> <style type="text/css" title="text/css" media="all" > label,legend{ font-weight: bold; color: #300acc; } form{ background-color: #cccccc; } </style> </head> <body> <!--script 2.1 form.html--> <form action="handle_pursue_form.php" method="post"> <fieldset><legend>Please enter your information in the form below:</legend> <p><label>Please complete all entries.</label></p> <p><label>First Name: <input type="text" name="fname" size="20" maxlength="40" /></label></p> <p><label>Last Name: <input type="text" name="lname" size="20" maxlength="40" /></label></p> <p><label>Email address: <input type="text" name="email" size="40" maxlength="60" /></label></p> <p><label for="gender">Gender: </label> <input type="radio" name="gender" value="male" /> Male <input type="radio" name="gender" value="female" /> Female</p> <p><label>Age: <select name="age"> <option value="0-24">Under 25</option> <option value="25-64">Between 30 and 60</option> <option value="65+">Over 60</option> </select></label></p> <p><label>How many times have you used the garage? <select name="use"> <option value="1">First time</option> <option value="<6">2 to 5 times</option> <option value=">5">More than 5 times</option> </select></label></p> <p><label>How did we do? (choose one only): </label></p> <p><input type="checkbox" name="quality[]" value="poor" />Poor</p> <p><input type="checkbox" name="quality[]" value="average" />Average</p> <p><input type="checkbox" name="quality[]" value="good" />Good</p> <p><input type="checkbox" name="quality[]" value="great" />Great</p> <p><input type="checkbox" name="quality[]" value="excellent" />Excellent</p> <p><label>What work did we do on your last visit? Choose all that apply: </label></p> <p><input type="checkbox" name="service[]" value="mot" />MOT</p> <p><input type="checkbox" name="service[]" value="mot repairs" />MOT Repairs</p> <p><input type="checkbox" name="service[]" value="air con recharge" />Recharge Air Conditioning</p> <p><input type="checkbox" name="service[]" value="tyres" />Tyres</p> <p><input type="checkbox" name="service[]" value="car servicing" />Car Servicing</p> <p><input type="checkbox" name="service[]" value="other work" />Other Work</p> <p><label>Any comments? What can we improve on? <textarea name="comments" rows="3" cols="40"></textarea></label></p> </fieldset> <p align="center"><input type="submit" value="Submit Information" /></p> </form> </body> </html> handle_pursue_form.php <!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" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Form Feedback</title> <style type="text/css" title="text/css" media="all" > label,legend{ font-weight: bold; color: #300acc; } form{ background-color: #cccccc; } .error{ color: #C00; font-weight: bold; } </style> </head> <body> <?php #script page 73 pursue. I have concatenated most of the page 73 questions in this one script //validate the gender: //rewrite gender conditional //is this what was asked for or have I misunderstood the question? //was anything selected and was it male or female if(isset($_POST['gender']) && (($_POST['gender']=='male')) ){//value checked AND male //value checked $gender = $_POST['gender'];//male $title = 'Mr.'; }elseif(isset($_POST['gender']) && $_POST['gender']=='female'){//value checked AND female $gender = $_POST['gender']; //female $title = 'Ms.'; }else{//radio button not checked $gender = NULL; $title_error = '<p class="error">Gender should be either male or female.</p>';//error message } //validate the first name if (!empty($_POST['fname'])){//first name entered $fname = $_POST['fname']; }else{//nothing entered in the box $fname = NULL; $fname_error = '<p class="error">You did not enter your first name.</p>'; //error message } //validate the last name if (!empty($_POST['lname'])){//last name entered OK $lname = $_POST['lname']; }else{//no last name entered $lname = NULL; $lname_error = '<p class="error">You did not enter your last name.</p>';//error message } //validate number of times garage used //couldn't resist trivial use of switch function if ((isset($_POST['use']))){ $use = $_POST['use']; switch ($use) { case '1': $greeting = 'Nice to see a new customer '; break; case '<6': $greeting = 'Great to see you back again'; break; case '>5': $greeting = 'Welcome back '; break; default: $greeting_error = '<p class="error">You have not told us how many times you have used the garage.</p>';//no number of visits selected error message break; } }else{//error message $greeting_error = '<p class="error">Please indicate how many times you used the garage</p>';//no number of visits selected error message } //validate email if(!empty($_POST['email'])){ //if the box is not empty $email = $_POST['email']; }else{//no mail address entered $email = NULL; $email_error = '<p class="error">You did not enter your email.</p>'; } //validate the comments if(!empty($_REQUEST['comments'])){//if the box is not empty $comments = $_REQUEST['comments']; }else{//no comments entered $comments = NULL; $comments_error = '<p class="error">You made no comments.</p>';//no comment error message } //validate age if (isset($_POST['age'])){//selection made $age = $_POST['age']; if($age=='0-24'){ $age_message = '<p>Your age is given as under 25 years old. You may qualify for a student discount.</p>'; }elseif ($age =='25-64'){ $age_message = '<p>Your age is given as between 25 and 60. If you are in part time employment or unemployed, you may qualify for a reduced rate.</p>'; }elseif ($age == '65+'){ $age_message = '<p>Your age is given as above 65 years old, so you may qualify for our pensioners discount.</p>'; }else{ $age = NULL; $age_error = '<p class="error">Please choose a value for your age.</p>';//no age chosen error message } }else{//nothing selected $age = NULL; $age_error = '<p class="error">You did not select an age.</p>';//error message } //how did we do? Array. If more than one box ticked give error message if (isset($_POST['quality'])){//at least one box ticked $quality = $_POST['quality']; $num = count($quality); //trivial use of count: better as radio button if ($num == 1){//if just one box ticked foreach ($quality as $k => $v){ $quality_ok = '<p>You have found our service to be ' . $v . '.</p>';//this only works because only one value can be printed } }else{//too many boxes ticked $quality = NULL; $quality_error = '<p class="error">You ticked ' . $num . ' boxes. Please make only a single choice for quality of service.</p>';//error message using count function } }else{//no box ticked $quality = NULL; $quality_error = '<p class="error">You made no choice of quality of service. Please tick one box.</p>';//error message } //what services did we provide? Array if (isset($_POST['service'])){//if box/boxes ticked $service = ($_POST['service']); sort ($service);//sort in alphabetical order //shuffle($service);//randomly sort the list $work_done = implode (' <br />', $service);//turn array into string $work = '<p>We provided the following services last time you visited: <br /> ' . $work_done . '</p>'; }else{ $service = NULL; $work_error = '<p class="error">Please choose at least one service.</p>';//error message } //if all OK, display information if($fname && $lname && $greeting && $email && $age && $gender && $use && $comments && $quality && $service){ echo '<p>' . $greeting . ' ' . $title . ' ' . $fname . ' ' . $lname . ' and thank you for your comments: <br /><br /><tt>' . $comments .'</tt> '. $work . $quality_ok . $age_message . ' We will reply to you at <i>'. $email . '</i></p>'; }else{//if it goes wahoonee shaped display error messages echo $title_error . $fname_error . $lname_error . $greeting_error . $age_error . $work_error .$quality_error . $comments_error . $email_error . '<p class="error">Please go back and complete the form.</p>';//concatenate error messages } ?> </body> </html> Link to comment Share on other sites More sharing options...
old.graham Posted June 11, 2012 Share Posted June 11, 2012 Oops. I should have put in some /n to make the source code readable. Thought of it just after I posted. Apologies. Link to comment Share on other sites More sharing options...
Larry Posted June 14, 2012 Author Share Posted June 14, 2012 Yeah, that's kind of a lot of unreadable code to look through. Perhaps if you wrapped it in the forum's code tags? And is there anything in particular you're looking for in terms of advice? Link to comment Share on other sites More sharing options...
old.graham Posted June 16, 2012 Share Posted June 16, 2012 I will be happy with any advice you feel able to give and try to follow it. I hoped that the code and your comments and advice on this code would be of help to me and other readers/student PHP code writers who have difficulty with the Pursue pages or are curious about other ways of doing things. I shall have a look at using forum formatting tags on the code and in an attempt to make it more readable. The formatting (line spacing and tab/margins) in the IDE I use (Aptana on Ubuntu 10) seems to get lost when I cut and paste. In the past when I have been stuck with a problem, I have found maybe a few lines of code in forums that helped me immensely. I feel that now I can pay back the debt to the wider community by offering some ideas and examples that seem to answer the questions posed in the Pursue section. I am a dilettante in this field (a welder to trade) and study code and computing for intellectual stimulation, occasionally designing and implementing websites for friends for doughnuts and coffee. If this forum is not the appropriate place for this, or if I am being presumptuous, please tell me and I will cease from posting here. Thank you for your time. Link to comment Share on other sites More sharing options...
Larry Posted June 18, 2012 Author Share Posted June 18, 2012 Paying back is wonderful and very much appreciated. Very cool that you're a welder. Something I've only done once or twice but wish I was better at. Not that I have the equipment or need or anything... This forum is absolutely appropriate, if you could just post the code in a more readable way, that'd be better. Link to comment Share on other sites More sharing options...
old.graham Posted June 23, 2012 Share Posted June 23, 2012 Hi Larry, First offering for chapter 3... I have followed the formatting used by gedit in Linux (Ubuntu) so I hope that this is more readable. Thanks for your kind comments on my trade. /*Review and Pursue chapter 3 page 110 PHP and MySQL edition4 *This is a new css template for use on pages in chapter 3 *author old.graham June 2012 */ body{ margin:auto; border: 0px; padding: 0px; background: #EBECF6; width: 950px; } a{ text-decoration: none; color: #4A2885; } a:hover{ color: blue; } #header{ border-style:solid; border-width: 2px; border-color: #BFB2D3; width: 950px; height: 80px; background-color: #F5F8EC; } #header h1{ text-align: center; margin-bottom: -20px; } #header h2{ text-align: right; margin-top: 10px; color: gray; } #navigation{ width: 950px; text-align: left; margin-left: -40px; } #navigation li{ width: 135px; height: 19px; list-style: none; border-width: 2px 1px 1px 2px; display: inline-block; border-style: solid; border-color: #BFB2D3; } #navigation a{ display : block; colo r: navy; font-family: Arial, Helvetica, sans-serif; font-weight: bold; text-decoration: none; background-color: #F5F8EC; text-align: center; } #navigation a:visited{ color: gray; text-decoration: none; background-color: white; } #navigation a:hover{ color: blue; background-color: silver; } #content{ margin: auto; width: 950px; height: 100%; font-family: Arial, Helvetica, sans-serif; color: #555555; } #content h1{ color: navy; } #footer{ width: 950px; margin: 0 auto; text-align: center; } Link to comment Share on other sites More sharing options...
Larry Posted June 25, 2012 Author Share Posted June 25, 2012 I'm a bit confused about this post. You're just sharing some CSS? Link to comment Share on other sites More sharing options...
old.graham Posted June 26, 2012 Share Posted June 26, 2012 Thanks for replying so quickly. Sorry. I can see that adding code piecemeal might be a bit confusing. It takes me ages to format the code so that it is easier to read since I don't seem to be able to paste it with the formatting that gedit or Aptana provide. The css goes with the ability to change the look of the entire site. The previous css file would be named style.css to work with the header. The footer follows shortly. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" href="includes/style.css" type="text/css" media="screen" /> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title> <?php echo $page_title; ?> </title> <style type="text/css" title="text/css" media="all" ><!--additional styling for forms--> label,legend{ font-weight: bold; color: #300acc; } form{ background-color: #cccccc; } </style> </head> <body> <div id="header"> <h1><?php echo $page_title ?></h1> <h2>better the deil ye ken...</h2> </div> <div id="navigation"> <ul> <li><a href="index.php">Home Page</a></li> <li><a href="calculator.php">Calculator </a></li> <li><a href="review_stickyform.php" >Sticky Form </a></li> <li><a href= "http://www.larryullman.com">Larry Ullman </a></li> <li><a href="http://www.ubuntu.com">Ubuntu </a></li> </ul> </div> <div id= "content"><!--start of page specific content--> <!--written for Review and Pursue chapter 3: header.html--> Link to comment Share on other sites More sharing options...
old.graham Posted June 26, 2012 Share Posted June 26, 2012 ...and here is the footer ... <!--end of page specific content--> <!--rewritten for Review and Pursue chapter3: footer.html--> </div> <div id="footer"> <p>Copyright © <a href="#">Awfy Simple</a> 2012 | Designed by <a href="http://www.opendesigns.org/">Old Graham </a> | Valid <a href="http://jigsaw.w3.org/css-validator/">CSS</a> & <a href="http://validator.w3.org/">XHTML </a></p> </div> </body> </html> Link to comment Share on other sites More sharing options...
old.graham Posted July 1, 2012 Share Posted July 1, 2012 The whole new form would be too much for a forum, so I have limited the new form to just the textarea and checkbox not described in the book. <?php # A solution to Review and Pursue chapter 3 page 110: Create a new sticky form. # I have supplied a header.html and footer.html to accompany this form. # The css I submitted above works with this form. # The form looked a bit bland, so I have included additional styling in the header.html file # rather than rewrite the whole of the css file. # I am indebted to http://www.phpfreaks.com/forums/index.php?topic=139571.0 boo_lolly for checkbox code design. # Author old.graham June 2012 using Aptana on Ubuntu Linux: PHP 5.3.3-1: Apache 2.2.16 $page_title = "Review and Pursue _ Sticky Form"; include ('includes/header.html'); ?> <h1>Review Solutions</h1> <h2>Sticky form</h2> <form action="review_stickyform.php" method="post"> <!--create a form--> <!--checkbox with php arrays--> <p><label for="tickbox">Checkbox: <?php $tickvalues = array('Ant', 'Bat', 'Cat', 'Dog');//checkbox titles foreach ($tickvalues as $key => $value){ echo "<input type=\"checkbox\" name=\"tickbox[]\" value=\"{$value}\""; foreach ($_POST['tickbox'] as $index => $checked){ if ($value == $checked){ echo ' checked = "checked"'; }//test for checked } echo " />{$value}\n"; //tick checked values } ?> </label></p> <!--textarea with php to make it sticky--> <p><label>Textarea:<textarea name="textareas" rows="3" cols="40" ><?php if (isset($_POST['textareas'])) {echo $_POST['textareas'];}?></textarea></label></p> <!--and finally, the submit button--> <p><input type="submit" name="submit" value="Submit" /></p> </form> <p> </p> <?php if ($_SERVER['REQUEST_METHOD'] == 'POST'){//has the form been submitted using POST? //some validation: has anything been entered? $animals = implode(', ', $_POST['tickbox']); //turn array from checkbox into a comma separated string if (!empty($_POST['textareas']) && isset($animals)){ //display results echo 'In the Checkboxes, you ticked: <i>' . $animals . '</i><br />' ."\n" . 'In the Textarea, you wrote: <i>' . $_POST['textareas'] . '</i>'; }else{//displayed if values missing echo 'Please complete the form.'; } }//end if submitted with POST ?> <?php include ('includes/footer.html'); ?> Link to comment Share on other sites More sharing options...
rafaec Posted July 6, 2012 Share Posted July 6, 2012 Hey Larry, Love your book btw. I have one question: On the "Pursue" item 3, on Chapter 9. It says to "rewrite customize.php so the script also applies the user's preferences." What do you mean by that? Could you post the solution here? Is the question telling you to make the "customize.php" page to set the css stylesheet to the cookie values once the cookie is set? In this case, can you comment if my code is correct? Please clarify Thanks! <?php //customize.php cookie handling if (isset($_GET['font_size'], $_GET['font_color'])) { //SEND THE COOKIES! setcookie ('font_size', $_GET['font_size'], time()+100000000, '/', '', 0); setcookie ('font_color',$_GET['font_color'], time()+100000000, '/', '', 0); //msg to be printed later: $msg = '<p>Your settings have been entered! Click , <a href="view_settings.php">here</a> to see it in action </p>'; } // end of submitted if ?> <!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=utf-8" /> </head> <body> <?php //if cookies set print msg if (isset($msg)) { print $msg; } ?> <style type="text/css"> body { <?php if (isset($_GET['font_size']) || isset($_GET['font_color'])) { print " font-size: " . htmlentities($_GET['font_size']) . ";\n"; print " color: #" . htmlentities($_GET['font_color']) . ";\n"; } ?> } </style> <p> Use this form to set preferences </p> <form action="customize.php" method="GET"> <select name="font_size"> <option value=""> Font Size </option> <option value="xx-small"> xx-small</option> <option value="x-small"> x-small</option> <option value="small">small </option> <option value="medium">medium </option> <option value="large">large </option> <option value="x-large">x-large </option> <option value="xx-large"> xx-large</option> </select> <select name="font_color"> <option value="">Font Color </option> <option value="999">Gray</option> <option value="0c0">Green</option> <option value="00f">Blue</option> <option value="c00">Red</option> <option value="000">Black</option> </select> <input type="submit" name="submit" value="Set My Preferences" /> </form> </body> </html> Link to comment Share on other sites More sharing options...
old.graham Posted July 11, 2012 Share Posted July 11, 2012 Hi Rafaec, I haven't got as far as you in the book so I can't answer your question. I've just been posting here what I think might be the answers to the Review and Pursue. Like you I am not always sure what Larry has in mind when he asks the questions. Could be the questions are just to make sure you have thought the thing through and are familiar with the code ideas and concepts and there are no hard answers? Larry will no doubt answer this better than anyone. Anyhow here is my take on chapter 3's last 3 questions which I have concatenated into one script. <?php function create_radio($value, $button_name){ echo '<input type="radio" name="' . $button_name . '" value="' . $value . '"'; if (isset ($_POST[$button_name]) && ($_POST[$button_name] == $value)){ echo ' checked = "checked"'; } echo " /> $value \n"; }//end of function ?> <form action="radio_review.php" method="post"> <p>Average Speed: <input type="text" name="speed" value="<?php if (isset($_POST['speed'])) echo $_POST['speed']; ?>"/></p> <p>Distance in Miles: <input type="text" name="distance" value="<?php if (isset($_POST['distance'])) echo $_POST['distance']; ?>"/></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"'; ?>>Good</option> <option value="50" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == 50)) echo 'selected = "selected"'; ?>>Excellent</option> </select></p> <?php $fuel_price = array('3.00', '3.50', '4.00'); //create array $number = count($fuel_price); //count items in array echo "<p>Fuel Price :"; $x = 0; while ($x < $number ) { //while $x is less than the number of items in array create_radio($fuel_price[$x], 'fuel');//call function $x++; //add 1 to $x }//end while loop echo "</p>"; $cost = ($_POST['distance']/$_POST['efficiency'])*$_POST['fuel']; $time = $_POST['distance']/$_POST['speed']; echo '<p>Cost of a journey of ' . $_POST['distance'] . ' miles at $' . $_POST['fuel'] . ' per gallon is $' . number_format($cost, 2) . '.</p>'; if ($time > 24){//if the journey time is more than 24 hours $days = $time/24; $days = round($days);//round the answer down to nearest whole number $hours = $time%24; //modulus echo '<p>The journey will take you: ' . $days . ' days and ' . number_format($hours,2) . ' hours</p>'; }else{ echo '<p>The journey will take you ' . number_format($time,2) . ' hours.</p>'; } ?> <input type="submit" name="submit" value="Submit" /> 1 Link to comment Share on other sites More sharing options...
Larry Posted July 14, 2012 Author Share Posted July 14, 2012 Hello rafaec, Thanks for the nice words on the book. I'm glad you're liking it. Yes, I believe that's what I was getting at. Kudos! Link to comment Share on other sites More sharing options...
old.graham Posted August 7, 2012 Share Posted August 7, 2012 Just posting my solution to Chapter 9 Review and Pursue, Pursue topic 2 _ Custom error handler : It seemed the easiest way to do this was to strip the handle_errors.php script of everything that wasn't inside the php tags,and remove line 36 and save the revised script in the same folder as the other php scripts as handle_error.php. I then added to view_users.php on line 6 include ('handle_errors.php'); which is under line 5 includes ('header.php'); then added on line 7 set_error_handler ('my_error_handler'); The new 'handle_errors.php' <?php #script 8.2 - display errors define('LIVE', FALSE); //create the error handler function my_error_handler($e_number, $e_message, $e_file, $e_line, $e_vars){ //build the error message $message = "An error occured in script '$e_file' on line $e_line: $e_message\n"; //append $e_vars to $message $message .= print_r ($e_vars, 1); if(!LIVE){//print error if in development echo '<pre>' . $message . "\n"; debug_print_backtrace(); echo '</pre><br />'; }else{ //don't show error' echo '<div class="error">A system error occured. We apologise for the inconvenience.<div><br /\>'; }//end if live }//end my_error_handler function ?> It seems to work OK for me. Link to comment Share on other sites More sharing options...
old.graham Posted August 8, 2012 Share Posted August 8, 2012 Another post: solution to Chapter 9 Review and Pursue, Pursue topic 3: In view_users.php: To use the mysqli_num_rows($result) as a boolean value to test TRUE, I simply removed line 19 if ($num > 0 ){//if any records returned and substituted if (mysqli_num_rows($result)){ Very simple _ and it seems to work all right. 1 Link to comment Share on other sites More sharing options...
Larry Posted August 10, 2012 Author Share Posted August 10, 2012 Thanks for sharing those solutions and kudos on your progress! Link to comment Share on other sites More sharing options...
old.graham Posted August 11, 2012 Share Posted August 11, 2012 Hi Larry, Thanks for your comment. But your book really makes it so easy I can take very little credit. Any road up, here is my solution to the 4th question on the chapter 9 Pursue and Review using mysqli_num_rows() function to test for previous use of the email address. //make a query to check if email is already in use $q = "SELECT user_id FROM users WHERE email = '$email'"; $r = mysqli_query($dbc, $q); $n_rows = mysqli_num_rows($r); if ($n_rows > 0){ $errors[] = 'This email address is already in use.'; }else{ $email = mysqli_escape_string($dbc, trim($_POST['email'])); }//end if email already in use This is inserted below line 45 and above line 46 register.php script on page 287. I have tested it and it works fine for me. I use $email rather than $e as a variable name to make it clearer for myself. Link to comment Share on other sites More sharing options...
Antonio Conte Posted August 11, 2012 Share Posted August 11, 2012 Looks good. Escape $email before the first query instead, though. 1 Link to comment Share on other sites More sharing options...
old.graham Posted August 12, 2012 Share Posted August 12, 2012 Hi Antonio, Thanks for the heads up on that, but it has me really puzzled _ unless you mean the error suppressor operator? If that is what you mean then you are quite correct and I should have it in. That would then be $r = @mysqli_query($dbc, $q); If that's not it, can you be more explicit because I can't think what else it could be? Thanking you in advance and mea culpa for the fault. I have just noticed another error in the code. I have inserted the code fragment in the wrong place. It should go in line 33. As it was, it only works if all the other inputs have been filled. I cover my eyes in shame. Link to comment Share on other sites More sharing options...
Antonio Conte Posted August 12, 2012 Share Posted August 12, 2012 Move mysql_real_escape_string() before the first query. You have an injection security hole there. Link to comment Share on other sites More sharing options...
Recommended Posts