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
+5.6k Android Studio : AlertDialog to get user attention example
+14.5k Golang : Gzip file example
+7.8k Golang : Turn string or text file into slice example
+4.4k Golang : Function as an argument type example
+7.2k Golang : Random integer with rand.Seed() within a given range
+10.7k Golang : How to check if your program is running in a terminal
+3.3k Which content-type(MIME type) to use for JSON data
+8.3k Golang : How to transmit update file to client by HTTP request example
+8.4k Golang : How to profile or log time spend on execution?
+2.9k Javascript : Empty an array example
+9.9k Golang : Change date format to yyyy-mm-dd
+6k Gogland : Single File versus Go Application Run Configurations