March 7, 2021
Hot Topics:

WML Scripting Tips and Integration with PHP

  • By Steve Schafer
  • Send Email »
  • More Articles »

Utilizing the Power of PHP

As previously mentioned, PHP's interoperability with other technologies makes it a powerful ally for WML. One such technology, databases, is especially useful.

As an introduction, we'll cover a basic example here. The next article in the series will showcase more examples of using PHP to extend WML.

For this example we'll use MySQL, a popular open-source database solution, although any database with PHP connectivity would work as well.

Suppose that we have a team of salespeople in the field who need regular access to customer data. In this example we'll only be concerned with the customer's phone number, but it illustrates the underlying power of using PHP while creating a useful "online" phonebook.

Our database for this example is very simple:

    First Name         Last Name         Phone Number    
John Smith 555-723-0900
Kathy Lamarr 555-876-2222
Sam Kinkaid 555-243-8766
Holly Haute 555-327-0987

In MySQL, the database and table would be created using the following code:


USE Customer;

  FirstName varchar(30) default NULL,
  LastName varchar(30) default NULL,
  Phone varchar(12) default NULL

INSERT INTO Phone VALUES ('John','Smith','555-723-0900');
INSERT INTO Phone VALUES ('Kathy','Lamarr','555-876-2222');
INSERT INTO Phone VALUES ('Sam','Kinkaid','555-243-8766');
INSERT INTO Phone VALUES ('Holly','Haute','555-327-0987');

Now we'll create a relatively simple PHP program to display the customers in a select list. When the user selects an entry, the phone will dial the selected number.

1.  <?php

2.  header("Content-type: text/vnd.wap.wml");  

3.  print "<?xml version=\"1.0\"?>\n";  
4.  print "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\""  
5.  . " \"http://www.wapforum.org/DTD/wml_1.1.xml\">\n"; 

6.  print "<wml>\n";
7.  print "<card id=\"Phonebook\">\n";
8.  print "<p><select>\n";

9.  $link = mysql_connect("localhost", "user", "password")
10.  or die("Could not connect to database!");
11.  mysql_select_db("customers")
12.  or die("Could not select database!");

13.  $query = "select * from Phone order by LastName";
14.  $result = mysql_query($query,$link)
15.  or die("Query failed:$query");

16.  while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
17.  print "<option title=\"Call\"";
18.  print " onpick=\"wtai://wp/mc;$line[Phone]\" >";
19.  print "$line[FirstName] $line[LastName] ($line[Phone])\n";
20.  print "</option>\n";
21.  }

22.  print "</select></p>\n";
23.  print "</card>\n";
24.  print "</wml>\n";

25.  ?>

The code is fairly straightforward:

  • Lines 1-5 are our standard WML preamble.
  • Lines 6-8 open the required tags.
  • Lines 9-12 create the link to the database. Be sure to substitute an actual username and password for the placeholders.
  • Lines 13-15 query the database for all fields in all rows of the Customers database.
  • Lines 16-21 step through the results row by row and build an <option> for each one.
  • Lines 22-24 close all open tags.

Each <option> includes an "onpick" attribute that will dial the selected number on most devices, via the "wtai://wp/mc;<phone number>" URL scheme. This is a useful trick for all users to dial numbers from within your WML.

The generated output resembles the following figure:

Image courtesy Openwave Systems Inc. (Openwave, the Openwave logo, Openwave SDK, Openwave SDK Universal Edition, Openwave SDK WAP Edition are trademarks of Openwave Systems Inc. All rights reserved.)

Quite a few changes can be made to the code above:

  • The "order by" clause of the query can be changed to sort by a different field or by multiple fields.
  • Although the current layout allows each record to be displayed in its entirety, using the "wrap" option in the <p> tags would allow more records to be displayed per screen (but each would have to be highlighted for the full record to be seen).
  • This example is convenient because there are only four rows in our database. To make our user interface more "friendly," we should create paging code to step through the data nine rows at a time (ensuring that each row has a quick access key [1-9] associated with it).

Next Time

The next article in this series will expand the phone book PHP example, highlighting techniques such as paging record display, searching for records, etc.

About the Author

Steve Schafer is president and CEO of Progeny Linux Systems, a Linux-based consulting company in Indianapolis, Indiana. He has written several technical books and articles and can be reached at sschafer@synergy-tech.com.

# # #

Page 3 of 3

This article was originally published on August 19, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date