Tuesday, August 17, 2010

CodeIgniter

Introduction to CodeIgniter

CodeIgniter is an Application Development Framework - a toolkit - for people who build web sites using PHP. Its goal is to enable you to develop projects much faster than you could if you were writing code from scratch, by providing a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries. CodeIgniter lets you creatively focus on your project by minimizing the amount of code needed for a given task. CodeIgniter (CI) uses the Model-View-Controller (MVC) approach similar to that of Ruby on Rails.

Some people said "Code Igniter is an Open Source Web Application Framework that makes writing kick-ass PHP programs simple as apple pie". Is it true? Lets begin.

1- Download CodeIgniter Current Version from http://codeigniter.com/downloads/
2- Extract the zip file in your root folder in your localhost pc/webserver.
3- Read User Guide or Basic Step on How to use CodeIgniter before you go further in the code.

In my case, I setup xampp server in my local PC.
Next step;
  1. I extract the codeigniter file insite C:\xampp\htdocs\
  2. Rename the folder name CodeIgniter to any name you want like form1.
  3. Inside the form1 folder, there is system and user guide folder, index.php and license.txt file.
  4. Go inside C:\xampp\htdocs\form1\system\application\config\ and open config.php file. At about line 14 you should see:

    $config['base_url'] = "http://www.your-site.com/";

    Edit this url with your URL:

    $config['base_url'] = "http://localhost/form1";
    form1 is refer to the folder name in step 2 above.

  5. Now save that file and open autoload.php. On line 42, let’s add the e-mail core class:

    $autoload['core'] = array('email');

    and database core class:

    $autoload['libraries'] = array('database');

    and on line 54 let’s add the form and url helpers:

    $autoload['helper'] = array('url', 'form');

    Save that file. The reason for adding the e-mail core class is pretty obvious since one of the features of our registration form is to notify the organizers when someone has submitted a completed form. The database core class is use to interact with database operation.

Controllers

With those core classes and helpers loaded, now the time create my first controller.
  1. Go to C:\xampp\htdocs\form1\system\application\controllers
  2. You will see sample controller file welcome.php
    I copy the file and name it home.php . Open the file and edit the function inside.

  3. This function refer to views file(welcome_message.php) in C:\xampp\htdocs\form1\system\application\views\

    function index()
    {
    $this->load->view('welcome_message');
    }

    **If I want to create new view files for the form, just rename welcome_message to a new name.Let say subject.

    function index()
    {
    $this->load->view('subject');
    }


VIEWS
  1. Go to C:\xampp\htdocs\form1\system\application\views\ and open subject.php
    All display setting for my form are formatted in this file. Create the html form here and set the submit function later in controller(home.php).

  2. Open internet browser and type this url to view the page
    http://localhost/form1/index.php/home
    *home is refer to your controller filename (home.php).
    The form will display in your browser as you want.

MODELS

O'o... I forgot to copy models file.
In my case, this is models file we always use.

Open php syntax

class Subject_model extends Model
{
function Subject_model() {
parent::Model();
table_name = 'subject';

}

function check_uid($uid) {
$this->db->where('uid', $uid);
$query = $this->db->get($this->table_name);
return ($query->num_rows() > 0) ? TRUE:FALSE;
}

function check_sem($sem) {
$this->db->where('semester', $sem);
$query = $this->db->get($this->table_name);
return ($query->num_rows() > 0) ? TRUE:FALSE;
}

function check($uid,$sem) {
$this->db->where('uid', $uid);
$this->db->where('semester', $sem);
$query = $this->db->get($this->table_name);
return ($query->num_rows() > 0) ? TRUE:FALSE;
}


function delete($id) {
$this->db->where('id', $id);
$this->db->delete($this->table_name);
return TRUE;
}

function details($uid,$semester) {
$this->db->where('uid', $uid);
$this->db->where('semester', $semester);
$query = $this->db->get($this->table_name);
if($query->num_rows() > 0) {

return $query;
}
else {
return FALSE;
}
}

function details_ref($refno) {
$this->db->where('student_ref_no', $refno);
$query = $this->db->get($this->table_name);
if($query->num_rows() > 0) {
$result = $query->row();
return $result;
}
else {
return FALSE;
}
}

function get_list($currentsem) {
if(!$currentsem) {

$currentsem = '';
}
/*
$this->db->order_by('student_ref_no', 'ASC');
$this->db->order_by('timecreated', 'DESC');
$this->db->order_by('id', 'DESC');
*/
$query = $this->db->query('SELECT *, COUNT(*)-1 as duplicate FROM (SELECT * FROM student '.$currentsem.' ORDER BY id DESC) as student GROUP BY student_ref_no ORDER BY timecreated DESC');
//$query = $this->db->get($this->table_name);
if($query->num_rows() > 0) {
return $query;
}
else {
return FALSE;
}
}

function count_unique() {
$this->db->group_by('student_ref_no');
$query = $this->db->get($this->table_name);
return $query->num_rows();
}

function get_name($id) {
if(!$id) { return FALSE; }

$this->db->where('id', $id);
$query = $this->db->get($this->table_name);
if($query->num_rows() > 0) {
$result = $query->row();
return $result->student_name;
}
else {
return FALSE;
}
}


function get_latest($limit = NULL, $offset = NULL) {
$this->db->limit($limit, $offset);
$this->db->order_by('date', 'DESC');
$query = $this->db->get($this->table_name);
if($query->num_rows() > 0) {

return $query;
}
else {
return FALSE;
}
}

function insert($data) {
$this->db->set($data);
$this->db->insert($this->table_name);

return $this->db->insert_id();
}

function total() {
$this->db->distinct();
$this->db->select('uid');
$query = $this->db->get($this->table_name);
if($query->num_rows() > 0) {
return $query->num_rows();
}
else {
return FALSE;
}
}
function update($uid,$sem,$data) {
$this->db->where('uid', (string)$uid);
$this->db->where('semester', (string)$sem);
$this->db->set($data);
$this->db->update($this->table_name);
}

function update_student($refno, $data) {
$this->db->where('student_ref_no', $refno);
$this->db->set($data);
$this->db->update($this->table_name);
}
function update_printed($id, $data) {
$this->db->where('id', $id);
$this->db->set($data);
$this->db->update($this->table_name);
}

function get_currentsemester() {
$this->db->distinct();
$this->db->select('currentsemester');
$query = $this->db->get($this->table_name);
if($query->num_rows() > 0) {
return $query;
}
else {
return FALSE;
}
}

}
Close php syntax

*The class name must same with models file name. In this case, file name is subject_model.php save in C:\xampp\htdocs\form1\system\application\models


Integrate Form with Database.


Task1: Create subject withdraw form for student
Data input: subject code, subject name, reason.

  1. Create blank database using phpmyadmin ()... to be continue

0 comments:

Post a Comment