crud with php oop and mvc design pattern

Today, We want to share with you crud with php oop and mvc design pattern.In this post we will show you insert data using class and function in php, hear for add edit delete in php using oops we will give you demo and example for implement.In this post, we will learn about Simple CRUD example of MVC coding in PHP with an example.

crud with php oop and mvc design pattern

There are the Following The simple About CRUD with PHP MVC, PDO, JSON and Twitter Bootstrap Full Information With Example and source code.

As I will cover this Post with live Working example to develop The Model-View-Controller (MVC) Design Pattern for PHP, so the PHP OOP CRUD Tutorial – Step By Step is used for this example is following below.

A CRUD application in PHP with OOP & MVC

How to Create PHP CRUD using OOPS with MySQLi in MVC Example

crud_example.sql

-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Jan 23, 2019 at 06:34 PM
-- Server version: 5.6.37
-- PHP Version: 7.1.19

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `crud_example`
--

-- --------------------------------------------------------

--
-- Table structure for table `tbl_reporting`
--

CREATE TABLE `tbl_reporting` (
  `id` int(11) NOT NULL,
  `patient_id` int(11) NOT NULL,
  `existing` tinyint(4) NOT NULL,
  `invisible` tinyint(4) NOT NULL,
  `reporting_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `tbl_patient`
--

CREATE TABLE `tbl_patient` (
  `id` int(11) NOT NULL,
  `name` varchar(55) NOT NULL,
  `roll_number` int(11) NOT NULL,
  `dob` date NOT NULL,
  `class` varchar(55) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tbl_reporting`
--
ALTER TABLE `tbl_reporting`
  ADD PRIMARY KEY (`id`),
  ADD KEY `patient_id` (`patient_id`);

--
-- Indexes for table `tbl_patient`
--
ALTER TABLE `tbl_patient`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tbl_reporting`
--
ALTER TABLE `tbl_reporting`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=65;

--
-- AUTO_INCREMENT for table `tbl_patient`
--
ALTER TABLE `tbl_patient`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `tbl_reporting`
--
ALTER TABLE `tbl_reporting`
  ADD CONSTRAINT `tbl_reporting_ibfk_1` FOREIGN KEY (`patient_id`) REFERENCES `tbl_patient` (`id`);
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

index.php

deleteReportingByDate($reporting_date);
                foreach($_POST["patient_id"] as $k=> $patient_id) {
                    $existing = 0;
                    $invisible = 0;
                    
                    if($_POST["reporting-$patient_id"] == "existing") {
                        $existing = 1;
                    }
                    else if($_POST["reporting-$patient_id"] == "invisible") {
                        $invisible = 1;
                    }
                    
                    $reporting->addReporting($reporting_date, $patient_id, $existing, $invisible);
                }
            }
            header("Location: index.php?action=reporting");
        }
        $patient = new Patient();
        $patientResult = $patient->getAllPatient();
        require_once "web/reporting-add.php";
        break;
    
    case "reporting-edit":
        $reporting_date = $_GET["date"];
        $reporting = new Reporting();
        if (isset($_POST['add'])) {
            $reporting->deleteReportingByDate($reporting_date);
            if(!empty($_POST["patient_id"])) {
                foreach($_POST["patient_id"] as $k=> $patient_id) {
                    $existing = 0;
                    $invisible = 0;
                    
                    if($_POST["reporting-$patient_id"] == "existing") {
                        $existing = 1;
                    }
                    else if($_POST["reporting-$patient_id"] == "invisible") {
                        $invisible = 1;
                    }
                    
                    $reporting->addReporting($reporting_date, $patient_id, $existing, $invisible);
                }
            }
            header("Location: index.php?action=reporting");
        }
        
        $output = $reporting->getReportingByDate($reporting_date);
        
        $patient = new Patient();
        $patientResult = $patient->getAllPatient();
        require_once "web/reporting-edit.php";
        break;
    
    case "reporting-delete":
        $reporting_date = $_GET["date"];
        $reporting = new Reporting();
        $reporting->deleteReportingByDate($reporting_date);
        
        $output = $reporting->getReporting();
        require_once "web/reporting.php";
        break;
    
    case "reporting":
        $reporting = new Reporting();
        $output = $reporting->getReporting();
        require_once "web/reporting.php";
        break;
    
    case "patient-add":
        if (isset($_POST['add'])) {
            $name = $_POST['name'];
            $roll_number = $_POST['roll_number'];
            $dob = "";
            if ($_POST["dob"]) {
                $dob_timestamp = strtotime($_POST["dob"]);
                $dob = date("Y-m-d", $dob_timestamp);
            }
            $class = $_POST['class'];
            
            $patient = new Patient();
            $insertId = $patient->addPatient($name, $roll_number, $dob, $class);
            if (empty($insertId)) {
                $response = array(
                    "message" => "Problem in Adding New Record",
                    "type" => "error"
                );
            } else {
                header("Location: index.php");
            }
        }
        require_once "web/patient-add.php";
        break;
    
    case "patient-edit":
        $patient_id = $_GET["id"];
        $patient = new Patient();
        
        if (isset($_POST['add'])) {
            $name = $_POST['name'];
            $roll_number = $_POST['roll_number'];
            $dob = "";
            if ($_POST["dob"]) {
                $dob_timestamp = strtotime($_POST["dob"]);
                $dob = date("Y-m-d", $dob_timestamp);
            }
            $class = $_POST['class'];
            
            $patient->editPatient($name, $roll_number, $dob, $class, $patient_id);
            
            header("Location: index.php");
        }
        
        $output = $patient->getPatientById($patient_id);
        require_once "web/patient-edit.php";
        break;
    
    case "patient-delete":
        $patient_id = $_GET["id"];
        $patient = new Patient();
        
        $patient->deletePatient($patient_id);
        
        $output = $patient->getAllPatient();
        require_once "web/patient.php";
        break;
    
    default:
        $patient = new Patient();
        $output = $patient->getAllPatient();
        require_once "web/patient.php";
        break;
}
?>

class/Reporting.php

db_handle = new ConController();
    }
    
    function addReporting($reporting_date, $patient_id, $existing, $invisible) {
        $query = "INSERT INTO tbl_reporting (reporting_date,patient_id,existing,invisible) VALUES (?, ?, ?, ?)";
        $paramType = "siii";
        $paramValue = array(
            $reporting_date,
            $patient_id,
            $existing,
            $invisible
        );
        
        $insertId = $this->db_handle->insert($query, $paramType, $paramValue);
        return $insertId;
    }
    
    function deleteReportingByDate($reporting_date) {
        $query = "DELETE FROM tbl_reporting WHERE reporting_date = ?";
        $paramType = "s";
        $paramValue = array(
            $reporting_date
        );
        $this->db_handle->update($query, $paramType, $paramValue);
    }
    
    function getReportingByDate($reporting_date) {
        $query = "SELECT * FROM tbl_reporting LEFT JOIN tbl_patient ON tbl_reporting.patient_id = tbl_patient.id WHERE reporting_date = ? ORDER By patient_id";
        $paramType = "s";
        $paramValue = array(
            $reporting_date
        );
        
        $output = $this->db_handle->runQuery($query, $paramType, $paramValue);
        return $output;
    }
    
    function getReporting() {
        $sql = "SELECT id, reporting_date, sum(existing) as existing, sum(invisible) as invisible FROM tbl_reporting GROUP By reporting_date";
        $output = $this->db_handle->runBaseQuery($sql);
        return $output;
    }
}
?>

class/ConController.php

conn = $this->connectDB();
    }   
    
    function connectDB() {
        $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
        return $conn;
    }
    
    function runBaseQuery($query) {
        $output = $this->conn->query($query);   
        if ($output->num_rows > 0) {
            while($row = $output->fetch_assoc()) {
                $outputset[] = $row;
            }
        }
        return $outputset;
    }
    
    
    
    function runQuery($query, $param_type, $param_value_array) {
        $sql = $this->conn->prepare($query);
        $this->bindQueryParams($sql, $param_type, $param_value_array);
        $sql->execute();
        $output = $sql->get_output();
        
        if ($output->num_rows > 0) {
            while($row = $output->fetch_assoc()) {
                $outputset[] = $row;
            }
        }
        
        if(!empty($outputset)) {
            return $outputset;
        }
    }
    
    function bindQueryParams($sql, $param_type, $param_value_array) {
        $param_value_reference[] = & $param_type;
        for($i=0; $iconn->prepare($query);
        $this->bindQueryParams($sql, $param_type, $param_value_array);
        $sql->execute();
        $insertId = $sql->insert_id;
        return $insertId;
    }
    
    function update($query, $param_type, $param_value_array) {
        $sql = $this->conn->prepare($query);
        $this->bindQueryParams($sql, $param_type, $param_value_array);
        $sql->execute();
    }
}
?>

class/Patient.php

db_handle = new ConController();
    }
    
    function addPatient($name, $roll_number, $dob, $class) {
        $query = "INSERT INTO tbl_patient (name,roll_number,dob,class) VALUES (?, ?, ?, ?)";
        $paramType = "siss";
        $paramValue = array(
            $name,
            $roll_number,
            $dob,
            $class
        );
        
        $insertId = $this->db_handle->insert($query, $paramType, $paramValue);
        return $insertId;
    }
    
    function editPatient($name, $roll_number, $dob, $class, $patient_id) {
        $query = "UPDATE tbl_patient SET name = ?,roll_number = ?,dob = ?,class = ? WHERE id = ?";
        $paramType = "sissi";
        $paramValue = array(
            $name,
            $roll_number,
            $dob,
            $class,
            $patient_id
        );
        
        $this->db_handle->update($query, $paramType, $paramValue);
    }
    
    function deletePatient($patient_id) {
        $query = "DELETE FROM tbl_patient WHERE id = ?";
        $paramType = "i";
        $paramValue = array(
            $patient_id
        );
        $this->db_handle->update($query, $paramType, $paramValue);
    }
    
    function getPatientById($patient_id) {
        $query = "SELECT * FROM tbl_patient WHERE id = ?";
        $paramType = "i";
        $paramValue = array(
            $patient_id
        );
        
        $output = $this->db_handle->runQuery($query, $paramType, $paramValue);
        return $output;
    }
    
    function getAllPatient() {
        $sql = "SELECT * FROM tbl_patient ORDER BY id";
        $output = $this->db_handle->runBaseQuery($sql);
        return $output;
    }
}
?>

web/reporting.php


    
    
$v) { ?>
Date Present Absent Action
"> crud with php oop and mvc design pattern "> crud with php oop and mvc design pattern

web/reporting-add.php



$v) { ?>
Patient Present Absent
"> " value="existing" checked /> " value="invisible" />

web/reporting-edit.php



$v) { $existingChecked = ""; $invisibleChecked = ""; if($patientResult[$k]["id"] == $output[$k]["patient_id"]) { if($output[$k]["existing"] == 1) { $existingChecked = "checked"; } else if($output[$k]["invisible"] == 1) { $invisibleChecked = "checked"; } } ?>
Patient Present Absent
"> " value="existing" /> " value="invisible" />

web/header.php



How to Create PHP Crud using OOPS and MySQLi



    

How to Create PHP Crud using OOPS and MySQLi

web/patient.php


    
    
$v) { ?>
Patient Name Roll Number Date of Birth Class Action
"> crud with php oop and mvc design pattern "> crud with php oop and mvc design pattern

web/patient-add.php







web/patient-edit.php




">

">

">

">

css/style.css

body {
    width: 610px;
    font-family: Arial;
    color: #212121;
}

#drags-grid {
    margin-bottom: 30px;
}

#drags-grid .txt-heading {
    background-color: #D3F5B8;
}

#drags-grid table {
    width: 100%;
    background-color: #F0F0F0;
}

#drags-grid table td {
    background-color: #FFFFFF;
}

.patientInfoCart {
    padding: 10px;
    border: #E0E0E0 1px solid;
    border-radius: 3px;
    width: 250px;
    margin: 5px 0px 10px 0px;
}

.btnEditAction {
    cursor: pointer;
    text-decoration: none;
    margin-right: 10px;
}

.btnDeleteAction {
    cursor: pointer;
}

#btnAddAction {
    padding: 6px 20px;
    color: #212121;
    text-decoration: none;
    border-radius: 3px;
    cursor: pointer;
    border: #212121 1px solid;
    margin-bottom: 10px;
    display: inline-block;
}

#btnAddAction img {
    margin-right: 5px;
    vertical-align: middle;
}

#patiantFrmAdded {
    border: #E0E0E0 1px solid;
    padding: 10px 20px 20px 20px;
    border-radius: 3px;
}

#patiantFrmAdded div {
    margin-bottom: 15px;
}

#patiantFrmAdded div label {
    margin-left: 5px;
}

.error {
    background-color: #FF6600;
    border: #AA4502 1px solid;
    padding: 5px 10px;
    color: #FFFFFF;
    border-radius: 4px;
}

.info {
    font-size: .8em;
    color: #FF6600;
    letter-spacing: 2px;
    padding-left: 5px;
}

ul.menu-list {
    list-style: none;
    margin: 0px 0px 40px 0px;
    padding: 0px;
    background: #E0E0E0;
    border-radius: 3px;
}

ul.menu-list li{
    display: inline-block;
    padding: 10px 20px;
}

ul.menu-list li a{
    color: #212121;
    text-decoration: none;
}

ul.menu-list li a:hover{
    text-decoration: underline;
}

#btnSubmit {
    padding: 8px;
    background: #09F;
    border: #048eec 1px solid;
    color: #FFF;
    font-size: 0.9em;
    border-radius: 3px;
    width: 150px;
}
.reporting_table td {
    text-align: center;
}
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about The Model-View-Controller (MVC) Design Pattern for PHP.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment