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
+14.8k Golang : How do I get the local IP (non-loopback) address ?
+3.9k Javascript : Empty an array example
+54.8k Golang : Unmarshal JSON from http response
+15.8k Golang : Generate universally unique identifier(UUID) example
+5.2k Unix/Linux/MacOSx : How to remove an environment variable ?
+11.7k Golang : How to parse plain email text and process email header?
+8.5k Golang : On lambda, anonymous, inline functions and function literals
+6.8k Golang : How to call function inside template with template.FuncMap
+8.9k Golang : Simple histogram example
+4.8k Python : Convert(cast) bytes to string example
+6.4k Golang : Warp text string by number of characters or runes example
+40.8k Golang : How to count duplicate items in slice/array?