php

MVC

<?php
/*
 *  The Model is the only class that interacts with the DB
 *  This class deals with all querys with the DB
 *  Extends to the Dbh class as obviously interacts with the DB
 *  class should be protected
 *  Only view and controller classes that extend to it can grab information
 * even when updating/inserting/deleting DB which is handles in the contr we still query in the model because it is only the modal that interacts with the DB
 */
//This is model.class.php
 class Model extends Dbh {

    protected function getUser($firstname) {
        $sql = 'SELECT * FROM users WHERE firstname = ?';
        $stmt = $this->connect()->prepare($sql);
        $stmt->execute([$firstname]);

        $results = $stmt->fetchAll();

        return $results;
    }

    protected function setUser($firstname, $lastname, $email) {
        $sql = 'INSERT INTO users (firstname, lastname, email) Values (?,?,?) ';
        $stmt = $this->connect()->prepare($sql);
        $stmt->execute([$firstname, $lastname, $email]);
    }

    protected function deleteUser($firstname, $lastname) {
        $sql = 'DELETE FROM users WHERE firstname = ? && lastname = ?';
        $stmt = $this->connect()->prepare($sql);
        $stmt->execute([$firstname, $lastname]);
    }

    protected function updateUser($email) {
        $sql = 'UPDATE users SET email = ? WHERE id = 8';
        $stmt = $this->connect()->prepare($sql);
        $stmt->execute([$email]);
    }

 }
?>
 <?php
/*
 * Must connect to DB so inportant to extends DBH class
 * Fetches Info from the Model 
 * 
 */
//This is view.class.php
class View extends Model {

    public function showUser($firstname){
        $results = $this->getUser($firstname);

        echo 'My name is ' . $results[0]['firstname'] . ' ' . $results[0]['lastname'];
        
    }

}
?>
<?php

/*
 * Updating, inserting, creating tables etc into database
 * 
 */
//This is controller.class.php
 class Controller extends Model {

    public function createUser($firstname, $lastname, $email) {
        $this->setUser($firstname, $lastname, $email);
    }

    public function araseUser($firstname, $lastname) {
        $this->deleteUser($firstname, $lastname);
    }

    public function editUser($email) {
        $this->updateUser($email);
    }
 }

?>
 <?php
include 'includes/autoload.inc.php';
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>MVC</title>
    </head>
    <body>
    <?php
    //$testObj = new Test();
    //$testObj->inserUser('John', 'Doe', '[email protected]');
    //$testObj->updateUser('[email protected]');

    //$showUser = new View();
    //$showUser->showUser('John','Lark');

    //$createUser = new Controller();
    //$createUser->createUser('Dean','Lark','[email protected]');

    //$deluser = new Controller();
    //$deluser->araseUser('Dean','Lark');

    //$updateUser = new Controller();
    //$updateUser->editUser('[email protected]');
    ?>
    </body>
</html>

MVC

OOP

Was this helpful?