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
+26.1k Mac/Linux and Golang : Fix bind: address already in use error
+18.9k Golang : Implement getters and setters
+13.9k Golang : Gin framework accept query string by post request example
+15.5k Golang : Find location by IP address and display with Google Map
+6.2k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+14k Golang : Human readable time elapsed format such as 5 days ago
+8k Golang : Gomobile init produce "iphoneos" cannot be located error
+3.7k Java : Get FX sentiment from website example
+42.1k Golang : How do I convert int to uint8?
+3.5k Golang : Fix go-cron set time not working issue
+14.2k Golang : Check if a file exist or not
+10.7k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)