CodeIgniter : Load different view for mobile devices
Codeigniter(PHP framework) supports the detection of client devices(user agents). In this tutorial, we will see how to use Codeigniter User agent class to detect the devices and present different views.
The following code demonstrate the capability in the simplest form
function different_views () {
$this->load->library('user_agent');
if (!$this->agent->is_mobile()) {
$this->load->view('desktop_view');
}
elseif ($this->agent->is_mobile('ipad')) {
$this->load->view('ipad_view');
}
else {
$this->load->view('mobile_view');
}
}
You can view all the user_agents that CodeIgniter recognizes in the file application/config/user_agents.php
Bear in mind that it is case sensitive. The word iPad
will produce different result from the word ipad
Reference:
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+11.1k How to test Facebook App on localhost ?
+5.1k Golang : Calculate a pip value and distance to target profit example
+12.6k Golang : Forwarding a local port to a remote server example
+20.9k Golang : Underscore or snake_case to camel case example
+7.2k Golang : A simple forex opportunities scanner
+5.1k Golang : micron to centimeter example
+9.8k Golang : Format strings to SEO friendly URL example
+5.2k Golang : Issue HTTP commands to server and port example
+13.5k Golang : error parsing regexp: invalid or unsupported Perl syntax
+12k Golang : Convert decimal number(integer) to IPv4 address
+4.7k Mac OSX : Get disk partitions' size, type and name
+10.1k Golang : Setting variable value with ldflags