If you're not familiar with the Code Igniter framework, don't worry; the program that I'm going to build in the next few lines will be pretty straightforward. In simple terms, any web application developed with this framework will be composed of three separate modules, based on the Model-View-Controller approach: first the controller, which is charged with handling application logic; then the model, which handles business logic, generally in the form of database tables or a data access layer; and finally, one or more views, usually represented by a set of HTML pages. So, first I'm going to define a "Products" controller. It will handle the logic required to generate a fictional, multi-lingual "products" web page section. Finally, I'm going to create a view file, tasked with displaying these products as a simple web document. The prototype of the controller class looks like this: <?php // define 'Products' controller class class Products extends Controller{ function Products(){ // load controller parent parent::Controller(); // load database class and connect to MySQL $this->load->database(); } // display products list function index(){ // get user IP address and check to see if it's valid if($this->input->valid_ip($this->input->server('REMOTE_ADDR'))){ $ip= sprintf('%u',ip2long($this->input->server('REMOTE_ADDR'))); // get 3-char country code $this->db->select('three_chars_country'); $code=$this->db->get_where('iptocountry',array('lower_bound <=' => $ip,'upper_bound =>' => $ip); // get products list from country code-based MySQL table $products=$this->db->get('products_'.$code); // load 'products' view depending on country code $this->load->view('products_'.code'.php',$products); } } } ?> To put it simply, the above "Products" controller starts loading the database class that comes bundled with Code Igniter, which is used within the "index()" method, to fetch the contents from different "products" MySQL tables, depending on the countries from which users are accessing this fictional web site. In the end, the controller finishes its execution by loading the appropriate view file and embedding the product-related data into it. This short example should give you an idea of how to generate a "products" web page in several languages using Code Igniter. However, the missing piece of this schema is the view file that renders these products in the form of a basic web document. Thus, in the final section of this tutorial I'm going to build this file. Therefore, click on the link that appears below and read the following segment. We're almost done!
blog comments powered by Disqus |
|
|
|
|
|
|
|