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

Trainer Primavera P6 Project Management or Methodology Management

Hery Purnama 081.223344.506 is a Freelance Inhouse Trainer for Primavera P6 Project Portfolio Management , 15 years experience as Project Management Concept , MS. Project , Primavera P6 freelance Trainer Inhouse trainer for location in Bandung, Jakarta, Bali, Yogya, Batam, Medan, Balikpapan, Samarinda, Makasar, Surabaya and other cities in indonesia For inquiry please Call/ SMS/ Whatsapp to 081.223344.506 , email : purnamainfo or visit http://freelance-it-trainer.blogspot.com , http://good.web.id

Wow !, #infoproperti withdrawal complete : syahrul, Rp 840000, 2017-02-21 [token:963]

Wow Luar biasa !, agen Syahrul Rahmat Hadi telah sukses menarik komisinya untuk ke sekian kalinya sebagai member dari Program Agen Milyuner www.propertilink.com sebesar Rp 840000 pada tanggal 2017-02-21 (YYYY-MM-DD) , Apakah Anda berikutnya ?, Ikuti jejak agen Syahrul Rahmat Hadi di www.propertilink.com, daftar sekarang dan posting iklan properti Anda sepuasnya tanpa batas serta raih pasif income yang sesungguhnya dan mengalir terus tak terbatas dengan bantuan mesin promo otomatis kami. (kami yang pertama memperkenalkan teknologi mesin promo otomatis di Indonesia !). Program Agen Milyuner ini Terbuka untuk siapapun Ingin beriklan properti gratis ?, tersedia juga di www.propertilink.com , ayo jadi yang terdepan , semangat langkah Anda menentukan masa depan Anda dan keluarga ! #agenmilyuner #iklanproperti #iklanpropertigratis #propertilink #agenproperti #jualsewaproperti [Message Server propertilink dot com security token:963], Automatic Engine

TRAINER IN JAKARTA - ITIL V.4

What's new in ITIL V.4 ITIL 4 is the latest version of the ITIL framework and was released back in February 2019. It is highly value-centric, primarily focusing on bringing different stakeholders in an organization together to co-create value for the end-users. With the arrival of new frameworks like VeriSM, SIAM, and FitSM in the IT service management landscape, there was a need for the previous version of ITIL (ITIL v3) to become more refined in the way it approaches service management as an enabler for businesses. ITIL 4, the current version of ITIL takes the best parts of ITIL v3, enhances them, and creates a framework that focuses more on delivering tailor-made solutions for organizations. The previous versions of ITIL directed practitioners to implement ITIL by the book which led them to a complex web of practical solutions. Whereas ITIL 4, is more pragmatic in this approach. Instead of idealizing processes and encouraging the practitioners to impl...