Skip to main content

Very Simple Sample CRUD codeigniter 2

TUTORIAL SAMPLE CRUD WITH CODEIGNITER 2.0

start by create sample mysql database bellowed :

CREATE TABLE IF NOT EXISTS `tbl_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(50) DEFAULT NULL, `gender` char(1) DEFAULT NULL, `dob` date DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ; INSERT INTO `tbl_person` (`id`, `name`, `gender`, `dob`) VALUES (1, 'henrihnr', 'm', '1985-09-09'), (2, 'name_001', 'm', '1990-01-01'), (3, 'name_002', 'f', '2000-01-01'), (4, 'name_003', 'm', '2000-02-02'), (5, 'name_005', 'f', '2000-04-04'), (6, 'khushbu', 'f', '1983-08-10'), (10, 'rahul mehta', 'm', '1983-05-16'), (11, 'jatin', 'm', '1983-05-16'); 


application/views/personadd.php

<html> <head> <title>person add</title> </head> <body> <h1>Person add</h1><a href="/">list</a> <form name="add" method="POST" action="insert"> <table border="1"> <tr> <th>name</th> <td><input type="text" name="name"/></td> <tr> <th>gender </th> <td><input type="text" name="gender"/></td> <tr> <th>dob </th> <td><input type="text" name="dob"/></td> </tr> <tr> <td><input type="submit"></td> <td><input type="reset"></td> </tr> </form> </table> </body </html>


application/views/personedit.php

<html> <head> <title>person edit</title> </head> <body> <h1>Person edit</h1><a href="/">list</a> <form name="add" method="POST" action="/index.php/person/update"> <input type="hidden" name="id" value="<?php echo $person[0]->id ?>"> <table border="1"> <tr> <th>name</th> <td><input type="text" name="name" value="<?php echo $person[0]->name;?>"/></td> <tr> <th>gender </th> <td><input type="text" name="gender" value="<?php echo $person[0]->gender;?>"/></td> <tr> <th>dob </th> <td><input type="text" name="dob" value="<?php echo $person[0]->dob;?>"/></td> </tr> <tr> <td><input type="submit"></td> <td><input type="reset"></td> </tr> </form> </table> </body </html>


application/views/personlist.php

<html> <head> <title>person list</title> </head> <body> <h1>Person list</h1><a href="/index.php/person/add">Add</a> <table border="1"> <tr> <th>name</th> <th>gender </th> <th>dob </th> <th>edit </th> <th>delete </th> </tr> <?php //print_r($persons); for($i=0; $i<count($persons);$i++) { ?> <tr> <td><?php echo $persons[$i]->name;?></td> <td><?php echo $persons[$i]->gender;?></td> <td><?php echo $persons[$i]->dob;?></td> <td><a href="/index.php/person/edit/<?php echo $persons[$i]->id;?>">edit</a></td> <td><a href="/index.php/person/delete/<?php echo $persons[$i]->id;?>" onclick="return confirm('are you sure to delete')">delete</a></td> </tr> <?php }?> </table> </body> </html>


application/controllers/person.php

<?php class Person extends CI_Controller { public function index() { $this->load->database(); $this->load->model('PersonModel'); $persons=$this->PersonModel->get_last_ten_entries(); $this->load->view('personlist',array('persons'=>$persons)); } public function add() { //$this->load->database(); //$this->load->model('PersonModel'); $this->load->view('personadd'); } public function edit($id) { $this->load->database(); $this->load->model('PersonModel'); $person=$this->PersonModel->get($id); $this->load->view('personedit',array('person'=>$person)); } public function insert() { $this->load->database(); $this->load->model('PersonModel'); $this->PersonModel->insert_entry(); $persons=$this->PersonModel->get_last_ten_entries(); $this->load->view('personlist',array('persons'=>$persons)); } public function update() { $this->load->database(); $this->load->model('PersonModel'); $this->PersonModel->update_entry(); $persons=$this->PersonModel->get_last_ten_entries(); $this->load->view('personlist',array('persons'=>$persons)); } public function delete($id) { $this->load->database(); $this->load->model('PersonModel'); $this->PersonModel->delete_entry($id); $persons=$this->PersonModel->get_last_ten_entries(); $this->load->view('personlist',array('persons'=>$persons)); } } ?>


application/model/personModel.php

<?php class PersonModel extends CI_Model { var $gender = ''; var $name = ''; var $dob = ''; function __construct() { // Call the Model constructor parent::__construct(); } function get_last_ten_entries() { $query = $this->db->get('tbl_person', 100); return $query->result(); } function get($id){ echo $id; $sql = "SELECT * FROM tbl_person WHERE id = ?"; $query =$this->db->query($sql, array($id)); echo $this->db->last_query(); return $query->result(); } function insert_entry() { $this->name = $_POST['name']; // please read the below note $this->gender = $_POST['gender']; $this->dob = $_POST['dob']; $this->db->insert('tbl_person', $this); } function update_entry() { $this->name = $_POST['name']; // please read the below note $this->gender = $_POST['gender']; $this->dob = $_POST['dob']; $this->db->update('tbl_person', $this, array('id' => $_POST['id'])); } function delete_entry($id) { $this->db->delete('tbl_person', array('id' => $id)); } } ?>


quoted by

SISINDOTEK - IT Training & Solution Provider

Office 1 : Jl. Pelajar Pejuang 45 No.23 , Lt.2 Bandung - Jawa Barat

Office 2 : Jl. Sukasenang VI-6B Bandung - Jawa Barat 40124
Tel. 022-71242266, SMS. 0812.8733.1966
info , YM. sisindotek
www.sisindotek.com , facebook.com/sisindotek , twitter.com/sisindotek

Comments

Popular posts from this blog

Download Slank Full Album lagi sedih 4shared

Untuk para slanker, salah satu album legendaris slank lagi sedih 1997, download gratis di 4shared.com http://www.4shared.com/get/HsDEfGc6/slank-lagi_sedih.html HERY PURNAMA Certified IT Trainer http://www.inhousetrainer.net , http://www.purnama.info hery.purnama@gmail.com , hery@purnama.info  Call/ SMS/WA :  081.223344.506    Blog :   inhousetrainer.blogspot.com   freelance-it-trainer.blogspot.com    Social Net :   facebook.com/inhousetrainer   twitter.com/inhousetrainer   instagram.com/inhousetrainer Hometown : Bandung - Jawa Barat Please consider your environmental responsibility. Before printing this e-mail message, ask yourself whether you really need a hard copy.

Laravel Training Purnama Academy

Nov 2018 Purnama Academy 0838-0838-0001 Kursus Laravel Training Center INdonesia, jakarta, Bandung, Bogor, Semarang, Yogya, Palembang kalimantan, surabaya visit www.purnamaacademy.com , Laravel - Online Courses, Classes, Training, Tutorials on Lynda https://www.lynda.com/Laravel-training-tutorials/2779-0.html Learn how to use Laravel, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Top Laravel Courses Online - Updated November 2018 | Udemy https://www.udemy.com/topic/laravel/ Whether you're interested in developing web apps in the Laravel PHP framework, or building a RESTful API with Laravel, Udemy has a course to help you ... PHP Laravel Framework | Brainmatics.Com brainmatics.com/php-laravel-framework/ Translate this page Laravel merupakan framework PHP yang dirancang untuk membangun situs web ... Pada training ini diharapkan mampu membantu meningkatkan kemampuan ... Dec 3 - Dec 6 Brainmatics Laravel training - Laracasts htt...

TRAINING SQL Server 2017 DATABASE ADMINISTRATOR-BANDUNG

TRAINING SQL Server 2017 DATABASE ADMINISTRATOR By Purnama Academy - Training Center January 18, 2018  No comments SQL Server 2017 DATABASE ADMINISTRATOR Syllabus Class Type           : Training Duration              : 4 Days (09.00 – 17.00) Training Description : Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications—which may run either on the same computer or on another computer across a network Database administrators (DBAs) use specialized software to store and organize data. The role may include capacity planning, installation, configuration, database design, migration, performance monitoring, security, troubleshooting, as well as backup and data recovery Call us for detail syllabus ! ...