Sunday, May 10, 2009

Sending array to a view file [ Step 5 ]

Lets go back to the controller, at this point we done - pass two simple strings to a view file.
I like to send a different data type to view file, so I create a new index call todo and I like to send an array to a view file. Ok I got array with three elements in side index call todo.
class Blog extends Controller
{
function Index()
{
data['title'] = "My blog title";
data['heaading'] = "My blog heading";
data['todo'] = array( 'clean house','eat lunch','call man');
this->load->view('blog_view', $data);
}
}
I create an order list and I use for each loop to do this , and you know that I'am using an alternative PHP syntax using a column instead of an opening brace and like wise instead of an opening brace I use an endforeach construct now I copy an element to list element.
<html>
<head>
<title> <?=$title; ?> </title>
</head>
<body>
<h1> <?=$heading ; ?> </h1>
<ol>
<?php foreach($todo as $item): ?>
<li> <?= $item ?> </li>
<?php endforeach; ?>
</ol>
</body>
</html>
So I reload a page you know see a todo list , so I can pass arrays to view files multidimensional array,objects, it's really use data type you want to work with. One thing before I'am going to the controller and create a constructor, in PHP a constructor is a simply function that named identically to the class, in PHP5 it's a little a bit syntax if you use a local constructor you need to be override constructor that contain parent class the side effect is it will break you application.
class Blog extends Controller
{
function Blog()
{

}
function Index()
{
data['title'] = "My blog title";
data['heaading'] = "My blog heading";
data['todo'] = array( 'clean house', 'eat lunch' , 'call man');
this->load->view ('blog_view', $data );
}

}
If I reload a page you see that know I get an error message
In line number 16.
Fatal error : Call to a member function view()  on non-object  in blog.php line 16.
In order to fix this we need to explicitly call the constructor in the parent class.
With that single line of code I reestablish that relationship
class Blog extends Controller
{
function Blog()
{
parent::controller();
}
function Index()
{
data['title'] = "My blog title";
data['heaading'] = "My blog heading";
data['todo'] = array( 'clean house', 'eat lunch' , 'call man');
this->load->view ('blog_view', $data );
}
}
and I can reload a page and that my application is working again.
So remember any time you use a constructor to explicitly call your parent controller class.

That's all folks

We created a controller , made a view file , generate some dynamic information that we pass to view file and this should see how easy is CodeIgniter to use. This is your first look in a model view controller approach I'll think you see very nice separation in your application logic and
your display element, in later tutorials we will expand this information.
You can see video tutorial
here

Creating CodeIgniter Views [ Step 4 ]

It's not considerate a good practice to send an information to your web-browser directly from your controller so create a correspondent view file to do that I name this one blog_view.php in the views folder Unlike controllers which must be named identically to class name,
view files can be what ever you want, I simply choose this that correlation is clear for a
tutorial, but you can name what ever type of abstraction your like. Your can add further organization by organization a sub folders within a view folder to give you a dipper structure if you want.For now I just paste some simple html
<html>
<head>
<title> My First page </title>
</head>
<body>
<h1> My First Heading </h1>
</body>
</html>
with a title and page heading, now that's done, I go back to the controller blog.php and load a view file form the controller by replacing the echo statement with a view load function
class Blog extends Controller
{
function Index()
{
this->load->view (blog_view);
}
}
you know that a parameter I using is a same as filename for the view file, you can use the file extension if you want but it not necessary.I can now go back to a browser and reload a page and see new view file is been serve up from the controller, at this point, view file is just a static web page, so I want to replace some of the static elements with the dynamic once.

Adding dynamic data to view.

