-
Video : Showing all the results in Codeigniter 4
-
Video : Update the record in Codeigniter 4
-
Video : Delete operation in Codeigniter 4
-
Video : Adding Bootstrap 4 in Codeigniter 4 in the Crud Application
-
Video : Create the form in Codeigniter 4 and insert the record
-
Video : Conclusion of CRUD operation in codeigniter 4
Conclusion of CRUD operation in codeigniter 4
Please Login and enroll in this course to view the lesson.
We discuss the things we covered in this course, The CRUD operation in Codeigniter 4 with Bootstrap 4. We give you an overview of the entrire system which you have created in this series.
Here is your whole controller for the CRUD application in Codeigniter 4
<?php
namespace AppControllers;
use AppModelsMyStudents;
use CodeIgniterConfigConfig;
use CodeIgniterController;
class Students extends Controller
{
public function index()
{
$session = ConfigServices::session();
$message = $session->getFlashdata('message');
$std = new MyStudents();
$data['students'] = $std->findAll();
$pager = ConfigServices::pager();
$data['message'] = $message;
echo view('students',$data);
//var_dump($results);
//echo 'index Students';
}
public function newstudent()
{
echo view('newstudent');
//echo ' new method';
}
public function addstudent()
{
$request = ConfigServices::request();
$session = ConfigServices::session();
$name = $request->getPost('std');
$subject = $request->getPost('subject');
$newStudent = [
's_name'=>$name,
's_subject'=>$subject,
];
$std = new MyStudents();
$result = $std->insert($newStudent);
if ($result) {
$session->setFlashdata('message','You have successfully inserted the student.');
}
else{
$session->setFlashdata('message','Oops something went wrong please try again.');
}
return redirect()->to(site_url('students'));
//var_dump($result);
//echo 'working..';
}
public function editstudent($userId = null)
{
$session = ConfigServices::session();
if (!empty($userId)) {
$std = new MyStudents();
$result = $std->where('s_id',$userId)->findAll();
if (count($result) > 0) {
$data['student'] = $result;
echo view('editStudent',$data);
}
else{
$session->setFlashdata('message','The Student is not exist');
return redirect()->to(site_url('students'));
}
}
else{
$session->setFlashdata('message','The id is not available, please try again.');
return redirect()->to(site_url('students'));
}
}
public function updatestudent()
{
$request = ConfigServices::request();
$session = ConfigServices::session();
$name = $request->getPost('std');
$subject = $request->getPost('subject');
$studentId = $request->getPost('id');
$updateStudent = [
's_name'=>$name,
's_subject'=>$subject,
];
//echo $studentId;
//die();
$std = new MyStudents();
$result = $std->update($studentId,$updateStudent);
if ($result) {
$session->setFlashdata('message','You have successfully updated the student.');
}
else{
$session->setFlashdata('message','Oops something went wrong please try again.');
}
return redirect()->to(site_url('students'));
}
public function delete($userId)
{
$session = ConfigServices::session();
if (!empty($userId)) {
$std = new MyStudents();
$result = $std->where('s_id',$userId)->findAll();
if (count($result) > 0) {
//$result = $std->delete($userId);
$result = $std->where('s_id',$userId)->delete();
if ($result){
$session->setFlashdata('message','You have successfully deleted.');
return redirect()->to(site_url('students'));
}
else{
$session->setFlashdata('message','You can't delete the student right now.');
return redirect()->to(site_url('students'));
}
}
else{
$session->setFlashdata('message','The Student is not exist');
return redirect()->to(site_url('students'));
}
}
else{
$session->setFlashdata('message','The id is not available, please try again.');
return redirect()->to(site_url('students'));
}
}
}//class