Testing the application can obviously be done with a WAP phone. Since
not everybody out there owns a WAP-enabled phone or PDA, and still needs
to test its WAP applications, the best thing to do is to install a WAP
browser emulator. There are not many WAP browsers running under Linux (the
best WAP emulator I have used is part of the Windows Nokia SDK, it is written
mostly in Java, but requires a few DLLs to function). Personally, I use
the Deck-it previewer,
available freely (it is not open source) at http://www.pyweb.com.
Its best advantages are that it is quite fast, and it can display the WML
code it receives.
create table responses (
response_id int not null auto_increment,
response_refpoll int,
response_string varchar(50),
response_nb int default 0,
primary key(response_id)
);
Once the table are created, they are populated with the following data. This poll basically ask visitors which Linux distributions they use, between Red Hat, Mandrake, etc...
insert into responses (response_refpoll, response_string) values
(1, "Red Hat");
insert into responses (response_refpoll, response_string) values
(1, "Mandrake");
insert into responses (response_refpoll, response_string) values
(1, "TurboLinux");
insert into responses (response_refpoll, response_string) values
(1, "Caldera");
insert into responses (response_refpoll, response_string) values
(1, "SuSE");
$host = "localhost";
$user = "root";
$password = "";
$database = "test";
$conn = mysql_pconnect($host,$user,$password);
if (!$conn)
{
echo "Connection impossible\n";
exit;
}
mysql_select_db($database);
?>
<card id="MainCard" title="Polling system">
<p>
Which Linux distribution do you use?<br/>
<select name="response" multiple="true">
<option value="1">Red Hat</option>
<option value="2">Mandrake</option>
<option value="3">TurboLinux</option>
<option value="4">Caldera</option>
<option value="5">SuSE</option>
</select>
<a href="vote_poll.php?poll=1&response=$(response)">Vote now!</a><br/>
</p>
</card>
</wml>
The <select> tag implements a selection list form. Since the
multiple
attribute is set to true, several options can be selected simultaneously,
and each <option> tag constitutes a possible choice. The variable
called $(response) is filled with the "value" attributes of the
selected options, separated by ";". For example, if the user selects both
Red Hat (response=1) and Mandrake (response=2),
$(response) will
be filled with the string "1;2".
The initial screen corresponding to this example is shown below:
Clicking on the [...] link (see above) prompts the following display:
Since all our polls are stored within the database, the following PHP script (PHP code is shown in blue) fetches information from the database, and create the appropriate WML code. You simply have to specify, when calling the script from your browser, which poll you want to display, for example: http://localhost/show_poll.php?poll=1
Here is the PHP code for show_poll.php:
include("config.inc.php");
header("Content-type: text/vnd.wap.wml");
print '<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD
WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">';
?>
<wml>
<template>
<do type="prev" label="Back">
<prev/>
</do>
</template>
<card id="MainCard" title="Polling system">
<p>
<?php
//fetches the desired
poll question
$result = mysql_query("select
poll_question from polls where poll_id = $poll") or die("There is something
wrong the SQL query (" . mysql_error() . ")\n");
list($question) = mysql_fetch_array($result);
print "$question<br/>\n";
print "<select
name=\"response\" multiple=\"true\">\n";
//now fetches the
possible answers
$result = mysql_query("select
response_id, response_string from responses where response_refpoll = $poll")
or die("There is something wrong the SQL query (" . mysql_error() . ")\n");
while (list($response_id,
$response_string) = mysql_fetch_array($result)) {
print "<option value=\"$response_id\">$response_string</option>\n";
}
print "</select>\n";
print "<a href=\"vote_poll.php?poll=$poll&response=$(response)\">Vote
now!</a><br/>\n";
?>
</p>
</card>
</wml>
include("config.inc.php");
//explode $response
$response_array = explode(";", $response);
//join the array with ',' for inclusion in
the following sql query
$response_sql = join(",", $response_array);
//update the number of responses
mysql_query("update responses set response_nb
= response_nb + 1 where response_id in ($response_sql)") or die("There
is something wrong the SQL query (" . mysql_error() . ")\n");
header("Content-type: text/vnd.wap.wml");
print '<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD
WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">';
?>
<wml>
<template>
<do type="prev" label="Back">
<prev/>
</do>
</template>
<card id="MainCard" title="Polling system">
<p>
Thank you! See the results at this <a href="show_results.php?poll=<?php
print $poll; ?>">page</a>.
</p>
</card>
</wml>
And the corresponding screen is:
The "page" link shown above leads to a very simple WML page
(show_results.php), which contains a dynamically generated WBMP
image. The code for show_results.php is shown below:
<template>
<do type="prev" label="Retour">
<prev/>
</do>
</template>
<card id="MainCard" title="Polling system">
<p>
<img src="show_image.php?poll=<?php print $poll;
?>" alt="WBMP image"/><br/>
</p>
</card>
</wml>
Here is the PHP code for show_image.php, the script that automatically
generates the WBMP bar charts image from the results stored within the
database. Note that the Content-type sent to the WAP browser is image/vnd.wap.wbmp.
include("config.inc.php");
$height = 50;
$width = 100;
//initialize the array
$responses = array();
$responses_strings = array();
//exec the sql query that retrives results
of the desired poll
$result = mysql_query("select response_string,
response_nb from responses where response_refpoll = $poll") or die("There
is something wrong the SQL query (" . mysql_error() . ")\n");
//store the results into the array
while (list($response_string, $response_nb)
= mysql_fetch_array($result)) {
$responses[] = $response_nb;
$responses_strings[] =
$response_string;
}
//normalize the values
$coef = $height / max($responses);
$normalized_responses = array();
while (list($key, $val) = each($responses))
$normalized_responses[]
= floor($val * $coef);
//set the bar width
$bar_width = floor($width / (count($responses)
* 2));
//send the required header for a WBMP image
header ("Content-type: image/vnd.wap.wbmp");
$im = @ImageCreate ($width, $height + 10) or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im,
255, 255, 255);
$text_color = ImageColorAllocate ($im, 233,
14, 91);
for ($i=0, $pos=0; $i<count($responses);
$i++, $pos+=2*$bar_width) {
//draw a vertical bar
if ($normalized_responses[$i]
> 0) ImageFilledRectangle ($im, $pos+$bar_width, $height-$normalized_responses[$i],
$pos+ 2*$bar_width, $height, 255);
//draw
vertical text
ImageStringUp ($im, 1,
$pos+2, $height, $responses_strings[$i], $text_color);
}
//send the image to the browser
ImageWBMP ($im);
?>
Here is an example of screen that can be obtained:
Olivier Elemento is a consultant in fixed and mobile Internet software technologies. You can contact him at elemento@club-internet.fr and have a look at its web page at http://genomenews.free.fr/cv.html