Monday, January 27, 2014

CodeIgniter (Step By Step Basic for Beginners)

CodeIgniter: Introduction to CodeIgniter Framework

CodeIgniter Step By Step Tutorial: CodeIgniter (CI) is one of popular php framework. If you are already building PHP Application, CodeIgniter will help you to do it better and more easily. With CodeIgniter, you can save time, make your web more robust, your code will be easier to read and maintenance. It is free, lightweight, and simple to install. In this post, we will know more deep about CodeIgniter before write code.

Nice, CodeIgniter is small and lightweight framework. The zipped download for version 1.5 is only 737 KB. You don't need long time to download it. You can get it from http://www.codeigniter.com. CI was written by Rick Ellis, rock musician turned programmer.

With CodeIgniter, you can cut down the amount of code you need to type. This is not just good for lazy, but: less type, fewer mistake, and less time for spend debugging.

But, CodeIgniter is not everything. We will not find 'engine generator' that can build page self. Several frameworks have features like that. For example, they can create web page (that to do basic Create, Read, Update, and Delete operation) automatically. CodeIgniter doesn't do this.

This, I copy from their help page: "Our goal for CodeIgniter is maximum performance, capability, and flexibility in the smallest, lightest possible package.
...

From a technical and architectural standpoint, CodeIgniter was created with the following objectives:
•             Dynamic Instantiation. In CodeIgniter, components are loaded and routines executed only when requested, rather than globally. No assumptions are made by the system regarding what may be needed beyond the minimal core resources, so the system is very light-weight by default. The events, as triggered by the HTTP request, and the controllers and views you design will determine what is invoked.

•             Loose Coupling. Coupling is the degree to which components of a system rely on each other. The less components depend on each other the more reusable and flexible the system becomes. Our goal was a very loosely coupled system.

•             Component Singularity. Singularity is the degree to which components have a narrowly focused purpose. In CodeIgniter, each class and its functions are highly autonomous in order to allow maximum usefulness.
..."

Nice feature, CodeIgniter is very flexible. We can apply at PHP 4.3.2 and above, or PHP 5. It will realy help us when our web hosting still don't support PHP 5. CI support serveral database: MySQL, MySQLi, MS SQL, Postgre, Oracle, SQLite, and ODBC.

CodeIgniter: Installation

CodeIgniter Step By Step Tutorial: After understand about codeIgniter at this, now, we learn how to install CodeIgniter. We will install to our local computer. Before follow this instruction, please download CodeIgniter at www.codeigniter.com.
1.            Open your root web server.
2.            Put CodeIgniter downloaded.
3.            Extract it, you will get a folder named "codeigniter_[version]".
4.            For simple, rename the folder to be "CodeIgniter" only.
5.            Now, open config.php within CodeIgniter\system\application\config.
6.            Change base site url at line 14:
7.            Point your browser to http://localhost/CodeIgniter. You should get like this:


CodeIgniter: Creating First Application at CodeIgniter

CodeIgniter Step By Step Tutorial: As usually, we will create first application by build hello application. But, before write code, we must know that we will build application use Model Controller View (MVC) pattern. We ever talk about MVC at here. Hope you read that posting if you still don't know about MVC. Although that post about Joomla, it doesn't matter. It is same.
1.         First, we make controller, create a file name "hello.php" within: \system\application\controllers. Enter following code:

2.                  Next step, make a view. Create you_view.php within CodeIgniter\system\application\views. Enter just simple line code like:

3.         Now, test your application. Point your browser to  http://localhost/CodeIgniter/index.php/Hello/you  You should get like this: 
           

            See, this flow:



CodeIgniter: Creating and Sending Parameters between Controller and View

CodeIgniter Step By Step Tutorial: We still wok with our first application at CodeIgniter. But we will build controller and view better. Create parameters in controller and send it to view. It will make our application more flexible.

Open your hello.php within CodeIgniter/system/application/controller. Overwrite become like this:

Now, open your you_view.php within CodeIgniter/system/application/views. Rewrite with following code:



CodeIgniter: Getting Parameters from GET


CodeIgniter Step By Step Tutorial: If previous post we learn how to send parameter to view, now, we will learn how to get parameter from GET. As we know, we can see this parameter from Url. Example: http://localhost/index.php?name=Andi. Clear, we can get parameter name. How about in CodeIgniter?

Ok, we learn by doing. Open again your hello.php within CodeIgniter/system/application/controller. Rewrite with following code:


This is analysis:


CodeIgniter: Setting Database Configuration


CodeIgniter Step By Step Tutorial: Previous post, we have build simple first application. That application do not use database. In this post, we try to set database for CodeIgniter.

Please, open database.php within CodeIgniter\system\application\config. Set config like below:

Make sure, it matches with your database.


CodeIgniter: Preparing Database

CodeIgniter Step By Step Tutorial: After set database configuration,we will learn about showing data from database in CodeIgniter. But, before that, we prepare a database for practice. This post creates a database named "codeigniter" and a table named "hello". We use phpMyAdmin for easy.
1.            Open your phpmyadmin.
2.            Enter database name "codeigniter" in create new database field.
3.            Click Create button. Your database will be created.

4.            Create new table by entering name of new table at create new table field.
5.            Enter number of fields.
6.            Click Go button.
7.            Enter name of field, type, and length
8.            Choose auto increment and check primary key for primary key field.
9.            Click Save button.

10.          Now, we insert data. Click [Insert tab].
11.          Enter data.
12.          Click Go for saving.

CodeIgniter: Showing Simple All Data


CodeIgniter Step By Step Tutorial: We have set database and create table for CodeIgniter practice. Now, we learn how to show data in CodeIgniter. As we know, CodeIgniter use MVC pattern. We will use Model for retrieve data from database.

First, build a model. Create a file named "employee_model.php" within CodeIgniter/CodeIgniter\system\application\models. Enter following code:


Next, we make a view. Create a file named "employee_viewall.php" within CodeIgniter\system\application\views. Enter folowing code:

Build controller. Create a file named "employee.php" within CodeIgniter\system\application\controllers. Enter following code:

 

CodeIgniter: Showing Simple One Data


CodeIgniter Step By Step Tutorial: After show all data, We learn how to show one data. We want a data with certain ID, example ID = 1. We still use same files with previous practice.

For model: Open "employee_model.php" within CodeIgniter\system\application\models. Add following function:

For Controller: Open "employee.php" within CodeIgniter\system\application\controllers. Add following function:

For View: Open "employee_view.php" within CodeIgniter\system\application\views. Enter following code:

Now, test it. Point your browser to http://localhost/CodeIgniter/index.php/employee/get.

CodeIgniter: Choosing a Data from GET


CodeIgniter Step By Step Tutorial: At previous tutorial, we learn show a data. But we have defined ID that we want to show. In this post, we learn how to show a data that ID defined from GET.

For model: Open "employee_model.php" within CodeIgniter\system\application\models. Add following function:

For Controller: Open "employee.php" within CodeIgniter\system\application\controllers. Add following function:

Now, test it. Point your browser to http://localhost/CodeIgniter/index.php/employee/get/2.