Friday 21 September 2012

CodeIgniter- A sample project

CodeIgniter is a toolkit for people who build web applications 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 is similar o cakephp which is another framework of php.

INSTALLATION
           
You can download codeigniter latest vesion from their site http://codeigniter.com/downloads/
after download you copy it to the wamp->www folder.

  • At first step, you have to configure database for the project.
  • You can configure database at pplications/config folder.

CONFIGURATION
       Just like most MVC frameworks, codeigniter also have model,view ,controller folders You can create corresponding models,views and controllers inside each  folder.

Here iam explaining to create a sample project in codeigniter.

SAMPLE PROJECT

In this project iam creating a news listing website.

create a database named news and create a table named news 


CREATE TABLE news (
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(128) NOT NULL,
text text NOT NULL,
PRIMARY KEY (id),
KEY slug (slug)
);
change the default controller in routes.php like this

$route['default_controller'] = 'news/index';
$route['(:any)'] = 'news/index/$1';



First create a controller named news and save it as news.php inside controller folder.

<?php

class News extends CI_Controller {



public function __construct()// constuctor
{
parent::__construct();
$this->load->model('news_model');// load news_model at startup
}

public function index($page = 'index')
{
$data['news'] = $this->news_model->get_news();// function writen in model
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer', $data);

}

}
?>
Here we are loading three view files ,so we have to create them.

1) create a header.php named file and save it inside view folder in a folder named templates and paste folllowing code into it:-
<html>
<head>
<title>News listing</title>
</head>
<body>
<h1> News</h1>

2) create a footer.php named file and save it inside the view folder ina folder named templates and pate the following code into it:-
<strong>&copy; 2012</strong>
</body>
</html>

Thus we are successful in creating a layout for our files.

3) Next we have to create view file for news index. for this create a folder named news inside view folder. and then create a php file named index.php.

But before that we want to prepare a model for news,so that we can fetch data from the database.

create a model named news_model.php and save in the folder model.

<?php
class News_model extends CI_Model {

public function __construct()
{
$this->load->database();// loading the databse
}

public function get_news($title = FALSE)
{
if ($title === FALSE)
{
$query = $this->db->get('news');//getting all news
return $query->result_array();// returing array of data
}
$query = $this->db->get_where('news', array('title' => $title));// getting news with conditions
return $query->row_array();// returing array of data
}

}
?>

so now it's time to create index view for news.

<?php foreach ($news as $news_item): ?>

    <h2><?php echo $news_item['title'] ?></h2>
    <div id="main">
        <?php echo $news_item['text'] ?>
    </div>
    

<?php endforeach ?>

Here news will be fetched one byone...

For adding data to database
...................................................

Here iam explaining how to add data to databse using codeigniter.

first create html view as create.php in folder news inside view folder

<h2>Create a news item</h2>

<?php echo validation_errors(); ?>

<?php echo form_open('news/create') ?>

<label for="title">Title</label> 
<input type="input" name="title" /><br />

<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" /> 

</form>




create a function in contoller named create

public function create()
{
$this->load->helper('form');// helper library default of codeigniter
$this->load->library('form_validation');// helper library default of codeigniter
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');// we have to set criteria of each form elemen, it will evaluate automaticallly
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE)// if valiadtion false then return error message
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');// success message will be shown
}
}

Now  it's time to create model function

public function set_news()
{
$this->load->helper('url');

$data = array(
'title' => $this->input->post('title'),
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);// data will be inserted to database
}

Download from here:- https://github.com/litto/Codeigniter3-Sample-Project-with-CRUD

4 comments:

  1. thank you for tut.It's very simple and useful

    ReplyDelete
  2. I want to do a project codigniter music player use jw player to display the music and video. I hope this project is best . so you can write some tutorials for all beginer . thank you so much.

    ReplyDelete