Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 30 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
30
Dung lượng
344,19 KB
Nội dung
Appendix A: Answers to Exercises 661 echo $num1 - $num2; } else if ($operator == ‘*’) { echo $num1 * $num2; } else if ($operator == ‘/’) { echo $num1 / $num2; } else { echo ‘UNKNOWN’; } } ? > < /strong > < /div > < /form > Chapter 6 1. Create the edit/delete code for the people table. Use the movie code as an example. A. One possible solution is as follows. Change commit.php as highlighted: < ?php //connect to MySQL $db = mysql_connect(‘localhost’, ‘bp6am’, ‘bp6ampass’) or die (‘Unable to connect. Check your connection parameters.’); //make sure our recently created database is the active one mysql_select_db(‘moviesite’, $db) or die(mysql_error($db)); ? > < html > < head > < title > Commit < /title > < /head > < body > < ?php switch ($_GET[‘action’]) { case ‘add’: switch ($_GET[‘type’]) { case ‘movie’: $query = ‘INSERT INTO movie (movie_name, movie_year, movie_type, movie_leadactor, movie_director) VALUES (“’ . $_POST[‘movie_name’] . ‘”, ‘ . $_POST[‘movie_year’] . ‘, ‘ . $_POST[‘movie_type’] . ‘, ‘ . $_POST[‘movie_leadactor’] . ‘, ‘ . $_POST[‘movie_director’] . ‘)’; break; case ‘people’: $query = ‘INSERT INTO people (people_fullname, people_isactor, people_isdirector) VALUES (“’ . $_POST[‘people_name’] . ‘”, bapp01.indd 661bapp01.indd 661 12/10/08 5:35:18 PM12/10/08 5:35:18 PM Appendix A: Answers to Exercises 662 ‘ . $_POST[‘people_isactor’] . ‘, ‘ . $_POST[‘people_isdirector’] . ‘)’; break; } break; case ‘edit’: switch ($_GET[‘type’]) { case ‘movie’: $query = ‘UPDATE movie SET movie_name = “’ . $_POST[‘movie_name’] . ‘”, movie_year = ‘ . $_POST[‘movie_year’] . ‘, movie_type = ‘ . $_POST[‘movie_type’] . ‘, movie_leadactor = ‘ . $_POST[‘movie_leadactor’] . ‘, movie_director = ‘ . $_POST[‘movie_director’] . ‘ WHERE movie_id = ‘ . $_POST[‘movie_id’]; break; case ‘people’: $query = ‘UPDATE people SET people_fullname = “’ . $_POST[‘people_fullname’] . ‘”, people_isactor = “’ . $_POST[‘people_isactor’] . ‘”, people_isdirector = “’ . $_POST[‘people_isdirector’] . ‘” WHERE people_id = ‘ . $_GET[‘id’]; break; } break; } if (isset($query)) { $result = mysql_query($query, $db) or die(mysql_error($db)); } ? > < p > Done! < /p > < /body > < /html > Chapter 7 1. Create a site called “ A Virtual Vacation. ” Offer different backgrounds that people can superimpose photos of themselves on, and let them send virtual postcards to their friends and family. A. Your code would need to include a background/upload page and a result page. We cover sending e - mail postcards in Chapter 11 , so you can simply use the scripts from that chapter and insert your newly created postcard. This code comes with some caveats; of course, you don ’ t have access to the sample image files we ’ ve used, and you will have to alter your code a bit based on the sizes of your images. Also, we haven ’ t stored our images in any database, and we ’ ve only allowed for JPG images to be bapp01.indd 662bapp01.indd 662 12/10/08 5:35:19 PM12/10/08 5:35:19 PM Appendix A: Answers to Exercises 663 uploaded. Keeping those things in mind, your background/upload page should look something like this (we named our file vacation.html ): < html > < head > < title > Go on a Virtual Vacation! < /title > < /head > < body > < form method=”post” action=”upload_image.php” enctype=”multipart/ form-data” > < table > < tr > < td > < label for=”image_caption” > Image Name or Caption < /label > < br/ > < em > Example: Wish you were here! < /em > < /td > < td > < input id=”image_caption” name=”image_caption” type=”text” size=”55”/ > < /td > < /tr > < tr > < td > < label for=”image_username” > Your Name: < /label > < /td > < td > < input id=”image_username” name=”image_username” type=”text” size=”15” > < /td > < /tr > < tr > < td > < label for=”image_filename” > Upload Image: < /label > < /td > < td > < input id=”image_filename” name=”image_filename” type=”file” / > < /td > < /tr > < /table > < p > < em > Acceptable image formats include: JPG/JPEG < /em > < /p > < p > Select your destination: < /p > < table > < /tr > < tr > < td > < input type=”radio” id=”destination_1” name=”destination” value=”beach” / > < /td > < td > < label for=”destination_1” > < img src=”images/beach.jpg” > < /label > < /td > < /tr > < tr > < td > < input type=”radio” id=”destination_2” name=”destination” value=”golfcourse” / > < /td > < td > < label for=”destination_2” > < img src=”images/golfcourse.jpg” > < /label > < /td > < /tr > < tr > < td > < input type=”radio” id=”destination_3” name=”destination” value=”mountains” / > < /td > < td > < label for=”destination_3” > < img src=”images/mountains.jpg” > < /label > < /td > < /tr > < /table > < p style=”text-align: center” > < input type=”submit” name=”Submit” value=”Submit” / > < input type=”reset” value=”Clear Form” / > < /p > < /form > < /body > < /html > Then we have a page that processes the photos and merges them together, called upload_image.php . bapp01.indd 663bapp01.indd 663 12/10/08 5:35:19 PM12/10/08 5:35:19 PM Appendix A: Answers to Exercises 664 < ?php // filter incoming variables $image_caption = (isset($_POST[‘image_caption’])) ? $_POST[‘image_caption’] : ‘’; $image_username = (isset($_POST[‘image_username’])) ? $_POST[‘image_ username’] : ‘Anonymous’; $destination = $_POST[‘destination’]; $image_tempname = $_FILES[‘image_filename’][‘name’]; $today = date(‘Y-m-d’); //change this path to match your images directory $dir =’C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/images’; $image_name = $dir . $image_tempname; if (move_uploaded_file($_FILES[‘image_filename’][‘tmp_name’], $image_name)) { //get info about the image being uploaded list($width, $height, $type, $attr) = getimagesize($image_name); if ($type != IMAGETYPE_JPEG) { echo ‘ < p > < strong > Sorry, but the file you uploaded was not a JPG ‘ . ‘file. < br/ > Please hit your back button and try again. < /strong > < /p > ’; } else { //image is acceptable; ok to proceed $dest_image_name = $dir . $destination . ‘.jpg’; $image = imagecreatefromjpeg($image_name); list($width2, $height2, $type2, $attr2) = getimagesize($dest_image_ name); $image2 = imagecreatefromjpeg($dest_image_name); imagecopymerge($image2, $image, 0,0,0, 0, $width, $height, 100); } header(‘Content-type:image/jpeg’); imagejpeg($image2); } ? > 2. Have a page on your site with funny photographs or cartoons, and allow your users to write the captions for them. Place the text in a speech bubble that is appropriately sized, based on the length of the caption they submit. A. First, you need to have the page that gathers the input from the user: < html > < head > < title > Write your own caption! < /title > < /head > < body > < h1 > Write Your Own Caption! < /h1 > < img src=”images/cartoon.jpg” alt=”captionless cartoon”/ > bapp01.indd 664bapp01.indd 664 12/10/08 5:35:19 PM12/10/08 5:35:19 PM Appendix A: Answers to Exercises 665 < form method=”post” action=”caption.php” > < table > < tr > < td > < label for=”image_caption” > Write a caption for the cartoon: < /label > < br/ > < em > Example: You talkin’ to me? < /em > < /td > < td > < input id=”image_caption” name=”image_caption” type=”text” size=”25” maxlength=”25” / > < /td > < /tr > < tr > < td > < /td > < td > < input type=”submit” value=”Send my Caption”/ > < /td > < /tr > < /table > < /form > < /body > < /html > Then you need to put the text in the bubble. We are using a simple ellipse shape that will stretch to fit how long the text is. You can use the following code: < ?php $image_filename = ‘images/cartoon.jpg’; $image_caption = (isset($_POST[‘image_caption’])) ? $_POST[‘image_caption’] : ‘ ‘; $length = strlen($image_caption); $image = imagecreatefromjpeg($image_filename); //draw a white ellipse based on string length $white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); // the center point for the bubble on our cartoon is at coordinates 134, 14 // alter these values for your specific image if necessary $e_x = 134; $e_y = 14; // assume each character is 10px plus 10px on either side of the string for // extra cushion. $e_width=($length * 10) + 20; $ellipse = imagefilledellipse($image, $e_x, $e_y, $e_width, 25, $white); //get starting point for text $x = $e_x - (($length * 10) / 2) - 10; $y = $e_y + 5; //place the text in the bubble imagettftext($image, 12, 0, $x, $y, 0, ‘arial.ttf’, $image_caption); header(‘Content-type: image/jpeg’); imagejpeg($image); ? > bapp01.indd 665bapp01.indd 665 12/10/08 5:35:20 PM12/10/08 5:35:20 PM Appendix A: Answers to Exercises 666 You could also have used imagettfbbox() to perform a similar task. We didn ’ t cover this in Chapter 7 , so you might want to refer to the manual at www.php.net/imagettfbbox for more information on this function. 3. Create a page for kids where they can choose different heads, bodies, and tails from animals and put them together to make a new creation and a new image. Or, create a virtual paper doll site where kids can place different outfits on a model and then save the images they create. A. Although there are many ways to do this, we will have four separate pages: one for picking the head, one for picking the midsection, one for picking the behind/tail, and one for putting them all together and outputting our final result. We could easily create these images by taking stock photos of animals, resizing them so they are the same size, and then cutting them into three sections, using an image - processing program such as Photoshop or GIMP (or heck, we can even do this using PHP, right?). When we have our sections all ready to go, our first page, animal1. html , will look something like this: < html > < head > < title > Create your very own animal! < /title > < /head > < body > < p > First, you must pick a head for your new animal. < /p > < form method=”post” action=”animal2.php” > < table > < tr > < td > < input id=”head_1” name=”head” type=”radio” value=”cowhead” / > < /td > < td > < label for=”head_1” > < img src=”images/cowhead.jpg” / > < /label > < /td > < /tr > < tr > < td > < input id=”head_2” name=”head” type=”radio” value=”elephanthead” / > < /td > < td > < label for=”head_2” > < img src=”images/elephanthead.jpg” / > < /label > < /td > < /tr > < tr > < td > < input id=”head_3” name=”head” type=”radio” value=”giraffhead” / > < /td > < td > < label for=”head_3” > < img src=”images/giraffhead.jpg” / > < /label > < /td > < /tr > < tr > < td > < input id=”head_4” name=”head” type=”radio” value=”pighead” / > < /td > < td > < label for=”head_4” > < img src=”images/pighead.jpg” / > < /label > < /td > < /tr > < tr > < td > < /td > < td > < input type=”submit” name=”Submit” value=”Pick a Body - > ” / > < /td > < /tr > < /table > < /form > < /body > < /html > Our next file, animal2.php , looks like this: < html > < head > < title > Create your very own animal! < /title > < /head > < body > < p > Second, you must pick a body for your new animal. < /p > < form method=”post” action=”animal3.php” > bapp01.indd 666bapp01.indd 666 12/10/08 5:35:20 PM12/10/08 5:35:20 PM Appendix A: Answers to Exercises 667 < table > < tr > < td > < input id=”body_1” name=”body” type=”radio” value=”cowbody” / > < /td > < td > < label for=”body_1” > < img src=”images/cowbody.jpg” / > < /label > < /td > < /tr > < tr > < td > < input id=”body_2” name=”body” type=”radio” value=”elephantbody” / > < /td > < td > < label for=”body_2” > < img src=”images/elephantbody.jpg” / > < /label > < /td > < /tr > < tr > < td > < input id=”body_3” name=”body” type=”radio” value=”giraffbody” / > < /td > < td > < label for=”body_3” > < img src=”images/giraffbody.jpg” / > < /label > < /td > < /tr > < tr > < td > < input id=”body_3” name=”body” type=”radio” value=”pigbody” / > < /td > < td > < label for=”body_3” > < img src=”images/pigbody.jpg” / > < /label > < /td > < /tr > < tr > < td > < /td > < td > < input type=”hidden” name=”head” value=” < ?php echo $_POST[‘head’]; ? > ” / > < input type=”submit” name=”Submit” value=”Pick a Tail - > ” / > < /td > < /tr > < /table > < /form > < /body > < /html > And our next file, animal3.php , looks like this: < html > < head > < title > Create your very own animal! < /title > < /head > < body > < p > Finally, you must pick a tail for your new animal. < /p > < form method=”post” action=”animal4.php” > < table > < tr > < td > < input id=”tail_1” name=”tail” type=”radio” value=”cowtail” / > < /td > < td > < label for=”tail_1” > < img src=”images/cowtail.jpg” / > < /label > < /td > < /tr > < tr > < td > < input id=”tail_2” name=”tail” type=”radio” value=”elephanttail” / > < /td > < td > < label for=”tail_2” > < img src=”images/elephanttail.jpg” / > < /label > < /td > < /tr > < tr > < td > < input id=”tail_3” name=”tail” type=”radio” value=”girafftail” / > < /td > < td > < label for=”tail_3” > < img src=”images/girafftail.jpg” / > < /label > < /td > < /tr > < tr > < td > < input id=”tail_3” name=”tail” type=”radio” value=”pigtail” / > < /td > < td > < label for=”tail_3” > < img src=”images/pigtail.jpg” / > < /label > < /td > < /tr > < tr > < td > < /td > < td > < input type=”hidden” name=”head” value=” < ?php echo $_POST[‘head’]; ? > ” / > < input type=”hidden” name=”tail” value=” < ?php echo $_POST[‘tail’]; ? > ” / > < input type=”submit” name=”Submit” value=”Make the Animal!” / > < /td > < /tr > bapp01.indd 667bapp01.indd 667 12/10/08 5:35:21 PM12/10/08 5:35:21 PM Appendix A: Answers to Exercises 668 < /table > < /form > < /body > < /html > And finally, the file that combines the three images, our animal4.php file: < ?php $head = $_POST[‘head’]; $body = $_POST[‘body’]; $tail = $_POST[‘tail’]; $image_dir=’images/’; $head_image = imagecreatefromjpeg($image_dir . $head . ‘.jpg’); $body_image = imagecreatefromjpeg($image_dir . $body . ‘.jpg’); $tail_image = imagecreatefromjpeg($image_dir . $tail . ‘.jpg’); // Our images are 100px x 200px and were chopped horizontally. $new_animal = imagecreatetruecolor(300, 200); //merge the head imagecopymerge($new_animal, $head_image, 0, 0, 0, 0, 100, 200, 100); //now merge in the body imagecopymerge($new_animal, $body_image, 100,0, 0, 0, 100, 200, 100); //and finally the tail imagecopymerge($new_animal, $tail_image, 200, 0, 0, 0, 100, 200, 100); header(‘Content-type: image/jpeg’); imagejpeg($new_animal); ? > Chapter 8 1. Add validation to the code that adds and edits people records. A. In commit.php , add or change the lines highlighted below: switch ($_GET[‘action’]) { case ‘add’: switch ($_GET[‘type’]) { case ‘movie’: $error = array(); break; case ‘people’: $error = array(); $people_name = trim($_POST[‘people_name’]); if (empty($people_name)) { bapp01.indd 668bapp01.indd 668 12/10/08 5:35:21 PM12/10/08 5:35:21 PM Appendix A: Answers to Exercises 669 $error[] = urlencode(‘Please enter a name!’); } if (empty($_POST[‘people_isactor’]) & & empty($_POST[‘people_isdirector’])) { $error[] = urlencode(‘Please specify if the person is an actor ‘ . ‘or a director!’); } if (empty($error)) { $query = ‘INSERT INTO people (people_fullname, people_isactor, people_isdirector) VALUES (“’ . $people_name . ‘”, ‘ . $_POST[‘people_isactor’] . ‘, ‘ . $_POST[‘people_isdirector’] . ‘)’; } break; } break; case ‘edit’: switch ($_GET[‘type’]) { case ‘movie’: $error = array(); break; case ‘people’: $error = array(); $people_name = trim($_POST[‘people_name’]); if (empty($people_name)) { $error[] = urlencode(‘Please enter a name!’); } if (empty($_POST[‘people_isactor’]) & & empty($_POST[‘people_isdirector’])) { $error[] = urlencode(‘Please specify if the person is an actor ‘ . ‘or a director!’); } if (empty($error)) { $query = ‘UPDATE people SET people_fullname = “’ . $people_name . ‘”, people_isactor = “’ . $_POST[‘people_isactor’] . ‘”, people_isdirector = “’ . $_POST[‘people_isdirector’] . ‘” WHERE people_id = ‘ . $_GET[‘id’]; } break; } break; 2. Write and test a regular expression pattern to validate an e - mail address. A. This is not the only answer, but it gets the job done: /[\w\-]+(\.[\w\-]+)*@[\w\-]+(\.[\w\-]+)+/ bapp01.indd 669bapp01.indd 669 12/10/08 5:35:22 PM12/10/08 5:35:22 PM Appendix A: Answers to Exercises 670 Chapter 9 In Chapter 9 , you were shown three short snippets of code and asked to spot the errors and figure out how to fix them. Then you were asked to create a little error - catching script to catch the errors. 1. < ?php $query = “SELECT * FROM table_name “ . “WHERE name = ‘” . $_POST[‘name’] . “’;” $result = mysql_query($result) or die(mysql_error()); ? > A. Parse error from lack of semicolon at the end of the statement; the semicolon there is for the SQL statement. The correct code is: < ?php $query = “SELECT * FROM table_name “ . “WHERE name = ‘” . $_POST[‘name’] . “’”; $result = mysql_query($result) or die(mysql_error()); ? > 2. < ? if ($_POST[‘first_name’] = “Jethro”) { echo “Your name is “ . $_POST[‘first_name’]; } ? > A. You always need to check equality with double equals ( == ), not single equals ( = ). A single equals sign is for setting a variable equal to a value. The correct code is: < ?php if ($_POST[‘first_name’] == “Jethro”) { echo “Your name is “ . $_POST[‘first_name’]; } ? > 3. < ? $full_name = $_POST[‘mrmiss’] “. “ $_POST[‘first_name’] “ “ $_POST[‘last_name’]; ? > bapp01.indd 670bapp01.indd 670 12/10/08 5:35:22 PM12/10/08 5:35:22 PM [...]... the processing page (ml_purge.php): Chapter 15... VARCHAR(255); Then, modify edit_character.php, adding or modifying the lines highlighted below: . ‘db.inc.php’; $db = mysql_ connect (MYSQL_ HOST, MYSQL_ USER, MYSQL_ PASSWORD) or die (‘Unable to connect. Check your connection parameters.’); mysql_ select_db (MYSQL_ DB, $db) or die (mysql_ error($db)); . ‘db.inc.php’; $db = mysql_ connect (MYSQL_ HOST, MYSQL_ USER, MYSQL_ PASSWORD) or die (‘Unable to connect. Check your connection parameters.’); mysql_ select_db (MYSQL_ DB, $db) or die (mysql_ error($db)); . $alias = mysql_ real_escape_string($_POST[‘alias’], $db); $real_name = mysql_ real_escape_string($_POST[‘real_name’], $db); $address = mysql_ real_escape_string($_POST[‘address’], $db); $city = mysql_ real_escape_string($_POST[‘city’],