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
Related posts:
- CodeIgniter Tutorial – To do list application#2
- CodeIgniter Tutorial – To do list application part 3
- CodeIgniter Tutorial – ORM with DataMapper
- CodeIgniter Tutorials to Help You Get Started
- CakePHP ACL Tutorial – What and How
Tags: codeigniter
March 15th, 2009 | Comments(7)


[...] Gilang Chandrasa added an interesting post on Codeigniter tutorial – To do list application « KomunitasWebHere’s a small excerptBuilding 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’] … [...]
Pingback by Internet Marketing Email » Blog Archive » Codeigniter tutorial - To do list application « KomunitasWeb — March 15, 2009 @ 10:17 pm
Thanks for the tutorial. This is the first time I’ve tried anything with CI, and I’m excited about the possibilities.
Unfortunately, after implementing each step of the tutorial, I get the following error message:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /home/foundsce/public_html/ci/system/application/controllers/todo.php on line 4
I’m not sure what I’m doing wrong, but any help you could provide would be greatly appreciated.
Again … thanks!
Comment by Erik N. — March 17, 2009 @ 12:16 am
@Erik N, Can you verify that you’re using PHP5? If you do using PHP5, then maybe there is some typo in you code.
Comment by Gilang Chandrasa — March 17, 2009 @ 12:32 am
Update : source code for the tutorial now available for download.
Comment by Gilang Chandrasa — March 17, 2009 @ 12:38 am
Thanks Gilang. I am on PHP5, so I must have something wrong in my code. Thanks for uploading your source code. I’ll dig through it tonight and see if I can figure out where I’m going wrong.
Comment by Erik N. — March 17, 2009 @ 8:06 pm
Story added…
Your story was featured in MVCForge – News for MVC Web Developers! Here is the link to vote it up and promote it: http://mvcforge.com/codeigniter/Codeigniter-tutorial-To-do-list-application...
Trackback by MVCForge - News for MVC Web Developers — March 18, 2009 @ 8:37 am
its nice its usefull for me at this moument, thank you very much!
http://twitter.com/mayoosuf
Comment by Yoosuf — March 23, 2009 @ 12:55 am