Sample ApplicationsNow you are ready to rock 'n' rollbasically. The rest of this chapter features two more sample scripts that offer more complexity. Server-Side Form Data ProcessingThe first demo application features a task that is common on many pagesevaluating form data. We will not check the form data for required fields, because entering this data is hard enough on many mobile devices, and users would most probably not accept error messages after entering data. Instead, we will print out all entered data, converted to WML, therefore providing the script scriptname.php that was used in some of the form examples. The structure of this script (see Listing 19.13) is fairly easy. According to the send method (POST or GET) used, the submitted form data is retrieved from the arrays $_POST or $_GET. Then the output WML is created, and special characters are converted to WML using htmlspecialchars(): Listing 19.13. The Form Data Is Analyzed and Sent Out<?php header("Content-type: text/vnd.wap.wml"); switch (strtoupper($_SERVER["REQUEST_METHOD"])) { case "POST": $name = $_POST["name"]; $earlier = $_POST["earlier"]; $currently = $_POST["currently"]; break; case "GET": $name = $_GET["name"]; $earlier = $_GET["earlier"]; $currently = $_GET["currently"]; break; default: $name = $earlier = $currently = ""; } $name = htmlspecialchars($name); $earlier = htmlspecialchars(str_replace(";", " and ", $earlier)); $currently = htmlspecialchars($currently); echo <<<END <?xml version="1.0"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml12.dtd"> <wml> <card id="output" title="output"> <p> Hello, $name! <br/> You have used $earlier. <br/> But you are currently using $currently. </p> </card> </wml> END; ?> Take a look now at how the value returned from the multiple selection list is converted into something prettier (see Figure 19.15). The separators (semicolons) are replaced with "and", so that "php4;php5" is changed to "php4 and php5". Figure 19.15. The form data is displayedusing PHP.WAP Cinema Reservation SystemAt the end of this chapter is one more demo application, a rather complex one. Is the reservation phone line of your favorite cinema always occupied? Imagine how you could just make your reservation with your WAP-enabled mobile phone! To do so, you first need a MySQL server. Create a database "wap" and tables "movies" and "reservations". The first table holds information about all movies currently playing; the second table will contain the reservations. Let's start with the movies table. It will contain four fields:
Listing 19.14 shows the SQL code used to create this table; fill it with some data. Listing 19.14. SQL Code to Create the Movies TableCREATE TABLE `movies` ( `id` int(11) NOT NULL auto_increment, `title` varchar(255) NOT NULL default '', `showtime` datetime NOT NULL default '0000-00-00 00:00:00', `seats` int(11) NOT NULL default '200', PRIMARY KEY (`id`) ) TYPE=MyISAM ; INSERT INTO `movies` VALUES (1, 'Lord Of The Strings', '2004-03-08 20:00:00', 200); INSERT INTO `movies` VALUES (2, 'Harry Blogger', '2004-03-08 22:00:00', 250); INSERT INTO `movies` VALUES (3, 'Foolander', '2004-03-08 23:00:00', 200); INSERT INTO `movies` VALUES (4, 'Bugs', '2004-03-08 19:00:00', 250); The table reservations consists of four fields, as well:
The SQL code in Listing 19.15 will create the table: Listing 19.15. SQL Code to Create the Reservations TableCREATE TABLE `reservations` ( `id` int(11) NOT NULL auto_increment, `mobile` varchar(20) NOT NULL default '', `movies_id` int(11) NOT NULL default '0', `seats` int(11) NOT NULL default '0', PRIMARY KEY (`id`) ) TYPE=MyISAM; The movie application consists of two parts. One script will show all available movies, and the other one makes the reservation. Showing a list of all available movies is an easy taskjust issue a SELECT SQL statement to the database that returns all movies that have not been shown yet: SELECT * FROM movies WHERE showtime > '2004-03-13 12:00:00' AND seats > 0 ORDER BY showtime ASC (whereas 2004-03-13 12:00:00 is the current date and time). This information is displayed using WML. After each movie, there is a reservation link. This link contains the ID of the movie and links to a second script that will take the reservation for the selected presentation. But first, Listing 19.16 shows the source code that queries the MySQL server and prints out all movies: Listing 19.16. The Movie List<?php header("Content-type: text/vnd.wap.wml"); $moviedata = ""; $handle = mysql_connect("localhost", "username", "password"); mysql_select_db("wap", $handle); $now = date("Y-m-d H:i:s"); $rows = mysql_query("SELECT * FROM movies WHERE showtime > '$now' " . "AND seats > 0 ORDER BY showtime ASC", $handle); while ($row = mysql_fetch_array($rows)) { $moviedata .= htmlspecialchars( $row["title"] . ", " . $row["showtime"] . " (" . $row["seats"] . " available) "); $moviedata .= ' <a href="reservation.php?id=' . $row["id"] . '">book</a><br/>'; } echo <<<END <?xml version="1.0"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml12.dtd"> <wml> <card id="card1"> <p> Welcome! </p> <p>Available movies:<br /> $moviedata </p> </card> </wml> END; ?> The movie datawhen, where, how many seats available, reservation linkis saved in the variable $moviedata. This later makes printing out this information easier, using echo <<<. Figure 19.16 shows the movie list. Figure 19.16. The movie list.The file reservation.php reads out the ID parameter in the URL and then queries MySQL about the movie information. The user then may enter the number of requested seats into a text field and must also provide a phone number (to identify the user later or send an SMS message). <p>Number of tickets: <input type="text" name="seats" maxlength="1" size="1" format="N"/> <br/> Your mobile phone number: <input type="text" name="phone" size="15"/> </p> At the end of the WML page, postfields are created to send the reservation request to a third script (do_reservation.php) that executes the actual reservation: <anchor> <go href="do_reservation.php" method="post"> <postfield name="id" value="$id"/> <postfield name="seats" value="$(seats:e)"/> <postfield name="phone" value="$(phone:e)"/> </go> Make reservation </anchor> Note that the movie's ID is taken from a PHP variable ($id); the number of seats and phone number, however, come from the WML form. Listing 19.17 is the complete script reservation.php; Figure 19.17 shows the result in a WAP browser: Listing 19.17. The Reservation Form<?php header("Content-type: text/vnd.wap.wml"); $id = $_GET["id"]; if (get_magic_quotes_gpc() == 0) { $id = addslashes($id); } $handle = mysql_connect("localhost", "username", "password"); mysql_select_db("wap", $handle); $rows = mysql_query("SELECT * FROM movies WHERE id = '$id'", $handle); if ($rows && $row = mysql_fetch_array($rows)) { $title = htmlspecialchars($row["title"]); $showtime = htmlspecialchars($row["showtime"]); $available = htmlspecialchars($row["seats"]); echo <<<END <?xml version="1.0"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml12.dtd"> <wml> <card id="card1"> <p> Ticket reservation for $title at $showtime, $available seats available. </p> <p>Number of tickets: <input type="text" name="seats" maxlength="1" size="1" format="N"/> <br/> Your mobile phone number: <input type="text" name="phone" size="15"/> <br/> <anchor> <go href="do_reservation.php" method="post"> <postfield name="id" value="$id"/> <postfield name="seats" value="$(seats:e)"/> <postfield name="phone" value="$(phone:e)"/> </go> Make reservation </anchor> </p> </card> </wml> END; } ?> Figure 19.17. The reservation form.Finally, on do_reservation.php, the following steps are done:
The following code (see Listing 19.18) sends the SQL statements to the database. The ID of the newly created entry in the table reservations is retrieved using mysql_insert_id() and is used as a confirmation number (see Figure 19.18). Listing 19.18. The Reservation Confirmation<?php header("Content-type: text/vnd.wap.wml"); $id = $_POST["id"]; $seats = $_POST["seats"]; $phone = $_POST["phone"]; if (get_magic_quotes_gpc() == 0) { $id = addslashes($id); $seats = addslashes($seats); $phone = addslashes($phone); } $handle = mysql_connect("localhost", "username", "password"); mysql_select_db("wap", $handle); $rows = mysql_query("SELECT seats FROM movies WHERE id = '$id'", $handle); if ($rows && $row = mysql_fetch_array($rows)) { if ($row["seats"] >= $seats) { mysql_query("UPDATE movies SET seats = seats - $seats WHERE id='$id'"); mysql_query("INSERT INTO reservations (mobile, movies_id, seats) VALUES (" . "'$phone', '$id', '$seats')"); $message = "Reservation successful! Your confirmation number is " . mysql_insert_id() . "."; } else { $message = "Reservation failed, not enough seats available."; } } echo <<<END <?xml version="1.0"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml12.dtd"> <wml> <card id="card1"> <p> $message </p> <p> <a href="index.php">Back to movie list</a> </p> </card> </wml> END; ?> Figure 19.18. The reservation confirmation.This example is only the first step toward a commercial application and can be expanded and extended in various ways:
|