Home arrow PHP arrow Page 3 - Generating Web Pages in Multiple Languages with a PHP IP-to-Country Mapping Application

Building web pages in different languages - PHP

If you’re a PHP developer interested in learning how to develop an IP-to-country application that can be integrated into any existing web site, then look no further. Welcome to the third part of a series on developing an IP-to-country mapping application with PHP. Made up of four approachable tutorials, this series teaches you how to create a program like this by using a single MySQL lookup table, and shows you how to use this program to enrich your own web sites with geo-location capabilities.

TABLE OF CONTENTS:
  1. Generating Web Pages in Multiple Languages with a PHP IP-to-Country Mapping Application
  2. Review: building IP-to-country mapping applications
  3. Building web pages in different languages
  4. Building a simple front controller
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
February 18, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Without a doubt, web sites that show their contents in different languages are very popular these days, right? You should feel glad because you can belong to this club too! Indeed, by using the prior MySQL lookup table, it's possible to generate web pages on a multi-lingual platform.

To demonstrate how to achieve this, I'm going to reuse the MySQL abstraction class that you learned in the previous section to recreate a fictional scenario in which a web site is capable of displaying a typical "products" page in several languages.

Essentially, the prototype code that should be used to produce this web page would look as follows:


// define 'MySQL' class

class MySQL{

private $mysqli;

private $result;

public function __construct($host='localhost',$user='user',$password='password',$database='database'){

// connect to MySQL and select database

$this->mysqli=new mysqli($host,$user,$password,$database);

if(mysqli_connect_errno()){

throw new Exception('Error connecting to MySQL: '.$this->mysqli->error);

}

}

// run SQL query

public function query($query){

if(!$this->result=$this->mysqli->query($query)){

throw new Exception('Error running SQL query: '.$this->mysqli->error);

}

}

// fetch one row

public function fetchRow(){

while($row=$this->result->fetch_assoc()){

return $row;

}

return false;

}

}


try{

// connect to MySQL and select database

$db=new MySQL('host','user','password','database');

// get user IP address

$ip=sprintf('%u',ip2long($_SERVER['REMOTE_ADDR']));

// Map IP Address to country using 'iptocountry' database

$db->query("SELECT two_chars_country FROM iptocountry WHERE lower_bound <=$ip AND upper_bound >= $ip LIMIT 1");

// get row from database table

$row=$db->fetchRow();

// fetch contents from 'products' MySQL table based on 2-char country code

$db->query('SELECT id,name,description FROM products_'.$row['two_chars_country']);

while($row=$db->fetchRow()){

// display products list and provide a link to a details page

echo '<h2>Product :'.$row['name'].'</h2><p>Description :'.$row['description'].'</p>';

echo '<p><a href="details.php?id='.$row['id'].'">View product details</a></p>';

}

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


As you can see, the above code sample demonstrates how easy it is to construct an hypothetical "products" web page section whose contents are delivered using multiple languages. In this case, the example assumes that there is one "products" database table for each supported language.

Based on this assumption, the "iptocountry" MySQL table is naturally employed to map the IP of the user to the respective two-char country code, and once this value has been retrieved, it is used to fetch the products from the appropriate database table.

Of course, there are many ways to build multi-lingual web page sections dynamically. However, the previous example should give you a clear idea of how to accomplish this through the lookup MySQL table.

At this stage, I already showed you how to generate a products list by using a simple IP-to-country script. Yet, in that specific situation, only the products were displayed using distinct idioms. So what can you do if you need to build entire multi-lingual web documents?

One of the easiest ways to achieve this is by using a simple front controller. If you're interested in learning more about this topic, it will be discussed in detail in the last section of this tutorial.

Click on the link below and read the following section, please.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: