Skip to content

Instantly share code, notes, and snippets.

@prasanth22
Last active September 5, 2020 07:44
Show Gist options
  • Select an option

  • Save prasanth22/e51d1abbddd45d0af6596717e2504a8c to your computer and use it in GitHub Desktop.

Select an option

Save prasanth22/e51d1abbddd45d0af6596717e2504a8c to your computer and use it in GitHub Desktop.
  • Database
    • Connection.php
    • QueryBuilder.php
  • index.php
  • task.blade.php
Connection.php
<?php
class Connection
{
    public static function make(){
        try{
            return new PDO('mysql:host=localhost;dbname=test', 'root', '');
        }catch( PDOException $e ){
            die("couldn't connect ".$e->getMessage());
        }
    }
}

QueryBuilder.php
<?php
class QueryBuilder
{
    protected $pdo;
    public function __construct($pdo){
        $this->pdo = $pdo;
    }
    public function selectAll($table){
        $statement = $this->pdo->prepare("select * from {$table}");
        $statement->execute();
        return $statement->fetchAll(PDO::FETCH_CLASS);
    }
}

index.php
<?php
require 'Database/Connection.php';
require 'Database/QueryBuilder.php';

$pdo = Connection::make();

$query = new QueryBuilder($pdo);

$tasks = $query->selectAll('todos');

require 'task.blade.php';

?>

task.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <ul>
        <?php foreach ($tasks as $task): ?>
            <?php if ($task->status): ?>
                <strike><li><?= $task->name; ?></li></strike>
            <?php else: ?>
                <li><?= $task->name; ?></li>
            <?php endif; ?>
        <?php endforeach; ?>
    </ul>
</body>
</html>

Removing passwords

  • Database
    • Connection.php
    • QueryBuilder.php
  • index.php
  • Task.php
  • task.blade.php
  • bootstrap.php
  • config.php
index.php
<?php
$query = require 'bootstrap.php';
require 'Task.php';
$tasks = $query->selectAll('todos', 'Task');
require 'task.blade.php';

boostrap.php
<?php
$config = require 'config.php';
// print_r($config['database']);
require 'database/Connection.php';
require 'database/QueryBuilder.php';
return new QueryBuilder(Connection::make($config['database']));

Task.php
<?php 
class Task{
    public $name;
    public $status;
}

Connection.php
<?php
class Connection
{
    public static function make($config){
        try{
           // return new PDO('mysql:host=localhost;dbname=test', 'root', '');
           return new PDO(
            $config['connection'] . ';dbname=' . $config['name'],
            $config['username'],
            $config['password'],
            $config['options']
        );
        }catch( PDOException $e ){
            die("couldn't connect ".$e->getMessage());
        }
    }
}

QueryBuilder.php
<?php
class QueryBuilder
{
    protected $pdo;
    public function __construct($pdo){
        $this->pdo = $pdo;
    }
    public function selectAll($table, $class){
        $statement = $this->pdo->prepare("select * from {$table}");
        $statement->execute();
        return $statement->fetchAll(PDO::FETCH_CLASS,$class);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment