Commit 62d191e3 by oktavi

Project baru

parents
<?php
class HelloController {
public function home() {
$pesan = 'Hello World';
require_once('views/pages/home.php');
}
public function error() {
require_once('views/pages/error.php');
}
}
?>
<?php
require_once ('models/User.php');
class NamaController {
private $user;
public function input($nama) {
$this->user = new User($nama);
require_once('views/pages/nama.php');
}
public function error() {
require_once('views/pages/ErrorInput.php');
}
}
?>
\ No newline at end of file
<?php
if (isset($_GET['controller']) && isset($_GET['action'])) {
$controller = $_GET['controller'];
$action = $_GET['action'];
if (isset($_GET['nama'])) {
$nama = $_GET['nama'];
}
} else {
$controller = 'Hello';
$action = 'home';
}
require_once('views/layout.php');
?>
\ No newline at end of file
<?php
class User {
private $namaPengguna;
public function __construct($namaPengguna) {
$this->namaPengguna = $namaPengguna;
}
public function __toString() {
return $this->namaPengguna;
}
}
<?php
function call($controller, $action) {
require_once('controllers/' . $controller . 'Controller.php');
switch ($controller) {
case 'Hello':
$controller = new HelloController();
$controller->{ $action }();
break;
}
}
function callInput($nama) {
require_once('controllers/NamaController.php');
$controller = new NamaController();
if ($nama == "") {
$controller->error();
} else {
$controller->input($nama);
}
}
$controllers = array('Hello' => ['home', 'error'], 'Nama' => ['input',
'error']);
if (array_key_exists($controller, $controllers)) {
if ($controller == "Nama") {
if (in_array($action, $controllers[$controller])) {
callInput($nama);
} else {
call('Hello', 'error');
}
} else {
if (in_array($action, $controllers[$controller])) {
call($controller, $action);
} else {
call('Hello', 'error');
}
}
} else {
call('Hello', 'error');
}
?>
\ No newline at end of file
<DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<a href='/hello'>Beranda</a>
</header>
<?php require_once('routes.php'); ?>
<body>
<html>
\ No newline at end of file
<p>Terdapat Error.. Nama Tidak Boleh Kosong</p>
\ No newline at end of file
<p>Terdapat Error.. </p>
\ No newline at end of file
<?php echo $pesan; ?>!<p>
<form action="" method="get">
<label>Nama :</label>
<input type="text" name="nama" placeholder="Siapa nama Kamu ?">
<input type="submit" value="Ok">
<input type="hidden" name="controller" value="Nama">
<input type="hidden" name="action" value="input">
</form>
\ No newline at end of file
<p>Hello <?php echo $this->user ?> </p>
\ No newline at end of file
<?php
class DeleteController
{
public function delete()
{
require_once('views/pages/delete.php');
}
public function error()
{
require_once('views/pages/error.php');
}
}
?>
\ No newline at end of file
<?php
class HomeController
{
public function home()
{
require_once('views/pages/home.php');
}
public function error()
{
require_once('views/pages/error.php');
}
}
?>
\ No newline at end of file
<?php
class InsertController
{
public function insert()
{
require_once('views/pages/insert.php');
}
public function error()
{
require_once('views/pages/error.php');
}
}
?>
\ No newline at end of file
<?php
class SearchController
{
public function search()
{
require_once('views/pages/search.php');
}
public function error()
{
require_once('views/pages/error.php');
}
}
?>
\ No newline at end of file
<?php
class UpdateController
{
public function update()
{
require_once('views/pages/update.php');
}
public function error()
{
require_once('views/pages/error.php');
}
}
?>
\ No newline at end of file
<?php
class ClientDao
{
private $connection;
private $db;
public function __construct(PDO $connection = null)
{
$this->connection = $connection;
if ($this->connection === null)
{
$this->connection = new PDO
(
'mysql:host=localhost;dbname=mvc',
'root',
''
);
$this->connection->setAttribute(
PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION
);
}
}
public function search($name)
{
$clients = $this->connection->prepare("SELECT * FROM identity WHERE fullnames LIKE '%".$name."%'");
$clients->execute();
$clients->setFetchMode(PDO::FETCH_CLASS, 'Client');
return $clients->fetchAll();
}
public function findById($id)
{
$find = $this->connection->prepare("SELECT * FROM identity WHERE id=:id");
$find->bindParam(":id", $_GET["id"]);
$find->execute();
$find->setFetchMode(PDO::FETCH_CLASS, 'Client');
return $find->fetch();
}
public function findAll()
{
$stmt = $this->connection->prepare('SELECT * FROM identity');
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_CLASS, 'Client');
return $stmt->fetchAll();
}
public function delete($id){
try{
$stmt = $this->connection->prepare("DELETE FROM `identity` WHERE id=:id");
$stmt->bindParam(":id", $_GET["id"]);
$stmt->execute();
// Alihkan ke index.php
header("location: index.php");
}catch(PDOException $e){
echo $e->getMessage();
return false;
}
}
public function update($image,$fullnames,$sex,$idcard,$birthdate,$province,$district,$sector){
try{
// Prepared statement untuk mengubah data
$query = $this->connection->prepare("UPDATE `identity` SET `image`=:image,`fullnames`=:fullnames,`sex`=:sex,`idcard`=:idcard,`birthdate`=:birthdate ,`province`=:province ,`district`=:district ,`sector`=:sector WHERE id=:id");
$query->bindParam(":image", $image);
$query->bindParam(":fullnames", $fullnames);
$query->bindParam(":sex", $sex);
$query->bindParam(":idcard", $idcard);
$query->bindParam(":birthdate", $birthdate);
$query->bindParam(":province", $province);
$query->bindParam(":district", $district);
$query->bindParam(":sector", $sector);
$query->bindParam(":id", $_GET['id']);
// Jalankan perintah SQL
$query->execute();
// Alihkan ke index.php
header("location: index.php");
}catch(PDOException $e){
echo $e->getMessage();
return false;
}
}
public function insert($id,$image,$fullnames,$sex,$idcard,$birthdate,$province,$district,$sector)
{
try{
$query = $this->connection->prepare("INSERT INTO identity(id,image,fullnames,sex,idcard,birthdate,province,district,sector) VALUES (:id,:image,:fullnames,:sex,:idcard,:birthdate,:province,:district,:sector)");
$query->bindParam(":id", $id);
$query->bindParam(":image", $image);
$query->bindParam(":fullnames", $fullnames);
$query->bindParam(":sex", $sex);
$query->bindParam(":idcard", $idcard);
$query->bindParam(":birthdate", $birthdate);
$query->bindParam(":province", $province);
$query->bindParam(":district", $district);
$query->bindParam(":sector", $sector);
// Jalankan perintah SQL
$query->execute();
// Alihkan ke index.php
header("location: index.php");
}catch(PDOException $e){
// Jika terjadi error
if($e->errorInfo[0] == 23000){
//errorInfor[0] berisi informasi error tentang query sql yg baru dijalankan
//23000 adalah kode error ketika ada data yg sama pada kolom yg di set unique
$this->error = "id sudah digunakan!";
return false;
}else{
echo $e->getMessage();
return false;
}
}
}
}
?>
<?php
$host = "localhost";
$dbname = "mvc";
$username = "root";
$password = "";
try {
// Buat Object PDO baru dan simpan ke variable $db
$db = new PDO("mysql:host={$host};dbname={$dbname}", $username, $password);
// Mengatur Error Mode di PDO untuk segera menampilkan exception ketika ada kesalahan
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $exception){
die("Connection error: " . $exception->getMessage());
}
?>
<?php
include ('dao\ClientDao.php');
include ('models\Client.php');
if (isset($_GET['controller']) && isset($_GET['action']))
{
$controller = $_GET['controller'];
$action = $_GET['action'];
}
else
{
$controller = 'Home';
$action = 'home';
}
require_once('views/layout.php');
?>
\ No newline at end of file
<?php
class Client
{
private $id;
private $image;
private $fullnames;
private $sex;
private $idcard;
private $birthdate;
private $province;
private $district;
private $sector;
public function __construct($data = null)
{
if (is_array($data)) {
if (isset($data['id'])) {
$this->id = $data['id'];
$this->image = $data['image'];
$this->fullnames = $data['fullnames'];
$this->sex = $data['sex'];
$this->idcard = $data['idcard'];
$this->birthdate = $data['birthdate'];
$this->province = $data['province'];
$this->district = $data['district'];
$this->sector = $data['sector'];
}
}
}
public function getId()
{
return $this->id;
}
public function getImage()
{
return $this->image;
}
public function getFullnames()
{
return $this->fullnames;
}
public function getSex()
{
return $this->sex;
}
public function getIdcard()
{
return $this->idcard;
}
public function getBirthdate()
{
return $this->birthdate;
}
public function getProvince()
{
return $this->province;
}
public function getDistrict()
{
return $this->district;
}
public function getSector()
{
return $this->sector;
}
public function setId($id)
{
$this->id=$id;
}
}
?>
\ No newline at end of file
/*
SQLyog Community v12.2.1 (32 bit)
MySQL - 10.1.16-MariaDB : Database - mvc
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`mvc` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `mvc`;
/*Table structure for table `identity` */
DROP TABLE IF EXISTS `identity`;
CREATE TABLE `identity` (
`id` int(50) NOT NULL AUTO_INCREMENT,
`image` varchar(50) DEFAULT NULL,
`fullnames` varchar(50) DEFAULT NULL,
`sex` varchar(50) DEFAULT NULL,
`idcard` varchar(50) DEFAULT NULL,
`birthdate` varchar(50) DEFAULT NULL,
`province` varchar(50) DEFAULT NULL,
`district` varchar(50) DEFAULT NULL,
`sector` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
/*Data for the table `identity` */
insert into `identity`(`id`,`image`,`fullnames`,`sex`,`idcard`,`birthdate`,`province`,`district`,`sector`) values
(11,'bike.png','oyes','qw','mm','mmm','kk','kkk','kk'),
(12,'bike.png','paldo','11','zx','zx','zc','zx','zxc');
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
<?php
function call($controller, $action)
{
require_once('controllers/' . $controller . 'Controller.php');
switch($controller)
{
case 'Update':
$controller = new UpdateController();
$controller->{ $action }();
break;
case 'Insert':
$controller = new InsertController();
$controller->{ $action }();
break;
case 'Delete':
$controller = new DeleteController();
$controller->{ $action }();
break;
case 'Home':
$controller = new HomeController();
$controller->{ $action }();
break;
case 'Search':
$controller = new SearchController();
$controller->{ $action }();
break;
}
}
$controllers = array('Insert' => ['insert', 'error'],'Update' => ['update' , 'error'], 'Home' => ['home', 'error'],'Delete' => ['delete', 'error'],'Search' => ['search' , 'error']);
if (array_key_exists($controller, $controllers))
{
if (in_array($action, $controllers[$controller]))
{
call($controller,$action);
}
else
{
call('Home', 'error');
}
}
else
{
call('Home', 'error');
}
?>
\ No newline at end of file
<DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
</header>
<?php
require_once('routes.php');
?>
</body>
</html>
\ No newline at end of file
<?php
$client = new ClientDao();
if(isset($_GET["id"])){
if($client->delete($_GET["id"])){
$success = true;
}else{
// Jika gagal, ambil pesan error
$error = $client->getLastError();
}
}
?>
<p>Terdapat Error.. </p>
\ No newline at end of file
<?php
$user = new ClientDao();
$data = $user->findAll();
?>
<form method="get">
<input required type="text" name="name" placeholder="Search By Name"/>
<input required type="hidden" name="controller" value="Search"/>
<input required type="hidden" name="action" value="search"/>
<button type="submit"> Search</button>
<a href="index.php?controller=Insert&action=insert"><label type="button"> Add Client</label></a>
</form>
<table class=tabel border="1">
<tr>
<th> ID </th>
<th> Image </th>
<th> Full Names </th>
<th> Sex </th>
<th> Identity Card </th>
<th> Birth Date </th>
<th> Province </th>
<th> District </th>
<th> Sector </th>
<th> Option </th>
</tr>
<?php foreach ($data as $value): ?>
<tr>
<td><?php echo $value->getId() ?></td>
<td>
<img src="images\<?php echo $value->getImage() ?>" style="width:50px;heigh:50px;">
</td>
<td><?php echo $value->getFullnames() ?></td>
<td><?php echo $value->getSex() ?></td>
<td><?php echo $value->getIdcard() ?></td>
<td><?php echo $value->getBirthdate() ?></td>
<td><?php echo $value->getProvince() ?></td>
<td><?php echo $value->getDistrict() ?></td>
<td><?php echo $value->getSector() ?></td>
<td><a href="index.php?controller=Update&action=update&id=<?php echo $value->getId()?>">Update </a><a href="index.php?controller=Delete&action=delete&id=<?php echo $value->getId()?>"> Delete</a></td>
</tr>
<?php endforeach; ?>
</table>
<?php
$client = new ClientDao();
if(isset($_POST['submit'])){
// Simpan data yang di inputkan ke POST ke masing-masing variable
// dan convert semua tag HTML yang mungkin dimasukkan untuk mengindari XSS
$id = htmlentities($_POST['id']);
$image = htmlentities($_POST['image']);
$fullnames = htmlentities($_POST['fullnames']);
$sex = htmlentities($_POST['sex']);
$idcard = htmlentities($_POST['idcard']);
$birthdate = htmlentities($_POST['birthdate']);
$province = htmlentities($_POST['province']);
$district = htmlentities($_POST['district']);
$sector = htmlentities($_POST['sector']);
if($client->insert($id,$image,$fullnames,$sex,$idcard,$birthdate,$province,$district,$sector)){
$success = true;
}else{
// Jika gagal, ambil pesan error
$error = $client->getLastError();
}
}
?>
<form method="post">
<table class=tabel border="0">
<tr>
<td>Id</td><td><input required type="text" name="id" placeholder="ID" /></td>
</tr>
<tr>
<td>Image</td><td><input required type="file" name="image" placeholder="Image" /></td>
</tr>
<tr>
<td>Full Names</td><td><input required type="text" name="fullnames" placeholder="Full Names" /></td>
</tr>
<tr>
<td>Sex</td><td><input required type="text" name="sex" placeholder="Sex" /></td>
</tr>
<tr>
<td>ID Card</td><td><input required type="text" name="idcard" placeholder="ID Card" /></td>
</tr>
<tr>
<td>Birthdate</td><td><input required type="text" name="birthdate" placeholder="Birthdate" /></td>
</tr>
<tr>
<td>Province</td><td><input required type="text" name="province" placeholder="Province" /></td>
</tr>
<tr>
<td>District</td><td><input required type="text" name="district" placeholder="District" /></td>
</tr>
<tr>
<td>Sector</td><td><input required type="text" name="sector" placeholder="Sector" /></td>
</tr>
</table>
<button type="submit" name="submit" /> insert </button>
</form>
\ No newline at end of file
<p>Hello <?php echo $this->user ?></p>
\ No newline at end of file
<?php
include ('database\database.php');
$user = new ClientDao();
$data = $user->search($_GET['name']);
?>
<form method="get">
<input required type="text" name="name" placeholder="Search By Name"/>
<input required type="hidden" name="controller" value="Search"/>
<input required type="hidden" name="action" value="search"/>
<button type="submit"> Search</button>
<a href="index.php?controller=Insert&action=insert"><label type="button"> Add Client</label></a>
</form>
<table class=tabel border="1">
<tr>
<th> ID </th>
<th> Image </th>
<th> Full Names </th>
<th> Sex </th>
<th> Identity Card </th>
<th> Birth Date </th>
<th> Province </th>
<th> District </th>
<th> Sector </th>
<th> Option </th>
</tr>
<?php foreach ($data as $value): ?>
<tr>
<td><?php echo $value->getId() ?></td>
<td>
<img src="images\<?php echo $value->getImage() ?>" style="width:50px;heigh:50px;">
</td>
<td><?php echo $value->getFullnames() ?></td>
<td><?php echo $value->getSex() ?></td>
<td><?php echo $value->getIdcard() ?></td>
<td><?php echo $value->getBirthdate() ?></td>
<td><?php echo $value->getProvince() ?></td>
<td><?php echo $value->getDistrict() ?></td>
<td><?php echo $value->getSector() ?></td>
<td><a href="index.php?controller=Update&action=update&id=<?php echo $value->getId() ?>">Update </a><a href="index.php?controller=Delete&action=delete&id=<?php echo $value->getId() ?>"> Delete</a></td>
</tr>
<?php endforeach; ?>
</table>
\ No newline at end of file
<?php
$client = new ClientDao();
$data = $client->findById($_GET['id']);
if(isset($_POST['submit'])){
$id = htmlentities($_POST['id']);
$image = htmlentities($_POST['image']);
$fullnames = htmlentities($_POST['fullnames']);
$sex = htmlentities($_POST['sex']);
$idcard = htmlentities($_POST['idcard']);
$birthdate = htmlentities($_POST['birthdate']);
$province = htmlentities($_POST['province']);
$district = htmlentities($_POST['district']);
$sector = htmlentities($_POST['sector']);
if($client->update($image,$fullnames,$sex,$idcard,$birthdate,$province,$district,$sector))
{
$success = true;
}else{
$error = $client->getLastError();
}
}
?>
<form method="post">
<table class=tabel border="0">
<tr><td>Id</td><td><input required type="read only" name="id" placeholder="ID" value="<?php echo $data->getId() ?>"/></td><tr>
<td>Image</td><td><input required type="file" name="image" placeholder="Image" value="<?php echo $data->getImage() ?>"/></td></tr><tr>
<td>Full Names</td><td><input required type="text" name="fullnames" placeholder="Full Names" value="<?php echo $data->getFullnames() ?>"/></td></tr>
<tr><td>Sex</td><td><input required type="text" name="sex" placeholder="Sex" value="<?php echo $data->getSex() ?>"/></td></tr>
<tr><td>ID Card</td><td><input required type="text" name="idcard" placeholder="ID Card" value="<?php echo $data->getIdCard() ?>"/></td></tr>
<tr><td>Birthdate</td><td><input required type="text" name="birthdate" placeholder="Birthdate" value="<?php echo $data->getBirthdate() ?>"/></td></tr>
<tr><td>Province</td><td><input required type="text" name="province" placeholder="Province" value="<?php echo $data->getProvince() ?>"/></td></tr>
<tr><td>District</td><td><input required type="text" name="district" placeholder="District" value="<?php echo $data->getDistrict() ?>"/></td></tr>
<tr><td>Sector</td><td><input required type="text" name="sector" placeholder="Sector" value="<?php echo $data->getSector() ?>"/></td></tr>
</table>
<button type="submit" name="submit" > Update </button>
</form>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment