Record Insert and Display Using PHP CodeIgniter

Record Insert and Display Using PHP CodeIgniter

Right now, will embed and show record utilizing CodeIgniter. So as to do this we will follow MVC (Model, View, and Controller). MVC disentangles information stream and it’s straightforward.

We should do this by taking an example of Employee Management,

Stage 1: First you need to make another mapping in your database as “EmployeeManager“. Right now a table name Employee and include following fields in it.

Record Insert and Display Using PHP CodeIgniter

You have your table prepared here.

Stage 2: As you have your database prepared presently it’s an ideal opportunity to make your CURD. For this first, we should make our structure. Make a record named “EmployeeView.php” in sees organizer of your PHP venture envelope and include the following code in it.

<html>
	<form action='/EmployeeManager/index.php/EmployeeController/add_employee'>
		<center>
			<table>
				<caption><b><i><font color='blue' size='5'>Employee Interface</i></b></font></caption>
				<tr><td><b><i>Employee Name</i></b></td><td><input type='text' name='empname'></td></tr>
				<tr><td><b><i>DOB</i></b></td><td><input type='text' name='dob'></td></tr>
				<tr><td><b><i>Salary</i></b></td><td><input type='text' name='salary'></td></tr>
				<tr><td><b><i>Department</i></b></td><td><input type='text' name='deptname'></td></tr>
				<tr><td><b><i>Position</i></b></td><td><input type='text' name='position'></td></tr>
				<tr><td><input type='submit'></td><td><input type='reset'></td></tr>
			</table>
		</center>
	</form>
	<?php echo $message; ?> // to print message
</html>

This is HTML, structure which will be appeared to clients to enter subtleties of Employee. We can’t test this code yet as we have not our controller. Hence, Now we will make or controller.

Stage 3: Make a document named “EmployeeController.php” in controller organizer of PHP CodeIgniter undertaking and add this code to it.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class EmployeeController extends CI_Controller{
	function __construct()
	{
		parent::__construct();
	}
	public function index()
	{
		$data['message']="";
		$this->load->view('EmployeeView',$data);
	}
}
?>

Presently, show your code to utilizing URL: http://localhost/EmployeeManager/index.php/EmployeeController/

Worker director is your Project name. Here, we have not determined any function of EmployeeController, so as a matter of course file function will be called where we have stacked EmployeeView. We have likewise sent a vacant message we will talk about it later.

Your structure should resemble this,

Record Insert and Display Using PHP CodeIgniter

It’s a great opportunity to embed record in your SQL table “representative“.

Stage 4: First you have to set your database name in your task. To do this open applications → config → database.php

    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '123',
    'database' => 'EmployeeManager',
    'dbdriver' => 'mysqli',

Stage 5: Now make your Model, make a document named “EmployeeModel.php“. Here, we will make a function to embed information in MySQL.

<?php
	class EmployeeModel extends CI_Model{
		function __construct()
		{
			parent::__construct();
			$this->load->database();//loading database
		}
		function add_record($values)//adding values into employee table
		{
			$result=$this->db->insert('employee',$values);
			return($result);
		}
		function display_all()//function to display all records
		{
			$query=$this->db->get('employee');
			return($query->result());
		}
	}
?>

These functions will be called by Controller so we will make functions in Controller to call model functions.

Stage 6: Making include and show function in Controller. Your total Controller will resemble this,

<?php
	defined('BASEPATH') OR exit('No direct script access allowed');

	class EmployeeController extends CI_Controller{
		function __construct()
		{
			parent::__construct();
			$this->load->model('EmployeeModel');// to load the model
		}
		
		public function index()
		{
			$data['message']="";
			$this->load->view('EmployeeView',$data);
		}

		public function add_employee()
		//function to take values from view and sending to model
		{ 
			$values['name']=$_GET['empname'];     
			$values['dob']=$_GET['dob'];
			$values['salary']=$_GET['salary'];
			$values['department']=$_GET['deptname'];
			$values['position']=$_GET['position'];
			$result=$this->EmployeeModel->add_record($values);
			//calling model function add_record 
			if($result)
			{
				$data['message']="Employee Added";//message to be send to view
			}
			else
			{
				$data['message']="failed to add Employee...don't worry,try again!:)";
			}
			$this->load->view('EmployeeView',$data);
		}

		public function display_all()
		//method to display all records in database 
		{
			$result=$this->EmployeeModel->display_all();
			$data['result']=$result;
			$this->load->view('EmployeeDisplayAll',$data);
		}
	}
?>

Stage 7: To make show all view page, make a document named “EmployeeDisplayAll.php” in Views.

<html>
	<center><b><font size='5' color='blue'> List of All Employees</font></b>
		<table border=1>
			<tr>
			<th>Employee ID</th>
			<th>Employee Name</th>
			<th>DOB</th>
			<th>Salary</th>
			<th>Department</th>
			<th>Position</th>
			</tr>
			<?php
				foreach($result as $rec)
				{
					echo "<tr>
						<td>$rec->id</td><
						td>$rec->name</td>
						<td>$rec->dob</td>
						<td>$rec->salary</td>
						<td>$rec->department</td>
						<td>$rec->position</td>
					</tr>";
				}
			?>
		</table>
	</center>
</html>

This will show the information of all workers as follows.

Record Insert and Display Using PHP CodeIgniter

Recent Posts

error: Alert: Content is protected!!