Skip to main content

How To Load Different View For Mobile Devices In CodeIgniter 3 - PHP




How To Load Different View For Mobile Devices In 
CodeIgniter 3 - PHP



User Agent Class


The User Agent Class provides functions that help identify information about the browser, mobile device, or robot visiting your site. In addition, you can get referrer information as well as language and supported character-set information.


The User Agent class is initialized in your controller using the $this->load->library function:


$this->load->library('user_agent');
The name of the web browser viewing your site:


$browser = $this->agent->browser();

> Home Controller



<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
    public function index()
    {
// Load User Agent Library [OR autoload.php file]
$this->load->library('user_agent');
if ($this->agent->is_mobile()) {
// Pass Data in View File
$data_array['title'] = "Mobile Page Title";
// Mobile View Template
$this->load->view('home/_mobile/index', $data_array);
} else {
// Pass Data in View File
$data_array['title'] = "Page Title";
// Other Plateform View Template
$this->load->view('home/index', $data_array);
}
    }
}

> Main View File


<h1>Main View Template</h1>
<small><?= $title ?></small>
<br>
<?php
$is_mobile = $this->agent->is_mobile();
var_dump($is_mobile);
?>
> Mobile View File

<h1>Mobile View Template</h1>
<small><?= $title ?></small>
<br>
<?php
$is_mobile = $this->agent->is_mobile();
var_dump($is_mobile);
?>

Comments

Popular posts from this blog

Support Page - Fill Excel Data - CTH (Google Chrome Extension)

Fill Excel Data - CTH (Google Chrome Extension) Download Link Video GitHub Support Links:

CodeIgniter 3 Complete Login and Registration System with MySQL and Session Part #3

#2 CodeIgniter 3 Complete Login and Registration System with MySQL and Session

CodeIgniter 3 Complete Login and Registration System with MySQL and Session Part #2 ✎ Working * Install CodeIgniter 3 in Xampp Server * Setup CodeIgniter 3 and remove index.php * Create User Controller in Codeigniter 3 * Create login and registration view page and load in user controller * Include Bootstrap 4 Library: https://getbootstrap.com/ * Adding Header and Footer ♦ Source Code:- https://goo.gl/7udsza 🔴 Subscribe to My Youtube Channel: https://goo.gl/rN2644