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

SEO , Top Keyword Research 2015

Google's Hummingbird update created a lot of anxiety, but ultimately, it could be a good thing for the industry, because it frees us from the tyranny of competing for a limited number of top keywords. Essentially, the role of the Hummingbird algorithm is to better answer those longer-tail queries users are typing in Google. If your pages are optimized for these more conversational queries, you have a better chance of top rankings. Try a new, niche-based approach to keywords, which allows you to double or even triple the list of profitable keywords in your SEO arsenal. This article explains the four steps for doing keyword research the modern way, using SEO PowerSuite or other tools. 1. Ideas: Most search marketers simply think of the main keywords related to their businesses, plop them into a tool like Google Keyword Planner, and then run with the keyword list it delivers. However, search habits vary widely: Searchers may use hundreds of different word combinations to describe the ...

Training Search Engine Optimization (SEO) and Internet Marketing

PURNAMAACADEMY.com (0838-0838-0001) info Jadwal silabus : TRAINING SEARCH ENGINE OPTIMIZATION (SEO) AND INTERNET MARKETING (3 HARI), Kegiatan pelatihan/ training akan dilaksanakan selama 3 Hari, dengan pilihan lokasi Jakarta, Bandung , Surabaya, Yogya dan Bali. Purnama Academy Training (0838-0838-0001) merupakan salah satu penyelenggara training IT dan Manajemen terbaik di Bandung Jakarta Surabaya dan Bali serta beberapa kota lainnya, selain topik training diatas Anda juga bisa mengikuti beberapa pelatihan populer kami lainnya seperti Excel VBA Macro, Leadership, BUsiness Analisys , UML, Software Testing, Autocad, Google Map API, Power BI for Business User, ITIL foundation, COBIT 5 Foundation, Yii Framework, Laravel , Microsoft Project, Primavera dan Magento. Kegiatan Training tersedia atas 3 Paket (Full Day : 09.00 - 16.00, Night Class : 16.30 - 21.00, Weekend Class : 09.00 - 21.00). Hubungi tim Purnama Academy untuk informasi lebih detail terkait silabus, waktu pelaksanaan dan biaya...

TRAINING UNITY 3D GAME ADVANCED : WORKING WITH FIREBASE-BANDUNG

TRAINING UNITY 3D GAME ADVANCED : WORKING WITH FIREBASE By Purnama Academy - Training Center January 08, 2018  No comments UNITY 3D GAME ADVANCED : WORKING WITH FIREBASE www.purnamaacademy.com , Hotline (Call/SMS/WA) :  0838-0838-0001 Syllabus Overview Training yang membahas tingkatan lanjut dari pengembangan game 3D menggunakan UNITY 3D baik skala desktop ataupun mobile, dimana peserta mengetahui cara cara menyimpan informasi data game dari player ke dalam backend dengan bantuan firebase SDK, hal ini memungkinkan untuk pengembangan game skala jaringan (online) Class Type : Training Duration : 2 Days (09.00 – 17.00) Venue / Price : Click Here , Registration : Click Here  (Save up to 20% for Early Bird Registration !) Description : Firebase is Google's mobile platform that helps you quickly develop high-quality apps and grow your business A dependable backend is a must-have for today's games. Giving users the ability to log in and save and retrieve player data can make or...