CodeIgniter Tutorial – To do list application part 3

March 18th, 2009

ci_logo_flame

Let’s take a look at our to do list application.

By now, you have familiarize your self with CodeIgniter framework. It is take a while to know everything, but if you keep learning then someday you will get there.

If you have completed last tutorial, then you have a to do list application based on CodeIgniter framework. It’s not complete, but at least working.

But if there is something you don’t understand, you could stop by, give a comment or ask me. Or maybe you want to correct me. That’s fine. We all learning here.

Let’s go build another features.

Model

Open your todo_model.php and add 3 new functions.

  • function get – Get to do based on id
  • function delete – Delete to do based on id
  • function setComplete – Set to do as complete
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
class Todo_model extends Model {
 
    ...
 
    function get($id) 
    {
        $query = $this->db->get_where('todo', array('id'=>$id));  
        if ($query->num_rows()==0) {
            return false;
        }
        $result = $query->result();
        return $result[0];
    }
 
    function delete($id)
    {
        $query = $this->db->get_where('todo', array('id'=>$id));  
 
        if ($query->num_rows()==0) {
            return false;
        }
        else {
            $this->db->delete('todo', array('id' => $id)); 
            return true;
        }    
    }
 
    function setComplete($id)
    {
        $query = $this->db->get_where('todo', array('id'=>$id));  
 
        if ($query->num_rows()==0) {
            return false;
        }
        else {
            $this->db->update('todo', array('status' => 0), array('id' => $id));
            return true;
        }   
    }
}
?>

Controller

We will add 2 function to controller, delete function and complete function. We give parameter $id to both of them, so we know which to do list we need to delete or update as complete.

Open your todo controller, todo.php and add the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
class Todo extends Controller {
 
    ...
 
    function delete($id)
    {
        $data = $this->Todo_model->get($id);
 
        if ($this->Todo_model->delete($id)) {
            $this->session->set_flashdata('message', "Done. You have deleted $data->name.");                        
        } else {
            $this->session->set_flashdata('message', "No data found. You deleted wrong to do list."); 
        }
        redirect('');
 
    }
 
    function complete($id)
    {
        $data = $this->Todo_model->get($id);
 
        if ($this->Todo_model->setComplete($id)) {
            $this->session->set_flashdata('message', "You have set $data->name as complete.");                        
        } else {
            $this->session->set_flashdata('message', "No data found. You access wrong to do list.");  
        }
        redirect('');
    }
}
?>

View

We need to update view for to do listing, we add delete and mark as complete link.

Open your index.php in your todo view, system/application/views/todo/index.php and update it the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<html>
    <head>
        <title>CodeIgniter To Do List Application</title>
    </head>
<body>
 
<h1>Welcome to CodeIgniter To Do List Application!</h1>
 
<?= anchor('todo/add/', 'Add new to do', array('title' => 'Add new to do!')); ?>
 
<p><?= $flash_message ?></p>
 
<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 ?>
    <p><?= anchor("todo/delete/$todo->id", 'delete'); ?> | <?= anchor("todo/complete/$todo->id", 'mark as complete'); ?></p>
    </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 if($todo->description): ?><p><?= $todo->description ?></p><?php endif ?>
    <?php endforeach ?>
</ul>
<?php else: ?>
None
<?php endif ?>
</body>
</html>

In each tutorial we will add more features until we have complete application. So subscribe to KomunitasWeb and see you in the next tutorial.

Source code for this tutorial : todo3.zip

Related posts:

  1. Codeigniter tutorial – To do list application
  2. CodeIgniter Tutorial – To do list application#2
  3. CodeIgniter Tutorials to Help You Get Started
  4. CodeIgniter Tutorial – ORM with DataMapper
  5. CakePHP ACL Tutorial – What and How

Tags: ,

2 Comments

  • Zack Curl says:

    This tutorial is very interesting, thanks a bunch for sharing!

  • Mike says:

    Great tutorial. However, i found i had to do a couple of things differently to get it to work.
    1. had to load->database()
    2. from function index i could not load view todo/index.php. had
    to rename index. php to something else(i would get error about
    EXT already defined
    3. redirect i had to specify todo or else i would get the page that
    indicates codeigniter installed correctly

    That said, this is really helping me get my hands around CI.
    Thanks very much and keep up the good work.