TUTORIAL SAMPLE CRUD WITH CODEIGNITER 2.0
start by create sample mysql database bellowed :
application/views/personadd.php
application/views/personedit.php
application/views/personlist.php
application/controllers/person.php
application/model/personModel.php
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
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
Post a Comment
Silahkan isi komentar atau iklan baris Anda, Jangan lupa visit social media kami di FB/Twitter/Instagram @alamatclick