Skip to content

Commit c9997a2

Browse files
committed
initial commit
0 parents  commit c9997a2

File tree

11 files changed

+216
-0
lines changed

11 files changed

+216
-0
lines changed

database/config.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
$host = 'localhost';
4+
$port = '5432';
5+
$username = 'postgres';
6+
$password = 'tes123';
7+
$dbname = 'pweb';
8+
$connection_string = "host={$host} port={$port} dbname={$dbname} user={$username} password={$password}";
9+
10+
$conn = pg_connect($connection_string);
11+
12+
if (!$conn) {
13+
echo "<marquee>Not connected to db</marquee> \n";
14+
}

database/db.sql

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- dbname = pweb
2+
-- table = tugas6pweb
3+
4+
CREATE TABLE tugas6pweb(
5+
nama varchar(255) NOT NULL,
6+
nrp varchar(20) PRIMARY KEY,
7+
departemen varchar(255) NOT NULL,
8+
asal varchar(255) NOT NULL,
9+
mining_at date NOT NULL
10+
);

form.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
require 'includes/header.php';
3+
?>
4+
5+
<div class="container my-4">
6+
<div class="row">
7+
<div class="col col-4 mx-auto">
8+
<div class="card shadow">
9+
<div class="card-header">
10+
<h4>Form Pendaftaran</h4>
11+
</div>
12+
<div class="card-body">
13+
<form action="process-form.php" method="post">
14+
<div class="form-group">
15+
<label for="">Nama</label>
16+
<input type="text" name="nama" class="form-control" placeholder="Masukkan Nama Lengkap...">
17+
</div>
18+
<div class="form-group">
19+
<label for="">NRP</label>
20+
<input type="text" name="nrp" class="form-control" placeholder="Masukkan NRP...">
21+
</div>
22+
<div class="form-group">
23+
<label for="">Departemen</label>
24+
<input type="text" name="departemen" class="form-control" placeholder="Masukkan Asal departemen...">
25+
</div>
26+
<div class="form-group">
27+
<label for="">Asal</label>
28+
<input type="text" name="asal" class="form-control" placeholder="Masukkan Asal daerah...">
29+
</div>
30+
<div class="form-group">
31+
<label for="">Tanggal</label>
32+
<input type="date" name="tanggal" class="form-control">
33+
</div>
34+
</div>
35+
<div class="card-footer">
36+
<div class="d-flex justify-content-around">
37+
<a href="index.php" class="btn btn-danger btn-sm">Batal</a>
38+
<button type="submit" name="daftar" class="btn btn-primary btn-sm">Daftar</button>
39+
</form>
40+
</div>
41+
</div>
42+
</div>
43+
</div>
44+
</div>
45+
</div>

functions/functions.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
function All($tbname) {
3+
global $conn;
4+
$qry = "SELECT * FROM " . $tbname;
5+
$res = pg_query($conn,$qry);
6+
7+
if(!$res) {
8+
echo "Error when mining data";
9+
exit;
10+
}
11+
12+
while($row = pg_fetch_assoc($res)) {
13+
$results[] = $row;
14+
}
15+
return $results;
16+
}
17+
18+
function byNRP($id) {
19+
global $conn;
20+
$qry = "SELECT * FROM tugas6pweb WHERE nrp = " . $id;
21+
$res = pg_query($conn,$qry);
22+
23+
if(!$res) {
24+
echo "Error when mining data";
25+
exit;
26+
}
27+
28+
while($row = pg_fetch_assoc($res)) {
29+
$results[] = $row;
30+
}
31+
return $results[0];
32+
}
33+
34+
function insert($data) {
35+
global $conn;
36+
$dname = $data['nama'];
37+
$dnrp = $data['nrp'];
38+
$ddpt = $data['departemen'];
39+
$dasal = $data['asal'];
40+
$dmining = $data['tanggal'];
41+
$qry = "INSERT INTO tugas6pweb VALUES('$dname','$dnrp','$ddpt','$dasal','$dmining')";
42+
$res = pg_query($conn,$qry);
43+
return pg_affected_rows($res);
44+
}
45+
46+
function delete($id){
47+
global $conn;
48+
$qry = "DELETE FROM tugas6pweb WHERE nrp =" . $id;
49+
$res = pg_query($conn,$qry);
50+
return pg_affected_rows($res);
51+
}

icon/pencil.svg

+1
Loading

icon/trash.svg

+1
Loading

includes/header.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
require 'init.php';
3+
?>
4+
5+
<!DOCTYPE html>
6+
<html lang="en">
7+
<head>
8+
<meta charset="UTF-8">
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
10+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
11+
<link rel="stylesheet" type="text/css" href="styles/style.css">
12+
<title>PostgreSQL CRUD</title>
13+
</head>

index.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
require 'includes/header.php';
3+
4+
$getUrl = $_SERVER['REQUEST_URI'];
5+
$uri = explode('/', $getUrl);
6+
if($uri[2] == '' || $uri[2] == 'index.php') {
7+
$minings = All('tugas6pweb');
8+
}
9+
?>
10+
11+
<div class="container my-4">
12+
<h3>PostgreSQL | CRUD</h3>
13+
<div class="row custom d-none">
14+
<div class="col"></div>
15+
</div>
16+
<div class="row">
17+
<div class="col">
18+
<div class="card shadow">
19+
<div class="card-body">
20+
<table class="table">
21+
<thead class="thead-dark">
22+
<tr>
23+
<th scope="col">#</th>
24+
<th scope="col">Nama</th>
25+
<th scope="col">NRP</th>
26+
<th scope="col">Departemen</th>
27+
<th scope="col">Asal</th>
28+
<th scope="col">Tanggal</th>
29+
<th scope="col">Tindakan</th>
30+
</tr>
31+
</thead>
32+
<tbody>
33+
<?php foreach($minings as $key => $mining): ?>
34+
<tr>
35+
<th scope="row"><?= $key + 1 ?></th>
36+
<td><?= $mining['nama'] ?></td>
37+
<td><?= $mining['nrp'] ?></td>
38+
<td><?= $mining['departemen'] ?></td>
39+
<td><?= $mining['asal'] ?></td>
40+
<td><?= $mining['mining_at'] ?></td>
41+
<td>
42+
<a href="update.php?id=<?= $mining['nrp'] ?>" class="btn btn-primary shadow"><img src="icon/pencil.svg" width="20"></a>
43+
<a href="delete.php?id=<?= $mining['nrp'] ?>" class="btn btn-danger shadow" onclick="return confirm('Data akan terhapus')"><img src="icon/trash.svg" width="20"></a>
44+
</td>
45+
</tr>
46+
<?php endforeach; ?>
47+
</tbody>
48+
</table>
49+
</div>
50+
</div>
51+
</div>
52+
<div class="col col-2 text-center side-navbar">
53+
<div class="card shadow">
54+
<div class="card-body">
55+
<a href="form.php" class="btn btn-primary btn-sm">Daftar</a>
56+
</div>
57+
</div>
58+
</div>
59+
</div>
60+
</div>

init.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
require 'database/config.php';
3+
require 'functions/functions.php';

process-form.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
require 'includes/header.php';
3+
if (isset($_POST['daftar'])) {
4+
if (insert($_POST)) {
5+
return header('Location: index.php');
6+
} else {
7+
return "error";
8+
}
9+
} else {
10+
die("Error");
11+
}
12+
?>

styles/style.css

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.custom {
2+
background-color: #74baff85;
3+
margin: 10px 5px;
4+
border-radius: 10px;
5+
padding: 10px;
6+
}

0 commit comments

Comments
 (0)