Book Image

Programming with CodeIgniter MVC

Book Image

Programming with CodeIgniter MVC

Overview of this book

The CodeIgniter Model-View-Controller framework provides genius simplicity, flexibility, and efficient resource usage, boosting performance and reusability. "Programming with CodeIgniter MVC" reviews the unique features of CodeIgniter using simple, easy-to-follow, and practical examples. Programming with CodeIgniter MVC provides a simple step-by-step guide to efficiently utilizingthe CodeIgniter MVC framework for developing web applications. This book is packed with engaging examples to understand the usage of controllers, libraries, and (Codeigniter) CI Models. This book commences with a quick discussion of the CodeIgniter Integration with  external plugins such as Flickr API, Google Maps and more will be reviewed with clear usage examples. It will then cover CI naming convention rules, mandatory and optional configurations, and usage within a CI project. It will also cover user defined configurations. By the end of this book, you will not only understand user-defined libraries in a CI framework, but also their services, role, usage, and scope with the help of an example-based approach. The book also covers helpers, models, and views, as well as their usage. Using this book, youwill soonbe able to develop feature-rich web applications using the CodeIgniter MVC framework. "Programming with CodeIgniter MVC" is a one-stop solution to developing solutions with CodeIgniter MVC.
Table of Contents (15 chapters)
Programming with CodeIgniter MVC
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Appendix
Index

Example 3 – the database query by a model rendering results to a view


In this example, we will show you how the CI controller uses the CI model to retrieve data from the database and render it to a CI view.

The URL will be http://ourdomain.com/index.php/user.

First, we will have to configure the database settings in the configuration file application/config/database.php.

We should keep the default database settings unchanged, and only change the following configuration parameters:

$db['default']['hostname'] = '127.0.0.1';
//In many cases when the hostname's value is 'localhost' theconnection to the database fails.
//Setting the hostname to 127.0.0.1 solves the problem.
$db['default']['username'] = 'dbUser;
$db['default']['password'] = 'dbPassword';
$db['default']['database'] = 'dbDataAbse';
$db['default']['port']     = 'dbPort';

The model class will retrieve all the user details from the table users.

For more information on configurations, refer to Chapter 2, Configuration and Naming Conventions.

We will build the following three scripts:

  • The controller class: application/controllers/user.php

  • The model file: application/model/usermodel.php

  • The view script: application/views/userview.php

The controller file

The controller retrieves the users list from the database via the model and renders the view with it.

The following is the code sample of the controller:

<?php
class User extends CI_Controller {
  function users()
  {
    //Manually loading the database
    $this->load->database();
    //Loading the model class
    $this->load->model('Usermodel');
    $view_params['mega_title'] = 'Model Example';
    //Calling the model to retrieve the users from the database
    $view_params['users']= $this->Usermodel->get_users();
    $this->load->view('userview', $view_params);
  }
}
/* End of file welcome.php */
/* Location: /application/controllers/welcome.php */

The model file

The following is the code sample of the model.

<?php
class Usermodel extends CI_Model {
  function __construct()
  {
    // Call the Model constructor	parent::__construct();
  }
  //This method retrieves the users list and returns an array of //objects each containing user details
  function get_users()
  {
    //Calling CI's database object's method for generating SQL//queries.
    $query = $this->db->get('users');
    //returns an array of users objects
    return $query->result();
  }
}

In this example, the CI object database's method is called for generating and executing the SQL query.

Please refer to the CI database's library at http://ellislab.com/codeigniter/user-guide/database/index.html.

For more information about models, refer to Chapter 6, Models.

The view file

The view in this example shows the table content received from the controller containing the users list as defined in the database.

The following is the corresponding rendered view:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title><?php echo $mega_title ?></title>
</head>
<body>
<table>
<tr>
  <td>ID</td>
  <td>Name</td>
  <td>Email</td>
</tr>
<?php foreach ($users as $user): ?>
<tr>
  <td><?php echo $user->user_id ?></td>
  <td><?php echo $user->user_fname." ".$user->user_lname; ?></td>
  <td><?php echo $user->user_email ; ?></td>
</tr>
<?php endforeach; ?>
</body>
</html>