Step 1: Create MySQL Database and Table
//Table structure for table `employees`
CREATE TABLE `employees` (
`id` int(11) NOT NULL COMMENT 'primary key',
`name` varchar(255) NOT NULL COMMENT 'Employee Name',
`last_name` varchar(100) DEFAULT NULL,
`email` varchar(255) NOT NULL COMMENT 'Email Address',
`contact_no` varchar(16) DEFAULT NULL,
`address` text,
`salary` float(10,2) NOT NULL COMMENT 'employee salary'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table';
// Dumping data for table `employees`
INSERT INTO `employees` (`id`, `name`, `last_name`, `email`, `contact_no`, `address`, `salary`) VALUES
(1, 'Nixon', 'Tiger', 'tiger@techarise.com', '9000000001', 'Washington', 3208000.00),
(2, 'Garrett', 'Winters', 'winters@techarise.com', '9000000002', 'New York', 170750.00),
(3, 'Ashton Cox', 'Ashton', 'cox@techarise.com', '9000000003', 'New Jersey', 86000.00),
(4, 'Cedric', 'Kelly', 'kelly@techarise.com', '9000000004', 'Sydney', 433060.00),
(5, 'Airi', 'Satouy', 'airi@techarise.com', '9000000005', 'Canberra', 162700.00),
(6, 'Brielle', 'Williamson', 'will@techarise.com', '9000000006', 'Wallington', 372000.00),
(7, 'Herrod', 'Chandler', 'herrod@techarise.com', '9000000007', 'Germany', 137500.00),
(8, 'Rhona', 'Davidson', 'rd@techarise.com', '9000000008', 'Itly', 327900.00),
(9, 'Colleen', 'Hurst', 'colleen@techarise.com', '9000000009', 'Moscow City', 205500.00),
(10, 'Sonya', 'Frost', 'frost@techarise.com', '9000000010', 'Paris', 103600.00),
(11, 'John', 'Philip', 'filip@techarise.com', '9000000011', 'Landon', 26584.00),
(12, 'Jaeeme', 'Khan', 'khan@techarise.com', '9000000012', 'New Delhi', 26584.00);
// Indexes for table `employees`
ALTER TABLE `employees` ADD PRIMARY KEY (`id`);
// AUTO_INCREMENT for table `employees`
ALTER TABLE `employees` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key', AUTO_INCREMENT=13;
?>
Step 2: Create a model file
Create a model file named "Curd_model.php" inside "application/models" folder.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Curd Model: CodeIgniter CRUD Operations with MySQL
*
* @author TechArise Team
*
* @email info@techarise.com
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Curd_model extends CI_Model {
private $_empID;
private $_firstName;
private $_lastName;
private $_email;
private $_address;
private $_salary;
private $_contactNo;
public function setEmpID($empID) {
$this->_empID = $empID;
}
public function setFirstName($firstName) {
$this->_firstName = $firstName;
}
public function setLastName($lastName) {
$this->_lastName = $lastName;
}
public function setEmail($email) {
$this->_email = $email;
}
public function setAddress($address) {
$this->_address = $address;
}
public function setSalary($salary) {
$this->_salary = $salary;
}
public function setContactNo($contactNo) {
$this->_contactNo = $contactNo;
}
// get Employee List
public function getEmpList() {
$this->db->select(array('e.id', 'e.name', 'e.last_name', 'e.email', 'e.address', 'e.contact_no', 'e.salary'));
$this->db->from('employees e');
$query = $this->db->get();
return $query->result_array();
}
// create new Employee
public function createEmp() {
$data = array(
'name' => $this->_firstName,
'last_name' => $this->_lastName,
'email' => $this->_email,
'address' => $this->_address,
'contact_no' => $this->_contactNo,
'salary' => $this->_salary,
);
$this->db->insert('employees', $data);
return $this->db->insert_id();
}
// update Employee
public function updateEmp() {
$data = array(
'name' => $this->_firstName,
'last_name' => $this->_lastName,
'email' => $this->_email,
'address' => $this->_address,
'contact_no' => $this->_contactNo,
'salary' => $this->_salary,
);
$this->db->where('id', $this->_empID);
$this->db->update('employees', $data);
}
// for display Employee
public function getEmp() {
$this->db->select(array('e.id', 'e.name as first_name', 'e.last_name', 'e.email', 'e.address', 'e.contact_no', 'e.salary'));
$this->db->from('employees e');
$this->db->where('e.id', $this->_empID);
$query = $this->db->get();
return $query->row_array();
}
// delete Employee
public function deleteEmp() {
$this->db->where('id', $this->_empID);
$this->db->delete('employees');
}
}
?>
Step 3: Create a controller file
Next create a controller file named "Curd.php" inside "application/controllers" folder.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* @package Curd : CodeIgniter CRUD Operations with MySQL
*
* @author TechArise Team
*
* @email info@techarise.com
*
* Description of Curd Controller
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Curd extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Curd_model', 'emp');
}
// Employee list method
public function index() {
$data['page'] = 'emp-list';
$data['title'] = 'Employee List | TechArise';
$data['empInfo'] = $this->emp->getEmpList();
$this->load->view('emp/index', $data);
}
// Employee Add method
public function add() {
$data['page'] = 'emp-add';
$data['title'] = 'Employee Add | TechArise';
$this->load->view('emp/add', $data);
}
// Employee save method
public function save() {
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('address', 'Address', 'trim|required');
$this->form_validation->set_rules('contact_no', 'Phone', 'trim|required');
$this->form_validation->set_rules('salary', 'Salary', 'trim|required');
if($this->form_validation->run() == FALSE) {
$this->add();
} else {
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$email = $this->input->post('email');
$address = $this->input->post('address');
$contact_no = $this->input->post('contact_no');
$salary = $this->input->post('salary');
$this->emp->setFirstName($first_name);
$this->emp->setLastName($last_name);
$this->emp->setEmail($email);
$this->emp->setAddress($address);
$this->emp->setSalary($salary);
$this->emp->setContactNo($contact_no);
$this->emp->createEmp();
redirect('/');
}
}
// Employee edit method
public function edit($id='') {
$data['page'] = 'emp-edit';
$data['title'] = 'Employee Edit | TechArise';
$this->emp->setEmpID($id);
$data['empInfo'] = $this->emp->getEmp();
$this->load->view('emp/edit', $data);
}
// Employee update method
public function update() {
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('address', 'Address', 'trim|required');
$this->form_validation->set_rules('contact_no', 'Phone', 'trim|required');
$this->form_validation->set_rules('salary', 'Salary', 'trim|required');
if($this->form_validation->run() == FALSE) {
$this->edit();
} else {
$emp_id = $this->input->post('emp_id');
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$email = $this->input->post('email');
$address = $this->input->post('address');
$contact_no = $this->input->post('contact_no');
$salary = $this->input->post('salary');
$this->emp->setEmpID($emp_id);
$this->emp->setFirstName($first_name);
$this->emp->setLastName($last_name);
$this->emp->setEmail($email);
$this->emp->setAddress($address);
$this->emp->setSalary($salary);
$this->emp->setContactNo($contact_no);
$this->emp->updateEmp();
redirect('/');
}
}
// Employee display method
public function display($id='') {
$data['page'] = 'emp-display';
$data['title'] = 'Employee Display | TechArise';
$this->emp->setEmpID($id);
$data['empInfo'] = $this->emp->getEmp();
$this->load->view('emp/display', $data);
}
// Employee display method
public function delete($id='') {
$this->emp->setEmpID($id);
$this->emp->deleteEmp();
redirect('/');
}
}
?>
Step 4: Change Route file
So open "application/config/routes.php" file and add code like as bellow:
// create routes
$route['add'] = 'curd/add';
$route['edit/(:any)'] = 'curd/edit/$1';
$route['display/(:any)'] = 'curd/display/$1';
$route['delete/(:any)'] = 'curd/delete/$1';
?>
Step 5: Create a view
Create a view file named "add.php" inside "application/views/emp" folder
$this->load->view('templates/header');
?>
CodeIgniter CRUD Operations with MySQL Example
$this->load->view('templates/footer');
?>
Step 6: Create a view
Create a view file named "index.php" inside "application/views/emp" folder
$this->load->view('templates/header');
?>
CodeIgniter CRUD Operations with MySQL Example
$this->load->view('templates/footer');
?>
Step 7: Create a view
Create a view file named "edit.php" inside "application/views/emp" folder
$this->load->view('templates/header');
$id = $empInfo['id'] ? $empInfo['id'] : '';
$first_name = $empInfo['first_name'] ? $empInfo['first_name'] : '';
$last_name = $empInfo['last_name'] ? $empInfo['last_name'] : '';
$email = $empInfo['email'] ? $empInfo['email'] : '';
$address = $empInfo['address'] ? $empInfo['address'] : '';
$contact_no = $empInfo['contact_no'] ? $empInfo['contact_no'] : '';
$salary = $empInfo['salary'] ? $empInfo['salary'] : '';
?>
CodeIgniter CRUD Operations with MySQL Example
$this->load->view('templates/footer');
?>
Step 8: Create a view
Create a view file named "display.php" inside "application/views/emp" folder
$this->load->view('templates/header');
$first_name = $empInfo['first_name'] ? $empInfo['first_name'] : '';
$last_name = $empInfo['last_name'] ? $empInfo['last_name'] : '';
$email = $empInfo['email'] ? $empInfo['email'] : '';
$address = $empInfo['address'] ? $empInfo['address'] : '';
$contact_no = $empInfo['contact_no'] ? $empInfo['contact_no'] : '';
$salary = $empInfo['salary'] ? $empInfo['salary'] : '';
?>
CodeIgniter CRUD Operations with MySQL Example
First Name:
Last Name:
Email:
Address:
Phone:
Salary:
$this->load->view('templates/footer');
?>
Demo [sociallocker] Download[/sociallocker]