Example 1 – using the built-in libraries

In this initial example, we will see how to use the CI built-in library. Here we will use the CI library CI_Table as well as the CI_db library, which, for a given database table/view and some optional CSS settings, will enable us to render the table nicely with all the HTML table tags and CSS settings in just a single line of code. In this example, we will use the same user's table that we used for the controller example in Chapter 3, Usage and Scope of Controllers.

This example will be constructed from the following controller and view:

  • application/controllers/builtins.php: This controller loads the built-in CI library table as well as the db library, which is autoloaded (for more information, refer to Chapter 2, Configurations and Naming Conventions) to get the user's table content, and set up the table to render using the table library.
    $this->load->library('table'),
    

    The controllers prepare vectors of map settings and the list of places and possible controllers to zoom into each of the places, and render a view named google_map_view.

  • application/views/users_view.php: This view will use the table library service to render a nicely formatted table as loaded from db, and configured by the controller.

    Note

    Let us assume that the URL to the project root is http://mydomain.com/myproject, http://mydomain.com/myproject/builtins.

    (The source code is provided with this book via URLs.)

The controller file

The following is a step-by-step example of the controller code for each operation:

<?php 
/** Use CI built In libraries
class Builtins extends CI_Controller{
  function __construct(){
    parent::__construct();
    // Load the table library that generates the HTML tags for// showing the table structure within a view
    $this->load->library('table'),
    }
  public function index(){
    // Load the users list from DB into the view 
    $data['users'] = $this->db->get('users'),
    // Create custom header for the table 
    $header = array
    ('id', 'User Name', 'Hashed Password', 'Position' );
    // Set the headings
    $this->table->set_heading($header);
    // Set table formatting 
    $table_format = array ( 'table_open'  => '<table border="1"cellpadding="2" cellspacing="1" class="mytable">' );
    $this->table->set_template($table_format);
    // Load the view and send the results
    $this->load->view('users_view', $data);
    }
  }

The view file

To complete the operation, we will finish working on the view file.

<!DOCTYPE html">
<meta http-equiv="Content-type" content="text/html;charset=utf-8"/>
<html>
<head>
<title>
  Showing Users Table Using CI Build-In table Library
</title>
</head>
<body>
  <div id='results'>
  <!—All The Formatted Table is rendered by the table libraryinstance using the controller defined settings and the tableof users we have fetched from the DB >
  <?php echo $this->table->generate($users); ?>
  </div>
</body>
</html>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.240.252