first commit

This commit is contained in:
alazhar 2020-01-02 23:24:47 +07:00
commit 27c9266c66
13 changed files with 704 additions and 0 deletions

0
README.md Normal file
View File

9
bootstrap.min.css vendored Normal file

File diff suppressed because one or more lines are too long

232
database.php Normal file
View File

@ -0,0 +1,232 @@
<?php
class Database
{ private $db_host = 'localhost';
private $db_user = 'project';
private $db_pass = '';
private $db_name = 'rai_crud';
private $connection = false;
protected $result = array();
public function connect(){
if(!$this->connection){
$mycon = @mysql_connect($this->db_host,$this->db_user,$this->db_pass);
if($mycon){
$selectdb = @mysql_select_db($this->db_name,$mycon);
if($selectdb){
$this->connection = true;
return true;
}else{
echo "Tidak Menemukan Database";
return false;
}
}else{
echo "Tidak bisa tersambung ke mysql";
return false;
}
}else{
return true;
}
}
public function disconnect(){
if($this->connection){
if(@mysql_close()){
$this->connection = false;
return true;
}else{
return false;
}
}
}
public function select($table, $rows = '*', $where = null, $order = null){
$q = 'SELECT '.$rows.' FROM '.$table;
if($where != null) $q .= ' WHERE '.$where;
if($order != null) $q .= ' ORDER BY '.$order;
//echo $q;
if($this->tableExists($table)){
//echo $q;
$query = @mysql_query($q);
if($query){
$this->numRows = mysql_num_rows($query); //retrieves the number of rows from the query
for($i = 0; $i < $this->numRows; $i++)
{
$r = mysql_fetch_array($query); //mysql_fetch_array returns an array that corresponds to the fetched row
$key = array_keys($r); //array_keys() returns the keys, numeric and string, from the input array.
for($x = 0; $x < count($key); $x++) //count() counts all elements in an array
{
// Sanitizes keys so only alphavalues are allowed
if(!is_int($key[$x]))
{
if(mysql_num_rows($query) > 1)
$this->result[$i][$key[$x]] = $r[$key[$x]];
else if(mysql_num_rows($query) < 1)
$this->result = null;
else
$this->result[$key[$x]] = $r[$key[$x]];
}
}
}
return true;
//return $query;
}else{
echo "Ada kesalahan pada query";
return false;
}
}else {
echo "Table tidak ditemukan";
return false;
}
}
public function selectwithpaging($table, $limit = null, $hal = null,$where = null){
$rows = '*';
$q = 'SELECT '.$rows.' FROM '.$table;
if($where != null) $q .= ' WHERE '.$where;
if (!empty($hal)) {
$page = $hal - 1;
$MulaiAwal = $limit * $page;
} else if (!empty($hal) and $hal == 1) {
$MulaiAwal = 0;
} else if (empty($hal)) {
$MulaiAwal = 0;
}
$q .= ' LIMIT '.$MulaiAwal.' , '.$limit;
if($this->tableExists($table)){
$query = @mysql_query($q);
if($query){
$this->numRows = mysql_num_rows($query); //retrieves the number of rows from the query
for($i = 0; $i < $this->numRows; $i++)
{
$r = mysql_fetch_array($query); //mysql_fetch_array returns an array that corresponds to the fetched row
$key = array_keys($r); //array_keys() returns the keys, numeric and string, from the input array.
for($x = 0; $x < count($key); $x++) //count() counts all elements in an array
{
// Sanitizes keys so only alphavalues are allowed
if(!is_int($key[$x]))
{
if(mysql_num_rows($query) > 1)
$this->result[$i][$key[$x]] = $r[$key[$x]];
else if(mysql_num_rows($query) < 1)
$this->result = null;
else
$this->result[$key[$x]] = $r[$key[$x]];
}
}
}
return true;
//return $query;
}else{
echo "Ada kesalahan pada query";
return false;
}
}else {
echo "Table tidak ditemukan";
return false;
}
}
public function insert($table,$values,$rows = null) {
if($this->tableExists($table)){
$insert = 'INSERT INTO '.$table;
if($rows != null)$insert .= ' ('.$rows.')';
for($i = 0; $i < count($values); $i++){
if(is_string($values[$i]))$values[$i] = '"'.$values[$i].'"';
}
$values = implode(',',$values);
$insert .= ' VALUES ('.$values.')';
echo $insert;
$insert_query = @mysql_query($insert);
if($insert_query){
return true;
}else{
return false;
}
}
}
public function delete($table,$where = null){
if($this->tableExists($table)){
if($where == null){
$delete = 'DELETE '.$table;
}else{
$delete = 'DELETE FROM '.$table.' WHERE '.$where; //equivalent to DELETE FROM table1 WHERE ......
}
$delete = @mysql_query($delete);
if($delete){
return true;
}else{
return false;
}
}else{
return false;
}
}
public function update($table,$rows,$where){
if($this->tableExists($table)){
/*
for($i = 0; $i < count($where); $i++){
if($i%2 != 0){
if(is_string($where[$i])){
if(($i+1) != null)$where[$i] = '"'.$where[$i].'" AND ';
else $where[$i] = '"'.$where[$i].'"';
}
}
}
$where = implode('',$where);
*/
$update = 'UPDATE '.$table.' SET ';
$keys = array_keys($rows);
for($i = 0; $i < count($rows); $i++){
if(is_string($rows[$keys[$i]])) $update .= $keys[$i].'="'.$rows[$keys[$i]].'"';
else $update .= $keys[$i].'='.$rows[$keys[$i]];
// add commas
if($i != count($rows)-1) $update .= ',';
}
$update .= ' WHERE '.$where;
//echo $update;
$query = @mysql_query($update);
if($query)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public function getResult(){
return $this->result;
}
private function tableExists($table){
$tableInDb = @mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"');
if($tableInDb)
{
if(mysql_num_rows($tableInDb)==1)
{
return true;
}
else
{
return false;
}
}
}
}
?>

149
form.php Normal file
View File

@ -0,0 +1,149 @@
<!doctype html>
<?php
include('database.php');
include('mahasiswa.php');
$mhs = new Mahasiswa();
$mhs->connect();
if(!isset($_POST['submit'])){
if (isset($_GET['id'])){
$car = $_GET['id'];
$action='update&id='.$_GET['id'].'';
$mhs->select('mahasiswa','*',"nim='$car'");
$res = $mhs->getResult();
}else{
$action='insert';
}
}
else{
if ($_GET['action']=='update'){
$id = $_GET['id'];
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$telp = $_POST['telp'];
$asal = $_POST['asal'];
$alamat = $_POST['alamat'];
if(!empty($_FILES['file']['name'])){
$photos = $mhs->foto($_FILES,$id,'update',$nim);
$foto = $foto['lokasi'];
}
else{
$foto = $mhs->renamefoto($id,$nim);
}
$rows=array(
'nim' => $nim,
'nama' => $nama,
'telp' => $telp,
'asal' => $asal,
'alamat'=> $alamat,
'foto' => $foto
);
if($mhs->update('mahasiswa',$rows,'nim = '.$_GET['id'].'')){
header('Location: index.php');
}else{
echo 'Tidak bisa input';
}
}
else{
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$telp = $_POST['telp'];
$asal = $_POST['asal'];
$alamat = $_POST['alamat'];
if(!empty($_FILES['file']['name'])){
$foto = $mhs->foto($_FILES,$nim,'insert',$nim);
$lokasi = $foto['lokasi'];
$values=array($nim,$nama,$telp,$asal,$alamat,$lokasi);
$rows = null;
}else{
$values=array($nim,$nama,$telp,$asal,$alamat);
$rows= "nim,nama,telp,asal,alamat";
}
if($mhs->insert('mahasiswa',$values,$rows)){
header('Location: index.php');
}else{
echo 'Tidak bisa input';
}
}
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link href="style.css" rel="stylesheet">
<link href="bootstrap.min.css" rel="stylesheet">
<style type="text/css">
</style>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Registrasi</h1>
</div>
<form method="post" action="?action=<?php echo $action ?>" enctype="multipart/form-data">
<fieldset>
<legend>Data</legend>
<table>
<tr>
<td width="300px">Nama</td>
<td width="30px">:</td>
<td><input type="text" name="nama" value="<?php if (isset($_GET['id'])) echo $res['nama']; ?>" maxlength="30" required></td>
</tr>
<tr>
<td>NIM</td>
<td>:</td>
<td><input type="text" name="nim" value="<?php if (isset($_GET['id'])) echo $res['nim']; ?>" onkeypress="return isNumberKey(event)" maxlength="12" required></td>
</tr>
<tr>
<td>Alamat</td>
<td>:</td>
<td><input type="text" name="alamat" value="<?php if (isset($_GET['id'])) echo $res['alamat']; ?>" maxlength="50" required></td>
</tr>
<tr>
<td>Telp</td>
<td>:</td>
<td><input type="text" name="telp" value="<?php if (isset($_GET['id'])) echo $res['telp']; ?>" onkeypress="return isNumberKey(event)" maxlength="20" required></td>
</tr>
<tr>
<td>Sekolah Asal</td>
<td>:</td>
<td><input type="text" name="asal" value="<?php if (isset($_GET['id'])) echo $res['asal']; ?>" maxlength="20" required></td>
</tr>
<tr>
<td>Photo</td>
<td>:</td>
<td style="text-align:center">
<?php if (!empty($res['foto'])){
?>
<div style="background-color:#f0f0f0;padding:10px;">
<img src="<?php echo $res['foto'] ?>" height="200" width="300" name="foto"></img></br>
</div>
<?php
}
?>
<br/><input type="file" name="file" id="inputfile" style>
</td>
</tr>
</table>
</fieldset>
<div style="text-align:center;margin:20px;">
<a href="index.php"><button type="button" class="btn"><i class=" icon-ban-circle"></i> Cancel</button></a>
<button type="submit" name="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i> Save</button>
</div>
</form>
</div>
</body>
</html>
<script>
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
{
alert('Number Only');
return false;
}
return true;
};
</script>

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

105
index.php Normal file
View File

@ -0,0 +1,105 @@
<?php
include('database.php');
include('mahasiswa.php');
$mhs = new Mahasiswa();
$mhs->connect();
if (isset($_GET['id'])){
$mhs->deletemhs($_GET['id']);
}
$page = null;
$search = null;
//cek apakah ada getPage
if (isset($_GET['page']))$page = $_GET['page'];
//cek apakah ada getSearch
if ((isset($_GET['search'])) && (($_GET['search'])!='')){
$search = $_GET['search'];
$mhs->select('mahasiswa','*',"nama LIKE '%$search%' or nim LIKE '%$search%'");
}
else{
$mhs->select('mahasiswa');
}
$res = $mhs->getResult();
//pagination
if (!array_key_exists(0, $res)){
$resu[0]=$res;
$result = $mhs->paging($resu,5,$page,$search);
}
else{
$result = $mhs->paging($res,5,$page,$search);
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link href="style.css" rel="stylesheet">
<link href="bootstrap.min.css" rel="stylesheet">
<script>
function delconfirm(id){
var del=confirm('Are you sure you want to delete '+id+' record?');
if (del==true) window.location.href = 'index.php?id='+id;
else window.location.href = 'index.php';
}
</script>
</head>
<body>
<div class="container" style="margin-top:50px;">
<div class="page-header">
<h1>Data Mahasiswa</h1>
</div>
<div class="row">
<div class="navbar span11">
<div class="navbar-inner">
<form class="navbar-search pull-left" method="get" action="index.php">
<div class="input-append">
<input type="text" class="span2" name="search" placeholder="Search..." value="<?php if (isset($_GET['search'])) echo $_GET['search']; ?>">
<a href="index.php"><button type="button" class="btn btn-small"><i class="icon-remove"></i></button></a>
</div>
</form>
<a href="form.php"><button class="btn btn-small btn-primary pull-right" style="margin-top:8px;">+ Tambah</button></a>
</div>
</div>
</div>
<div class="row"><div class="span11">
<table class="table table-striped table-bordered">
<thead>
<th style="width:100px">NIM</th>
<th style="width:100px">Nama</th>
<th style="width:200px">Alamat</th>
<th style="width:130px">Telp</th>
<th style="width:200px">Sekolah Asal</th>
<th style="width:50px">Action</th>
</thead>
<tbody>
<?php
$i=$result['mulai'];
if( !empty( $result['data'] ) ){
foreach ($result['data'] as $value) {
?>
<tr>
<td><?php echo $result['data'][$i]['nim'] ?></td>
<td><?php echo $result['data'][$i]['nama'] ?></td>
<td><?php echo $result['data'][$i]['alamat'] ?></td>
<td><?php echo $result['data'][$i]['telp'] ?></td>
<td><?php echo $result['data'][$i]['asal'] ?></td>
<td>
<a href="<?php echo 'view.php?id='.$result['data'][$i]['nim'] ?>"><i class="icon-eye-open" title="View"></i></a>
<a href="<?php echo 'form.php?id='.$result['data'][$i]['nim'] ?>"><i class="icon-edit" title="Edit"></i></a>
<i class="icon-trash" title="Delete" onclick="delconfirm(<?php echo $result['data'][$i]['nim'] ?>)"></i>
</td>
</tr>
<?php
$i++;
}
}
?>
</tbody>
</table>
</div></div>
<?php
echo $result['paging'];
$mhs->disconnect();
?>
</div>
</body>
</html>

141
mahasiswa.php Normal file
View File

@ -0,0 +1,141 @@
<?php
class Mahasiswa extends Database{
private $nama;
private $nim;
private $telp;
private $alamat;
private $asal;
private $pagination;
public function paging($data,$Batas,$hal = 1,$cari = null){
//print_r($this->result);
if (!empty($hal)) {
$page = $hal - 1;
$MulaiAwal = $Batas * $page;
} else if (!empty($hal) and $hal == 1) {
$MulaiAwal = 0;
} else if (empty($hal)) {
$MulaiAwal = 0;
}
for ($i = $MulaiAwal; $i < $MulaiAwal+$Batas; $i++) {
if (array_key_exists($i, $data)){
$new[$i] = $data[$i];
}
}
$pagination = "";
$jumlahData = sizeof($data);
//echo "</br>".$jumlahData." Hasil";
if ($jumlahData > $Batas) {
$pagination = $pagination.'<div class="pagination" style="font-size:10pt;"><ul>';
$c = ceil($jumlahData / $Batas);
for ($i = 1; $i <= $c; $i++) {
$pagination = $pagination.'<li ';
if ($hal == $i) {
$pagination = $pagination.'class="active"';
}
$pagination = $pagination."><a href=?";
if(isset($cari))$pagination = $pagination."search=".$cari;
$pagination = $pagination."&page=".$i.">".$i."</a></li>";
}
$pagination = $pagination.'</ul></div>';
}
$hasil = array(
"paging" => $pagination,
"data" => $new,
"mulai" => $MulaiAwal,
);
return $hasil;
}
public function getPaging($array){
return $this->pagination;
}
public function foto($file,$id = null,$aksi,$newid){
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $file["file"]["name"]);
$extension = end($temp);
if ((($file["file"]["type"] == "image/gif")
|| ($file["file"]["type"] == "image/jpeg")
|| ($file["file"]["type"] == "image/jpg")
|| ($file["file"]["type"] == "image/pjpeg")
|| ($file["file"]["type"] == "image/x-png")
|| ($file["file"]["type"] == "image/png"))
&& ($file["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($file["file"]["error"] > 0)
{
echo "Return Code: " . $file["file"]["error"] . "<br>";
}
else
{
if ((file_exists("upload/" . $id.".".$extension)) && $aksi == 'insert')
{
echo 'aksi : insert dan file sudah ada </br>';
echo $id . " already exists. ";
$hasil = array(
'bool' => false
);
}
else if ((file_exists("upload/" . $id.".".$extension)) && $aksi == 'update')
{
echo 'aksi : update dan file sudah ada';
unlink("upload/" . $id.".".$extension);
move_uploaded_file($file["file"]["tmp_name"],
"upload/" . $newid.".".$extension);
$hasil = array(
'lokasi' => "upload/" . $newid.".".$extension,
'bool' => true
);
}
else
{
echo 'aksi : insert/update tapi file belum ada';
move_uploaded_file($file["file"]["tmp_name"],
"upload/" . $newid.".".$extension);
$hasil = array(
'lokasi' => "upload/" . $newid.".".$extension,
'bool' => true
);
}
}
}
else
{
$hasil = array(
'bool' => false
);
}
return $hasil;
}
public function deletemhs($id){
if($this->delete('mahasiswa','nim = '.$id.'')){
$files = glob("upload/$id.*");
foreach ($files as $file) {
unlink($file);
}
header('Location: index.php');
}
else echo 'Gagal hapus '.$id.'.*';
}
public function renamefoto($id,$newid){
$this->select('mahasiswa', 'foto', "nim='$id'");
$res = $this->getResult();
$file = $res['foto'];
$path_parts = pathinfo($file);
//echo $path_parts['dirname'], "\n";
//echo $path_parts['basename'], "\n";
//echo $path_parts['extension'], "\n";
//echo $path_parts['filename'], "\n";
rename ('upload/'.$id.'.'.$path_parts['extension'], 'upload/'.$newid.'.'.$path_parts['extension']);
$newfile = 'upload/'.$newid.'.'.$path_parts['extension'];
return $newfile;
}
}
?>

BIN
upload/1102119878.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

BIN
upload/1103113445.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

BIN
upload/1103131313.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

BIN
upload/nophoto.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

68
view.php Normal file
View File

@ -0,0 +1,68 @@
<!doctype html>
<?php
include('database.php');
include('mahasiswa.php');
$mhs = new Mahasiswa();
$mhs->connect();
$car = $_GET['id'];
$mhs->select('mahasiswa','*',"nim='$car'");
$res = $mhs->getResult();
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link href="style.css" rel="stylesheet">
<link href="bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body{
background-color: #f3f3f3;
}
.container{
margin:100px;
background-color: #fff;
box-shadow:0 1px 3px rgba(0,0,0,.5);
padding: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Informasi</h1>
</div>
<table class="table">
<tr>
<td rowspan="5" width="200"><img src="<?php if (!empty($res['foto'])) echo $res['foto']; else echo 'upload/nophoto.jpg'; ?>" width="200" height="300"/></td>
<td width="100">Nama</td>
<td width="10">:</td>
<td><?php echo $res['nama']; ?></td>
</tr>
<tr>
<td>NIM</td>
<td>:</td>
<td><?php echo $res['nim']; ?></td>
</tr>
<tr>
<td>Alamat</td>
<td>:</td>
<td><?php echo $res['alamat']; ?></td>
</tr>
<tr>
<td>Telp</td>
<td>:</td>
<td><?php echo $res['telp']; ?></td>
</tr>
<tr>
<td>Sekolah Asal</td>
<td>:</td>
<td><?php echo $res['asal']; ?></td>
</tr>
</table>
<div style="text-align:center;">
<a href="index.php"><button class="btn"><i class="icon-arrow-left"></i> Back</button></a>
<a href="<?php echo 'form.php?id='.$res['nim'] ?>"><button class="btn"><i class="icon-edit"></i> Update</button></a>
</div>
</div>
</body>
</html>