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

Sewa Ruang Meeting Daerah Citarum Bandung

Sewa ruang meeting kantor, training di Bandung Hubungi 0816-4038-78. Lokasi Ruang Meeting berada di Jl.Pelajar Pejuang 45 No.23B 1. Paket per Jam      - Biaya : Rp.50.000 / org / jam     - Fasilitas : Snack & Drink 2. Paket Half-Day     - Waktu : 09.00 s/d 13.00 Wib     - Biaya : Rp.120.000 / org      - Fasilitas : Makan siang 1x, Snack & Drink 3. Paket Full Day     - Waktu : 09.00 s/d 16.00 Wib     - Biaya : Rp.170.000 / org      - Fasilitas : Makan siang 1x, Coffe break 2x, Snack & Drink 4. Lain-lain     - Sewa Proyektor : Rp.150.000/day     - Extra time : Rp.100.000/Jam Syarat dan Ketentuan : 1. Min. 3org dan Max. 7org 2. Pembayaran H-3 sebelum tgl pelaksanaan 3. Melebihi jam ketentuan dikenakan biaya tambahan Sewa Ruang Meeting Daerah Citarum Bandung Hubungi 0816-4038-78 Ruang meeting murah mulai 50.000 di Buah Batu, Pasteur, Sukajadi, Lengkong, Antapani, Uj...

TRAINING PROJECT MANAGEMENT WITH PRIMAVERA P6-BANDUNG

TRAINING PROJECT MANAGEMENT WITH PRIMAVERA P6 By Purnama Academy - Training Center January 11, 2018  No comments PROJECT MANAGEMENT WITH PRIMAVERA P6 Syllabus Overview PRIMAVERA P6 adalah project management tool yang sangat populer digunakan hampir di perusahan perusahaan skala internasional dan multinasional yang sedang menjalankan project skala besar, lisensi atas PRIMAVERA P6 kini dimiliki oleh Vendor Oracle dan database yang disupport Primavera adalah SQL Server dan Oracle database. Sebagai sebuah software Project Tool skala enterprise maka Primavera memiliki 2 module di dalamnya yaitu (Methodology dan Project Management) Class Type : Training Duration : 4 Days (09.00 – 17.00) Venue  & Price : Click Here , Registration : Click Here  (Save up to 20% for Early Bird Registration !) Detail Syllabus or request for inhouse training  please contact Purnama Academy sales team info@purnamaacademy.com , Hotline (Call/SMS/Whatsapp) : 0838-0838-0001 Speaker  : Purn...