PHPGurukul

CodeIgniter View

  1. A view is a web page that display all the elements of User Interface. View deals with the fronted operations. View display data and capture user action. View send user action to controller. Location of  all view files is applications/views.
  2. View could not be called directly. You need to load the view through controller.

    How to create view

    Create a HTML page and name it firstview.php. Save this file inside  applications/views.
    Code of the firstview.php file.

    <html>
    <head>
    <title>CodeIgniter View</title>
    </head>
    <body>
    <h1>Welcome to PHPGurukul Programming Blog</h1>
    </body>
    </html>

    Load the view

    View will be load through controller.  Syntax for loading view will be :

    $this->load->view('name of the view');

    For our case syntax will be :

    $this->load->view('firstview');

    Load multiple views in Controller

    <?php
    class Blog extends CI_Controller {
        public function index()
        {
            $this->load->view('header');
            $this->load->view('body');
            $this->load->view('footer');
        }
    }

    You can also create a new folder inside application/view and save your files in this folder.

    Sort views in Subfolders

    You can  sort views within subfolders:

    $this->load->view(‘directory_name/file_name’);

    Pass Dynamic Data to view

    Data can be transfer from controller to view by using an array or object.

    <?php
    class Blog extends CI_Controller {
        public function index()
        {
            $data['title'] = "CodeIgniter View";
            $data['heading'] = "Welcome to PHPGurukul";
            $this->load->view('firstview', $data);
        }
    }

    View will be look like this :

    <html>
    <head>
    <title><?php echo $title;?></title>
    </head>
    <body>
    <h1><?php echo $heading;?></h1>
    </body>
    </html>

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
.

Recommended Tutorials for you


Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Web Hosting with a FREE domain. On sale ₹99.00/mo

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.