Going back to the controller blog.php I create a data array which control some dynamic information that pass to the view file,
class Blog extends Controller
{
function Index()
{
data['title'] = "My blog title";
data['heaading'] = "My blog heading";
this->load->view ('blog_view', $data );
}
}
Now I can take this data array and place it as a second parameter in view loading function and pass that information under the view file, in doing this the index of the var array will also be a converted to variables to make them easily work with a view files. So I go to a view file and replace static information with dynamic information, with some simple PHP
<html>
<head>
<title> <?php echo $title; ?> </title>
</head>
<body>
<h1> <?php echo $heading ; ?> </h1>
</body>
</html>
If now I save and reload page in a browser you can see that now our controller is dynamically passing the title and a heeding in the view file. If you service support PHP short tags you can simplify this a bit , replace "?php echo" statement with an equal sine eliminating semicolons, and simplify control structure. If you build you site with a help of the web-designer, this can be beneficial because it reduce the amount a PHP code on the page and make a page look cleaner.
<html>
<head>
<title> <?=$title; ?> </title>
</head>
<body>
<h1> <?=$heading ; ?> </h1>
</body>
</html>

Creating CodeIgniter Controller [ Step 3]

Any application you develop with CodeIgniter is going to minimally contain controllers and views. So lets build a controller. When you name your file It's must be the same name as the class of the controller with a php extension. I call this one blog so I can use this in next tutorial.
Create file blog.php in Controller folder with such code inside
class Blog extends Controller
{
Function Index()
{
echo 'Hello World ';
}
}

Who is mister "controller"?

A controller simply a php class, one thing you need to remember, when you name your controller it's need to be capitalize. Remember that a class and file name must match, but the class name must be capitalize and file name will not. The second thing is that must extend the parent controller class, the system controller by extending the main controller class you allow your local class to inherent all property, methods and resources the CodeIgniter makes available to your.
We got a simple function that I call Index, index always will be a default function within your class and I show your how that work in a moment.

Understanding CodeIgniter URL's.

In a model-view-controller approach the first segment correlates to a class name you wish to invoke and the second segment correlates to a function name within the class , I can accomplish the same thing on web page by typing index www.YouHostName.com/CodeIgniter/index.php/blog/index.php as the second segment and explicitly call that function , but the index function is always call by default. Now you going to a browser and type a file name in to a first segment of the URI www.DomainOnWhichYouInstallCodeIgniter.com/CodeIgniter/index.php/blog , your can see that we know getting a "Hello world " statement.
I should also mention that index.php file in our URL can easily be removed with .htaccess file, so if you like clean URL's you can easily accomplish this.

Set up default controller.

If I want reload page using only the front controller again you see I still get a 404 page because by even with a made of controller we hadn't told CodeIgniter to show anything by default.
There is no assumption made by CodeIgniter that a particular controller is what your want show by default we can do that by going into a config folder and opening the routes.php file
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";
This file help us creates mapping instructor's for a URL and amount of other things allow us to specify a default controller, if I change this
$route['default_controller'] = "welcome";
to
$route['default_controller'] = "blog";
a controller that I just create and than reload a page , it's know serving our controller Blog up as a default.

CodeIgniter Installation [ Step 2 ]

Installation is quite easy, read installation instructions in
user guide.
When you install the system you have directory struсture that look like this.






CodeIgniter Folders Overview

I like to focus I like to focus on Application folder, this where we'll be working most of the time as we build our applications. We'll be building controllers, this is a PHP classes that design to take incoming HTTP request , we'll be building view files , this will be html pages or they can be page fragments like headers footers or side bars. There also Error folder, this folder contains a couple of templates
  • error_404.php
  • error_db.php
  • error_general.php
  • error_php.php
to display various errors types. For instance if you want load "www.DomainOnWhichYouInstallCodeIgniter.com/CodeIgniter/index.php" , you'll see we getting a 404 page. Which happens to correspondent to this template error_404.php.

CodeIgniter Introductory Tutorial [ Step 1 ]

1. CodeIgniter Installation

2. Creating CodeIgniter Controller

3. Creating CodeIgniter View's

4. Sending array to a view file

Hi guys, in order to demonstrated you CodeIngniter I'am will create a simple dynamic web page.
If you haven't read the introduction section of the user guide
I encourage you to go ahead and do so. In particular you should read that the section where discus is Model-View-Controller approach to application design since CodeIngniter is based on that approach.