Codeigniter tutorial – To do list application | KomunitasWeb

September 11, 2009
Filed under: CodeIgniter, Featured 

What is CodeIgniter?

CodeIgniter is a powerful PHP framework with a very small footprint, built for PHP coders who need a simple and elegant toolkit to create full-featured web applications. If you’re a developer who lives in the real world of shared hosting accounts and clients with deadlines, and if you’re tired of ponderously large and thoroughly undocumented frameworks

CodeIgniter has an excellent documentation. It’s really help you in getting started. If you new to CodeIgniter, you can start by reading the user guide or read this excellent article Everything You Need to Get Started With CodeIgniter.

If you’re ready with your CodeIgniter, now we will create complete application using codeigniter. Building to do list in CodeIgniter. Let’s get started.

Open system/application/database.php, and change the following database configuration to fit your mysql configuration.

$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";

Then open system/application/config.php

$config['global_xss_filtering'] = TRUE;

Open system/application/autoload.php, and add database as autoload library so we don’t need to load database in every controllers.

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

Open system/application/routes.php, change default controller to todo controller as we’ll create later on this tutorial.




$route['default_controller'] = 'todo';

Model

First, we create table called todo table, and add some dummy data.

CREATE TABLE IF NOT EXISTS `todo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`description` text,
`status` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY  (`id`)
) ;

INSERT INTO `todo` (`id`, `name`, `description`, `status`) VALUES
(1, 'Buy flower', 'Buy flower for the girl next door', 1),
(2, 'Phone my dad', NULL, 1);

Now we need to create file in /system/application/models/ the new file should be called todo_model.php. After creating the file, add the following code to it.

<?php
class Todo_model extends Model {

    public function __construct()
    {
        // model constructor
        parent::__construct();
    }

    function getCompletedTodo()
    {
        $query = $this->db->get_where('todo', array('status' => '0'));		
        return $query->result();
    }

    function getNextTodo()
    {
        $query = $this->db->get_where('todo', array('status' => '1'));	
        return $query->result();
    }
}
?>

Controller

Create a new file under /system/application/controllers/ called todo.php. And for this tutorial, we only create index function to show our current to do list and completed to do list. Later on, we’ll create more function for our to do list application. Add the following code to your file.

<?php
class Todo extends Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Todo_model');
    }

    function index()
    {
        $data['todo_list'] = $this->Todo_model->getNextTodo();
        $data['completed_list'] = $this->Todo_model->getCompletedTodo();
        $this->load->view('todo/index', $data);
    }
}
?>

View

I really like to organize view page based on controller, so I create todo folder under system/application/views/. Then I create file index.php under system/application/views/todo/

<html>
    <head>
        <title>CodeIgniter To Do List Application</title>
    </head>
<body>

<h1>Welcome to CodeIgniter To Do List Application!</h1>

<h2>Next</h2>
<?php if($todo_list): ?>
<ul>
    <?php foreach($todo_list as $todo): ?>
    <li><h3><?= $todo->name ?></h3>
    <?php if($todo->description): ?><p><?= $todo->description ?></p><?php endif ?>
    </li>
    <?php endforeach ?>
</ul>
<?php else: ?>
None
<?php endif ?>

<h2>Completed</h2>
<?php if($completed_list): ?>
<ul>
    <?php foreach($completed_list as $todo): ?>
    <li><h3><?= $todo->name ?></h3>
    <?php endforeach ?>
</ul>
<?php else: ?>
None
<?php endif ?>
</body>
</html>

That’s it. You can go to your browser, and see the result. Don’t hesitate to ask me, if something goes wrong on your side. In the upcoming tutorial, we’ll add more features. Thank you for reading this tutorial.

Source code for this tutorial : todo.zip

More Info : click here

Comments

One Comment on Codeigniter tutorial – To do list application | KomunitasWeb

  1. umer singhera on Wed, 29th Jun 2011 3:07 am
  2. its the site where this application is using and check it how accurately its working and the outlook of this site is so attractively Codeigniter example Site

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!





*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image