CodeIgniter 3 is a popular PHP framework that allows developers to build web applications quickly and easily. One of the most common types of web applications is a CRUD (Create, Read, Update, Delete) application, which allows users to perform basic database operations on a set of data.
In this context, creating a CRUD application with CodeIgniter 3 involves setting up a database, defining a model to interact with the database, creating views to display and manipulate data, and defining controller methods to handle HTTP requests.
Here’s a simple CRUD (Create, Read, Update, Delete) application using CodeIgniter 3:
- Create a database with the name “ci_crud”.
- Create a “users” table in the “ci_crud” database with the following structure:
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`phone` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- In your CodeIgniter 3 project, create a controller called “Users” with the following methods:
<?php
class Users extends CI_Controller {
public function index() {
$this->load->model('User_model');
$data['users'] = $this->User_model->get_users();
$this->load->view('users/index', $data);
}
public function create() {
$this->load->view('users/create');
}
public function store() {
$this->load->model('User_model');
$this->User_model->insert_user();
redirect('users');
}
public function edit($id) {
$this->load->model('User_model');
$data['user'] = $this->User_model->get_user($id);
$this->load->view('users/edit', $data);
}
public function update($id) {
$this->load->model('User_model');
$this->User_model->update_user($id);
redirect('users');
}
public function delete($id) {
$this->load->model('User_model');
$this->User_model->delete_user($id);
redirect('users');
}
}
?>
- Create a model called “User_model” with the following methods:
<?php
class User_model extends CI_Model {
public function get_users() {
return $this->db->get('users')->result();
}
public function get_user($id) {
return $this->db->get_where('users', ['id' => $id])->row();
}
public function insert_user() {
$data = [
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone')
];
$this->db->insert('users', $data);
}
public function update_user($id) {
$data = [
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone')
];
$this->db->where('id', $id);
$this->db->update('users', $data);
}
public function delete_user($id) {
$this->db->where('id', $id);
$this->db->delete('users');
}
}
?>
- Create the following views in the “application/views/users” directory:
1- index.php
<h1>Users</h1>
<p><a href="<?= site_url('users/create') ?>">Create User</a></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?= $user->name ?></td>
<td><?= $user->email ?></td>
<td><?= $user->phone ?></td>
<td>
<a href="<?= site_url('users/edit/'.$user->id) ?>">Edit</a>
<a href="<?= site_url('users/delete/'.$user->id) ?>" onclick="return confirm('Are you sure you want to delete this user?')">Delete</a>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
2- create.php
<h1>Create User</h1>
<?= form_open('users/store') ?>
<p>
<label>Name</label>
<input type="text" name="name" required>
</p>
<p>
<label>Email</label>
<input type="email" name="email" required>
</p>
<p>
<label>Phone</label>
<input type="tel" name="phone" required>
</p>
<p>
<button type="submit">Create</button>
<a href="<?= site_url('users') ?>">Cancel</a>
</p>
<?= form_close() ?>
3- edit.php
<h1>Edit User</h1>
<?= form_open('users/update/'.$user->id) ?>
<p>
<label>Name</label>
<input type="text" name="name" value="<?= $user->name ?>" required>
</p>
<p>
<label>Email</label>
<input type="email" name="email" value="<?= $user->email ?>" required>
</p>
<p>
<label>Phone</label>
<input type="tel" name="phone" value="<?= $user->phone ?>" required>
</p>
<p>
<button type="submit">Update</button>
<a href="<?= site_url('users') ?>">Cancel</a>
</p>
<?= form_close() ?>
That’s it! You should now be able to create, read, update, and delete users using CodeIgniter 3.
Comments