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
+7.3k Golang : Rename part of filename
+17.2k Golang : Find smallest number in array
+8.1k Golang: Prevent over writing file with md5 hash
+7k Golang : Not able to grep log.Println() output
+20k Android Studio : AlertDialog and EditText to get user string input example
+11.7k Golang : Determine if time variables have same calendar day
+23.9k Golang : How to validate URL the right way
+14k Android Studio : Use image as AlertDialog title with custom layout example
+4.7k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+29.4k Golang : Get time.Duration in year, month, week or day
+18.7k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+10.7k Golang : Create Temporary File