Its a simple student entry form and listing of the entered students.You can follow the below steps to do this
Step1: create a database named (studentdatabase) in the mysql using phpmyadmin. Below query u can execute through phpmyadmin
1 2 3 4 5 6 7 8 9 10 11 12 |
CREATE TABLE IF NOT EXISTS `tbl_student` ( `id` int(11) NOT NULL, `studentname` varchar(250) NOT NULL, `email` varchar(250) NOT NULL, `phone` int(11) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; ALTER TABLE `tbl_student` ADD PRIMARY KEY (`id`); ALTER TABLE `tbl_student` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; |
Step2: Configuration changes in different pages.
In Application/config/routes.php
$route[‘default_controller’] = “register”;
default_controller will be a “welcome” controller in a fresh codeigniter. here i m changed the default_controller to register.
In Application/config/database.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $db['default']['hostname'] = 'localhost'; $db['default']['username'] = 'root'; $db['default']['password'] = ''; $db['default']['database'] = 'studentdatabase'; $db['default']['dbdriver'] = 'mysql'; $db['default']['dbprefix'] = ''; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE; $db['default']['cache_on'] = FALSE; $db['default']['cachedir'] = ''; $db['default']['char_set'] = 'utf8'; $db['default']['dbcollat'] = 'utf8_general_ci'; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE; ?> |
In Application/config/config.php
changed the base_url as like below. if you are running the application inside htdocs folder of XAMPP or WAMP server,can give any name for the folder. here the name of the folder is ‘school’.
1 |
<?php $config['base_url'] = 'http://localhost/school/'; ?> |
In Application/config/autoload.php
1 2 3 4 |
<?php $autoload['libraries'] = array('form_validation','database','session'); $autoload['helper'] = array('url','form'); $autoload['model'] = array('student_model'); ?> |
above mentioned helpers,model and libraries are used in the application.
Click here to see the detailed code and demo
Step3: Creation of controller. here i created a controller called register.php. path of this file will be inside application/controller/register.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Register extends CI_Controller { public function index() { $this->load->view('register/register_main'); } public function regstudent() { $this->load->view('register/register_student'); } public function liststudent() { $disp_data['student_details']=$this->student_model->student_list(); $this->load->view('register/list_student',$disp_data); } public function student_submit() { $this->form_validation->set_rules('student_name', 'Student name', 'trim|required'); $this->form_validation->set_rules('student_email', 'Student Email', 'trim|required'); $this->form_validation->set_rules('student_phone', 'Student Phone', 'trim|required'); if ($this->form_validation->run() == FALSE) { $this->load->view('register/register_student'); } else { $student_name = $this->input->post('student_name',TRUE); $student_email = $this->input->post('student_email',TRUE); $student_phone = $this->input->post('student_phone',TRUE); $insert_student_array = array( 'studentname' => $student_name, 'email' => $student_email, 'phone' => $student_phone, ); $this->student_model->insert_student($insert_student_array); $this->session->set_flashdata('success_msg', 'success fully entered details'); $this->load->view('register/register_student'); } } } |
Step4:View file creation-created three view files inside views/register folder
Three view files usage and code as below
register_main.php- This is used to display the main page of the application. This is a simple html page layout to display the student add,student list links
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Welcome to CodeIgniter</title> <style type="text/css"> ::selection{ background-color: #E13300; color: white; } ::moz-selection{ background-color: #E13300; color: white; } ::webkit-selection{ background-color: #E13300; color: white; } body { background-color: #fff; margin: 40px; font: 13px/20px normal Helvetica, Arial, sans-serif; color: #4F5155; } a { color: #003399; background-color: transparent; font-weight: normal; } h1 { color: #444; background-color: transparent; border-bottom: 1px solid #D0D0D0; font-size: 19px; font-weight: normal; margin: 0 0 14px 0; padding: 14px 15px 10px 15px; } code { font-family: Consolas, Monaco, Courier New, Courier, monospace; font-size: 12px; background-color: #f9f9f9; border: 1px solid #D0D0D0; color: #002166; display: block; margin: 14px 0 14px 0; padding: 12px 10px 12px 10px; } #body{ margin: 0 15px 0 15px; } p.footer{ text-align: right; font-size: 11px; border-top: 1px solid #D0D0D0; line-height: 32px; padding: 0 10px 0 10px; margin: 20px 0 0 0; } #container{ margin: 10px; border: 1px solid #D0D0D0; -webkit-box-shadow: 0 0 8px #D0D0D0; } </style> </head> <body> <div id="container"> <h1>Welcome to Student Entry and Listing <a href="<?php echo base_url();?>index.php/register/regstudent">Add Student</a> <a href="<?php echo base_url();?>index.php/register/liststudent">List Student</a></h1> <div id="body"> <p> Student Registration and Listing</p> </div> </div> </body> </html> |
register_student.php- This pages shows the student entry form. function name of the controller is specified in the form action page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Welcome to CodeIgniter</title> <style type="text/css"> ::selection{ background-color: #E13300; color: white; } ::moz-selection{ background-color: #E13300; color: white; } ::webkit-selection{ background-color: #E13300; color: white; } body { background-color: #fff; margin: 40px; font: 13px/20px normal Helvetica, Arial, sans-serif; color: #4F5155; } a { color: #003399; background-color: transparent; font-weight: normal; } h1 { color: #444; background-color: transparent; border-bottom: 1px solid #D0D0D0; font-size: 19px; font-weight: normal; margin: 0 0 14px 0; padding: 14px 15px 10px 15px; } code { font-family: Consolas, Monaco, Courier New, Courier, monospace; font-size: 12px; background-color: #f9f9f9; border: 1px solid #D0D0D0; color: #002166; display: block; margin: 14px 0 14px 0; padding: 12px 10px 12px 10px; } #body{ margin: 0 15px 0 15px; } p.footer{ text-align: right; font-size: 11px; border-top: 1px solid #D0D0D0; line-height: 32px; padding: 0 10px 0 10px; margin: 20px 0 0 0; } #container{ margin: 10px; border: 1px solid #D0D0D0; -webkit-box-shadow: 0 0 8px #D0D0D0; } </style> </head> <body> <div id="container"> <h1>Welcome to Student Entry and Listing <a href="<?php echo base_url();?>index.php/register/regstudent">Add Student</a> <a href="<?php echo base_url();?>index.php/register/liststudent">List Student</a></h1> <div id="body"> <p> ADD STUDENT</p> <div style="color:red"><?php echo validation_errors(); ?></div> <p style="color:green"><?php echo $this->session->flashdata('success_msg');?></p> <form action="<?php echo base_url();?>index.php/register/student_submit" method="post"> Student Name :: <input type="text" name="student_name" ><br><br> Student email :: <input type="text" name="student_email"><br><br> Student Phone:: <input type="text" name="student_phone"><br><br> <input type="submit" value="Submit" class="submit"> </form> </div> </div> </body> </html> |
list_student.php- This page is used to display the student details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Welcome to CodeIgniter</title> <style type="text/css"> ::selection{ background-color: #E13300; color: white; } ::moz-selection{ background-color: #E13300; color: white; } ::webkit-selection{ background-color: #E13300; color: white; } body { background-color: #fff; margin: 40px; font: 13px/20px normal Helvetica, Arial, sans-serif; color: #4F5155; } a { color: #003399; background-color: transparent; font-weight: normal; } h1 { color: #444; background-color: transparent; border-bottom: 1px solid #D0D0D0; font-size: 19px; font-weight: normal; margin: 0 0 14px 0; padding: 14px 15px 10px 15px; } code { font-family: Consolas, Monaco, Courier New, Courier, monospace; font-size: 12px; background-color: #f9f9f9; border: 1px solid #D0D0D0; color: #002166; display: block; margin: 14px 0 14px 0; padding: 12px 10px 12px 10px; } #body{ margin: 0 15px 0 15px; } p.footer{ text-align: right; font-size: 11px; border-top: 1px solid #D0D0D0; line-height: 32px; padding: 0 10px 0 10px; margin: 20px 0 0 0; } #container{ margin: 10px; border: 1px solid #D0D0D0; -webkit-box-shadow: 0 0 8px #D0D0D0; } </style> </head> <body> <div id="container"> <h1>Welcome to Student Entry and Listing <a href="<?php echo base_url();?>index.php/register/regstudent">Add Student</a> <a href="<?php echo base_url();?>index.php/register/liststudent">List Student</a></h1> <div id="body"> <p> LIST STUDENT</p> <table border="1" style="width:100%"> <tr> <td>Name</td> <td>Email</td> <td>Phone</td> </tr> <?php foreach($student_details['result'] as $disp_student_details) { ?> <tr> <td><?php echo $disp_student_details['studentname']; ?></td> <td><?php echo $disp_student_details['email']; ?></td> <td><?php echo $disp_student_details['phone'];?></td> </tr> <?php }?> </table> </div> </div> </body> </html> |
Step5: creation of model file in the application/model/student_model.php. This file contains the database query to fetch the student details from tbl_student.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php class Student_model extends CI_Model { function __construct() { // Call the Model constructor parent::__construct(); } function student_list() { $this->db->select('*'); $query_result= $this->db->get('tbl_student'); $data['num_rows']=$query_result->num_rows(); if($query_result->num_rows()>0) { $data['result']=$query_result->result_array(); } return $data; } function insert_student($data) { $this->db->insert('tbl_student', $data); } } |