How to Create a Codeigniter CRUD Application

Codeigniter CRUD Application

In this article, we’ll learn about building the Codeigniter CRUD Application.

CodeIgniter, an exceptional PHP framework renowned for its speed and simplicity, empowers developers to create remarkable web applications.

We will embark on a journey to build a fully functional Codeigniter CRUD (Create, Read, Update, Delete) application.

By the end of this guide, you will possess the expertise to harness CodeIgniter’s capabilities, enabling you to construct a dynamic web application that seamlessly performs Codeigniter CRUD operations on a database.

Codeigniter CRUD Sample Application

We’ll learn Codeigniter CRUD by Creating Classroom Management System Using Codeignitor, Here is Code For Develop App:

Firstly Create Table Named student Inside Codeignitor Database


CREATE TABLE IF NOT EXISTS `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`s_name` varchar(64) DEFAULT NULL,
`p_name` varchar(64) DEFAULT NULL,
`address` varchar(128) DEFAULT NULL,
`city` varchar(32) DEFAULT NULL,
`state` char(2) DEFAULT NULL,
`zip` char(10) DEFAULT NULL,
`phone` char(20) DEFAULT NULL,
`email` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


Now First Create Student Controller Named student.php Inside application/controllers/


<?php class Student extends CI_Controller { function Student() { parent::__construct(); } function index() { // display information for the view $data['title'] = "Classroom: Home Page"; $data['headline'] = "Welcome to the Classroom Management System"; $data['include'] = 'student_index'; $this->load->view('template', $data); } function add() { $this->load->helper('form'); // display information for the view $data['title'] = "Classroom: Add Student"; $data['headline'] = "Add a New Student"; $data['include'] = 'student_add'; $this->load->view('template', $data); } function create() { $this->load->helper('url'); $this->load->model('MStudent','',TRUE); $this->MStudent->addStudent($_POST); redirect('student/add','refresh'); } function listing() { $this->load->library('table'); $this->load->model('MStudent','',TRUE); $students_qry = $this->MStudent->listStudents(); // generate HTML table from query results $tmpl = array ( 'table_open' => '<table border="0" cellpadding="3" cellspacing="0">', 'heading_row_start' => '<tr bgcolor="#66cc44">', 'row_start' => '<tr bgcolor="#dddddd">' ); $this->table->set_template($tmpl); $this->table->set_empty("&nbsp;"); $this->table->set_heading('', 'Child Name', 'Parent Name', 'Address', 'City', 'State', 'Zip', 'Phone', 'Email'); $table_row = array(); foreach ($students_qry->result() as $student) { $table_row = NULL; $table_row[] = '<nobr>' . anchor('student/edit/' . $student->id, 'edit') . ' | ' . anchor('student/delete/' . $student->id, 'delete', "onClick=\" return confirm('Are you sure you want to ' + 'delete the record for $student->s_name?')\"") . '</nobr>'; // replaced above :: $table_row[] = anchor('student/edit/' . $student->id, 'edit'); $table_row[] = $student->s_name; $table_row[] = $student->p_name; $table_row[] = $student->address; $table_row[] = $student->city; $table_row[] = $student->state; $table_row[] = $student->zip; $table_row[] = $student->phone; $table_row[] = mailto($student->email); $this->table->add_row($table_row); } $students_table = $this->table->generate(); // generate HTML table from query results // replaced above :: $students_table = $this->table->generate($students_qry); // display information for the view $data['title'] = "Classroom: Student Listing"; $data['headline'] = "Student Listing"; $data['include'] = 'student_listing'; $data['data_table'] = $students_table; $this->load->view('template', $data); } function edit() { $this->load->helper('form'); $id = $this->uri->segment(3); $this->load->model('MStudent','',TRUE); $data['row'] = $this->MStudent->getStudent($id)->result(); // display information for the view $data['title'] = "Classroom: Edit Student"; $data['headline'] = "Edit Student Information"; $data['include'] = 'student_edit'; $this->load->view('template', $data); } function update() { $this->load->model('MStudent','',TRUE); $this->MStudent->updateStudent($_POST['id'], $_POST); redirect('student/listing','refresh'); } function delete() { $id = $this->uri->segment(3); $this->load->model('MStudent','',TRUE); $this->MStudent->deleteStudent($id); redirect('student/listing','refresh'); } } /* End of file student.php */ /* Location: ./system/application/controllers/student.php */

Now Create Views To Display Pages. There Are 5 Files Inside Views: student_add.php, student_listing.php, template.php, student_edit.php, student_index.php
First Create Template File Named template.php inside application/views Folder

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <title><?php echo $title;?></title> </head> <body> <div class="navigation"> <?php // nav bar echo anchor('student/index', 'Home'); echo (' | '); echo anchor('student/add', 'Add a New Student'); echo (' | '); echo anchor('student/listing', 'List All Students'); ?> </div> <h1><?php echo $headline;?></h1> <?php $this->load->view($include);?> </body> </html>  Then Create student_index.php inside application/views Folder

 

<p>Congratulations. Your initial setup is complete!</p>

Then Create student_add.php inside application/views Folder

<table>
<?php

echo form_open('student/create');

// an array of the fields in the student table
$field_array = array('s_name','p_name','address','city','state','zip','phone','email');
foreach($field_array as $field)
{
echo '<tr><td>' . $field . '</td>';
echo '<td>' . form_input(array('name' => $field)) . '</td></tr>';
}

// not setting the value attribute omits the submit from the $_POST array
echo '<tr><td>&nbsp;</td><td>' . form_submit('', 'Add') . '</td></tr>';

echo form_close();

?>
</table>
Then Create Student Listing Page Named student_listing.php inside application/views Folder


<?php echo $data_table; ?>

Now Create Student Edit Page Named student_edit.php inside application/views Folder


<?php echo form_open('student/update'); echo form_hidden('id', $row[0]->id); // an array of the fields in the student table $field_array = array('s_name','p_name','address','city','state','zip','phone','email'); foreach($field_array as $field_name) { echo '<p>' . $field_name; echo form_input($field_name, $row[0]->$field_name) . '</p>'; } // not setting the value attribute omits the submit from the $_POST array echo form_submit('', 'Update'); echo form_close(); ?>

Congrats, Now Your View Part Is Done

 

Now Create Model Named mstudent.php For Database Interaction Insider /application/models Folder


<?php class MStudent extends CI_Model{ // Create student record in database function addStudent($data) { $this->db->insert('student', $data); } function listStudents() { return $this->db->get('student'); } // Retrieve one student record function getStudent($id) { return $this->db->get_where('student', array('id'=> $id)); } // Update one student record function updateStudent($id, $data) { $this->db->where('id', $id); $this->db->update('student', $data); } // Delete one student record function deleteStudent($id) { $this->db->where('id', $id); $this->db->delete('student'); } } /* End of file mstudent.php */ /* Location: ./system/application/models/mstudent.php */

Now Your Codeigniter CRUD Application Is Ready 🙂

Congratulations! You’ve embarked on an extraordinary journey, constructing a seamless Codeigniter CRUD application using the remarkable CodeIgniter framework.

Armed with this newfound knowledge, you possess the ability to build your own innovative web applications that surpass expectations.

Embrace CodeIgniter’s power, let your creativity flow, and craft remarkable experiences for users. Happy coding and creating!

I hope this article helps you to build Codeigniter CRUD application

One thought on “How to Create a Codeigniter CRUD Application

Write a Reply or Comment

Your email address will not be published. Required fields are marked *