0% found this document useful (0 votes)
7 views7 pages

Mega Assignment Asad

The document describes a CRUD (create, read, update, delete) application built with CodeIgniter. It includes a model for managing users, a controller to handle requests and display views, and basic views for the index, create, edit and delete pages. The model defines methods for getting, creating, updating and deleting users from the database. The controller calls the model methods and passes data to views. The views display a list of users, a form to create a new user, and forms to edit or delete an existing user.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Mega Assignment Asad

The document describes a CRUD (create, read, update, delete) application built with CodeIgniter. It includes a model for managing users, a controller to handle requests and display views, and basic views for the index, create, edit and delete pages. The model defines methods for getting, creating, updating and deleting users from the database. The controller calls the model methods and passes data to views. The views display a list of users, a form to create a new user, and forms to edit or delete an existing user.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Asad Jamil

Mega Assignment Sap 38807


Submitted To: Mr Mohsin

Create A complete CRUD using CodeIgniter Framework

Model :
1. <?php
2.
3. namespace App\Models;
4.
5. use CodeIgniter\Model;
6.
7. class CrudModel extends Model
8. {
9. // Table
10. protected $table = 'contact_details';
11. // allowed fields to manage
12. protected $allowedFields = ['firstname', 'middlename','lastname',
'gender', 'contact', 'email', 'address'];
13. }

14. public function get_users() {


15. return $this->db->get('users')->result();
16. }

17. public function get_user($id) {


18. return $this->db->get_where('users', array('id' => $id))->row();
19. }

20. public function create_user($data) {


21. $this->db->insert('users', $data);
22. return $this->db->insert_id();
23. }

24. public function update_user($id, $data) {


25. $this->db->where('id', $id);
26. $this->db->update('users', $data);
27. }

28. public function delete_user($id) {


29. $this->db->where('id', $id);
30. $this->db->delete('users');
31. }
32. }
33. ?>

Controller:
1. <?php
2.
3. namespace App\Controllers;
4.
5. use App\Models\CrudModel;
6.
7. class Main extends BaseController
8. {
9. // Session
10. protected $session;
11. // Data
12. protected $data;
13. // Model
14. protected $crud_model;
15.
16. // Initialize Objects
17. public function __construct(){
18. $this->crud_model = new CrudModel();
19. $this->session= \Config\Services::session();
20. $this->data['session'] = $this->session;
21. }
22.
23. // Home Page
24. public function index(){
25. $this->data['page_title'] = "Home Page";
26. echo view('templates/header', $this->data);
27. echo view('crud/home', $this->data);
28. echo view('templates/footer');
29. }
30.
31. // Create Form Page
32. public function create(){
33. $this->data['page_title'] = "Add New";
34. $this->data['request'] = $this->request;
35. echo view('templates/header', $this->data);
36. echo view('crud/create', $this->data);
37. echo view('templates/footer');
38. }
39.
40. // Insert And Update Function
41. public function save(){
42. $this->data['request'] = $this->request;
43. $post = [
44. 'firstname' => $this->request->getPost('firstname'),
45. 'middlename' => $this->request->getPost('middlename'),
46. 'lastname' => $this->request->getPost('lastname'),
47. 'gender' => $this->request->getPost('gender'),
48. 'contact' => $this->request->getPost('contact'),
49. 'email' => $this->request->getPost('email'),
50. 'address' => $this->request->getPost('address')
51. ];
52. if(!empty($this->request->getPost('id')))
53. $save = $this->crud_model->where(['id'=>$this->request-
>getPost('id')])->set($post)->update();
54. else
55. $save = $this->crud_model->insert($post);
56. if($save){
57. if(!empty($this->request->getPost('id')))
58. $this->session->setFlashdata('success_message','Data has been
updated successfully') ;
59. else
60. $this->session->setFlashdata('success_message','Data has been added
successfully') ;
61. $id =!empty($this->request->getPost('id')) ? $this->request-
>getPost('id') : $save;
62. return redirect()->to('/main/view_details/'.$id);
63. }else{
64. echo view('templates/header', $this->data);
65. echo view('crud/create', $this->data);
66. echo view('templates/footer');
67. }
68. }
69.
70. // List Page
71. public function list(){
72. $this->data['page_title'] = "List of Contacts";
73. $this->data['list'] = $this->crud_model->orderBy('date(date_created)
ASC')->select('*')->get()->getResult();
74. echo view('templates/header', $this->data);
75. echo view('crud/list', $this->data);
76. echo view('templates/footer');
77. }
78.
79. // Edit Form Page
80. public function edit($id=''){
81. if(empty($id)){
82. $this->session->setFlashdata('error_message','Unknown Data ID.') ;
83. return redirect()->to('/main/list');
84. }
85. $this->data['page_title'] = "Edit Contact Details";
86. $qry= $this->crud_model->select('*')->where(['id'=>$id]);
87. $this->data['data'] = $qry->first();
88. echo view('templates/header', $this->data);
89. echo view('crud/edit', $this->data);
90. echo view('templates/footer');
91. }
92.
93. // Delete Data
94. public function delete($id=''){
95. if(empty($id)){
96. $this->session->setFlashdata('error_message','Unknown Data ID.') ;
97. return redirect()->to('/main/list');
98. }
99. $delete = $this->crud_model->delete($id);
100. if($delete){
101. $this->session->setFlashdata('success_message','Contact Details has
been deleted successfully.') ;
102. return redirect()->to('/main/list');
103. }
104. }
105.
106. // View Data
107. public function view_details($id=''){
108. if(empty($id)){
109. $this->session->setFlashdata('error_message','Unknown Data ID.') ;
110. return redirect()->to('/main/list');
111. }
112. $this->data['page_title'] = "View Contact Details";
113. $qry= $this->crud_model->select("*, CONCAT(lastname,',
',firstname,COALESCE(concat(' ', middlename), '')) as `name`")-
>where(['id'=>$id]);
114. $this->data['data'] = $qry->first();
115. echo view('templates/header', $this->data);
116. echo view('crud/view', $this->data);
117. echo view('templates/footer');
118. }
119.
120. }

Interface for application :


Index.php
<!DOCTYPE html>
<html>
<head>
<title>Users</title>
</head>
<body>
<h1>Users</h1>
<a href="<?php echo base_url('users/create'); ?>">Create User</a>
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Actions</th>
</tr>
<?php foreach ($users as $user) { ?>
<tr>
<td><?php echo $user->name; ?></td>
<td><?php echo $user->email; ?></td>
<td>
<a href="<?php echo base_url('users/edit/'.$user->id); ?>">Edit</a>
<a href="<?php echo base_url('users/delete/'.$user->id); ?>">Delete</a>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
Create.php
<!DOCTYPE html>
<html>
<head>
<title>Create User</title>
</head>
<body>
<h1>Create User</h1>
<form action="<?php echo base_url('users/store'); ?>" method="post">
<label>Name:</label>
<input type="text" name="name" required><br><br>
<label>Email:</label>
<input type="email" name="email" required><br><br>
<input type="submit" value="Create">
</form>
</body>
</html>
Edit.php
<!DOCTYPE html>
<html>
<head>
<title>Edit User</title>
</head>
<body>
<h1>Edit User</h1>
<form action="<?php echo base_url('users/update/'.$user->id); ?>"
method="post">
<label>Name:</label>
<input type="text" name="name" value="<?php echo $user->name; ?>"
required><br><br>
<label>Email:</label>
<input type="email" name="email" value="<?php echo $user->email; ?>"
required><br><br>
<input type="submit" value="Update">
</form>
</body>
</html>

You might also like