Putting some intelligence into web applications
DiggBlinkRedditDeliciousTechnorati
article by Anurag
There are about 192 countries in the world, including the Vatican. From a web developer's perspective, this might seem irrelevant. However, this list of countries is quite often used in user registration. By adding simple logic, we can help reduce the agony of the not so adept visitor during the registration process and other related cases.
We will create a simple user registration script with very basic information. However the interesting part would be the country listing. Instead of directly pasting the results from the database to the html select box, we sort the countries by popularity. We define popularity by the number of registered users from a particular country.
We start off by creating two database tables. Table country contains a list of countries, and users stores information for registered users.
create table `country` (
code char(2) not null,
country varchar(255),
popularity int default 0,
primary key(code)
);
create table `user` (
id int auto_increment,
name varchar(64),
email varchar(255),
passwd varchar(255),
country_code char(2),
primary key(id)
);
Then populate the country table with some sample data or use the sql file in the attached: http://www2.anurag.name:82/files/country_list.zip
With the database part complete, we move on to this application's logic. We start off by creating a simple user signup page. The page will have only four fields i.e. name, email, password and country. As we would be reusing the countries list in several places, we're better off making it a function. So we create a new file called functions.php. It'll contain a function which is responsible for rendering our country list. Let's make a simple assumption. The popularity (or the number of users) of a country is already recorded in the system and we are just fetching it. Later we'll worry about how to get this information into the database.
// connect to the database
function db_connect() {
$handle = mysql_connect("localhost", "root", "test");
mysql_select_db("intelligence", $handle);
return $handle;
}
// get country list in a html select list
function render_country_list($list_name) {
$handle = db_connect();
$sql = "select * from `country` order by popularity desc, country";
$results = mysql_query($sql, $handle);
$out = "<select name='$list_name'>";
while($country = mysql_fetch_array($results)) {
$out .= sprintf("<option value='%s'>%s</option>", addslashes($country['code']), addslashes($country['country']));
}
$out .= "</select>";
return $out;
}
The function render_country_list takes one argument $list_name which is the name given to the select list in <select name="...">. It retrieves all the country names and their code from the database sorted by their popularity, so the most popular countries appear first. db_connect is just a helper function for connecting to a database.
Now we are only left with registering a user, and increasing a country's popularity depending on the users country. We place the following functions in functions.php file we created a moment ago.
function register_user($name, $email, $passwd, $country_code) {
$handle = db_connect();
$sql = "insert into `user`
(name, email, passwd, country_code) values
('$name', '$email', '$passwd', '$country_code')";
$result = mysql_query($sql);
if($result) {
increase_country_popularity($country_code);
return "user registered";
}
else
return "error: could not register user";
}
function increase_country_popularity($country_code) {
$sql = "update `country`
set popularity = popularity + 1
where code = '$country_code'";
mysql_query($sql);
}
Function register_user takes the name, email, password and country code submitted by the user and populates the database. Then it increments the popularity field in the table country.
We have all the pieces in place. All we need is a signup form for the user.
<?php
include("functions.php");
if(isset($_REQUEST['submit'])) {
$message = register_user($_REQUEST['name'], $_REQUEST['email'], $_REQUEST['passwd'], $_REQUEST['countries']);
}
?>
<html>
<body>
<?php @print $message; ?>
<form name="signup" action="signup.php" method="post">
<pre>
Name
<input type="text" name="name"/>
Email
<input type="text" name="email"/>
Password
<input type="password" name="passwd"/>
Country
<?php print render_country_list("countries"); ?>
<input type="submit" name="submit" value="Register"/>
</pre>
</form>
</body>
</html>
The form checks whether this is the first time the user has opened the page. <?php print render_country_list("countries"); ?> generates the country dropdown list.
This is a very simple proof of concept. However, the underlying idea is to let the most wanted content rise to the top by itself rather than having to adjust it manually by analyzing site statistics.
You can try the sample application at http://www2.anurag.name:82/samplecode/signup.php
Download the code for this application: http://www2.anurag.name:82/files/country_list.zip