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 2 – passing the complex parameters to a view


In this example, we will show you how to pass and use complex parameters, such as arrays and object arrays, from the CI controller to the rendered CI view to be used in the view. You can pass any number of arrays as parameters to a view; you can also pass objects, such as rows of a query result.

A standard GET parameters URI looks like this: http://ourdomain.com/index.php/example2/more/?a=1&b=2&c=3.

However, let's remember that in CI the URI is passed in this manner: http://ourdomain.com/index.php/example2/more/1/2/3. For more information, see Chapter 2, Configurations and Naming Conventions.

Looking at the URI, we will build the controller example2.php with the function named more with the three parameters passed to it.

We will build the following two scripts:

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

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

The controller file

The controller is responsible for rendering the view with parameters such as mega title and message.

The following is the code sample of the controller:

<?php 
class Example2 extends CI_Controller {
  //This function gets parameters and passes them to the view//example2more
  //The example url//http://ourdomain.com/index.php/example2/more/1/2/3
  so $a = 1, $b = 2, $c = 3
  public function more($a, $b, $c)
  {
    // The parameters in $view_params are extracted in the view//example2more.php
    // In this example 2 variables will be generated by CI in the//view page example2more.php
    //variable: $mega_title, value: Codeigniter, Passing//url parameters to view
    variable: $rows, value: array('a' => $a, 'b' => $b, 'c' => $c);
    $rows = array('a' => $a, 'b' => $b, 'c' => $c);
    $view_params = array('mega_title' => 'Codeigniter -  Passing url parameters to view 'rows' => $rows);
    $this->load->view('example2more', $view_params);
    }	}// closing the class definition/* End of file welcome.php

The view file

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>Key</td>
  <td>Value</td>
</tr>
<?php foreach ($rows as $key => $value): ?>
<tr>
  <td><?php echo $key ; ?></td>
  <td><?php echo $value ; ?></td>
</tr> 
<?php endforeach; ?>
</table>
</body>
</html>