first commit

This commit is contained in:
alazhar 2020-01-02 23:21:46 +07:00
commit 3c8233afdc
14 changed files with 669 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

85
form.php Normal file
View File

@ -0,0 +1,85 @@
<!doctype html>
<?php
include 'query.php';
if (isset($_GET['id'])){
$action='update&id='.$_GET['id'].'';
$result = slcmhs($_GET['id']);
while($data = mysql_fetch_array($result)) {
$nim = $data['nim'];
$nama = $data['nama'];
$alamat = $data['alamat'];
$telp = $data['telp'];
$asal = $data['asal'];
$foto = $data['foto'];
}
}
else $action='insert';
?>
<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="query.php?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 $nama; ?>"></td>
</tr>
<tr>
<td>NIM</td>
<td>:</td>
<td><input type="text" name="nim" value="<?php if (isset($_GET['id'])) echo $nim; ?>"></td>
</tr>
<tr>
<td>Alamat</td>
<td>:</td>
<td><input type="text" name="alamat" value="<?php if (isset($_GET['id'])) echo $alamat; ?>"></td>
</tr>
<tr>
<td>Telp</td>
<td>:</td>
<td><input type="text" name="telp" value="<?php if (isset($_GET['id'])) echo $telp; ?>"></td>
</tr>
<tr>
<td>Sekolah Asal</td>
<td>:</td>
<td><input type="text" name="asal" value="<?php if (isset($_GET['id'])) echo $asal; ?>"></td>
</tr>
<tr>
<td>Photo</td>
<td>:</td>
<td>
<?php if (!empty($foto)){
?>
<img src="<?php echo $foto ?>" height="200" width="300"></img>
<?php
}else{
?>
<input type="file" name="file" id="file">
<?php } ?>
</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>

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

106
index.php Normal file
View File

@ -0,0 +1,106 @@
<!doctype html>
<?php
include 'query.php';
//$result = getmhs();
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link href="style.css" rel="stylesheet">
<link href="bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container" style="margin-top:50px;">
<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 search-query" name="search" placeholder="Search...">
</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">
<div class="tabel">
<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
$BatasAwal = 5;
if (!empty($_GET['page'])) {
$page = $_GET['page'] - 1;
$MulaiAwal = $BatasAwal * $page;
} else if (!empty($_GET['page']) and $_GET['page'] == 1) {
$MulaiAwal = 0;
} else if (empty($_GET['page'])) {
$MulaiAwal = 0;
}
if ((isset($_GET['search'])) && (($_GET['search'])!='')){
$car = $_GET['search'];
$query = mysql_query("SELECT * FROM mahasiswa WHERE nama LIKE '%$car%' or nim LIKE '%$car%' LIMIT $MulaiAwal , $BatasAwal");
}
else{
$query = mysql_query("SELECT * FROM mahasiswa LIMIT $MulaiAwal , $BatasAwal");
}
while($data = mysql_fetch_array($query)) {
?>
<tr>
<td><?php echo $data['nim'] ?></td>
<td><?php echo $data['nama'] ?></td>
<td><?php echo $data['alamat'] ?></td>
<td><?php echo $data['telp'] ?></td>
<td><?php echo $data['asal'] ?></td>
<td>
<a href="<?php echo 'view.php?id='.$data['nim'] ?>"><i class="icon-eye-open"></i></a>
<a href="<?php echo 'form.php?id='.$data['nim'] ?>"><i class="icon-edit"></i></a>
<a href="<?php echo 'query.php?id='.$data['nim'].'&action=delete' ?>"><i class="icon-trash"></i></a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div></div>
<div class="paging span11" style="text-align:center;">
<?php
if ((isset($_GET['search'])) && (($_GET['search'])!='')){
$car = $_GET['search'];
$cekQuery = mysql_query("SELECT * FROM mahasiswa WHERE nama LIKE '%$car%' or nim LIKE '%$car%'");
}
else{
$cekQuery = mysql_query("SELECT * FROM mahasiswa");
}
$jumlahData = mysql_num_rows($cekQuery);
echo $jumlahData;
if ($jumlahData > $BatasAwal) {
echo '<div class="pagination" style="font-size:10pt;"><ul>';
$c = ceil($jumlahData / $BatasAwal);
for ($i = 1; $i <= $c; $i++) {
echo '<li ';
if ($_GET['page'] == $i) {
echo 'class="active"';
}
echo "><a href=?";
if(isset($_GET['search']))echo "search=".$_GET['search'];
echo "&page=".$i.">".$i."</a></li>";
}
echo '</ul></div>';
}
?>
</div>
</div>
</body>
</html>

132
query.php Normal file
View File

@ -0,0 +1,132 @@
<?php
//koneksi database
$con = mysql_select_db('rai_crud',mysql_connect('localhost','project',''));
if (mysql_errno())
{
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
//fungsi update mahasiswa
function updmhs($id){
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$telp = $_POST['telp'];
$asal = $_POST['asal'];
$alamat = $_POST['alamat'];
$sql = "UPDATE mahasiswa
SET nim=$nim, nama='$nama', telp='$telp', alamat='$alamat', asal='$asal'
WHERE nim=$id";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
header('Location: index.php');
}
//fungsi hapus mahasiswa
function delmhs($id){
$sql = "DELETE FROM mahasiswa WHERE nim=$id";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
header('Location: index.php');
echo "<script type='text/javascript'>alert('One Record Deleted');</script>";
}
//fungsi cari mahasiswa
function srchmhs($search){
$sql = "";
$query = mysql_query($sql);
return $query;
}
//fungsi select mahasiswa
function slcmhs($id){
$sql = "SELECT * FROM mahasiswa WHERE nim=$id";
//echo $sql;
$query = mysql_query($sql);
//var_dump($query);
return $query;
}
//fungsi insert mahasiswa
function insmhs(){
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$telp = $_POST['telp'];
$asal = $_POST['asal'];
$alamat = $_POST['alamat'];
$foto = foto($_FILES,$nim);
$sql = "INSERT INTO mahasiswa (nim,nama,alamat,telp,asal,foto) VALUES ($nim,'$nama','$alamat','$telp','$asal','$foto')";
//echo $sql;
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
header('Location: index.php');
}
function foto($file,$id){
$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"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($file["file"]["error"] > 0)
{
echo "Return Code: " . $file["file"]["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $id.".".$extension))
{
echo $id . " already exists. ";
}
else
{
move_uploaded_file($file["file"]["tmp_name"],
"upload/" . $id.".".$extension);
$lokasi = "upload/" . $id.".".$extension;
return $lokasi;
}
}
}
else
{
echo "Invalid file";
}
}
if (isset($_GET['action'])) $func=$_GET['action'];
else $func='';
switch($func){
case 'delete' :
$id = $_GET['id'];
delmhs($id);
break;
case 'update' :
$id = $_GET['id'];
updmhs($id);
break;
case 'insert' :
insmhs();
break;
default :
//echo 'no run';
}
?>

0
style.css Normal file
View File

177
template_database.php Normal file
View File

@ -0,0 +1,177 @@
<?php
class Database
{ private $db_host = 'localhost'; //Database Host
private $db_user = 'root'; // Database User
private $db_pass = ''; // Database password
private $db_name = 'rai_crud'; //Database name
private $connection = false; // Checks to see if the connection is active
protected $result = array(); // Results returned from the query
public function connect(){
if(!$this->connection){ //checks if there is already a mysql connection active
$mycon = @mysql_connect($this->db_host,$this->db_user,$this->db_pass); //connecting to my mysql
if($mycon){
$selectdb = @mysql_select_db($this->db_name,$mycon);
if($selectdb){
$this->connection = true; //sets the value to variable connection to 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, $limit = null, $order = null){
$q = 'SELECT '.$rows.' FROM '.$table;
if($where != null) $q .= ' WHERE '.$where;
if($order != null) $q .= ' ORDER BY '.$order;
if($limit != null) $q .= ' LIMIT '.$limit;
//echo $q;
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.')';
$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;
$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;
}
}
}
}
?>

90
tutorial.php Normal file
View File

@ -0,0 +1,90 @@
<?php
require_once('database.php'); //connect to your database in this file
define("NUMBER_PER_PAGE", 5); //number of records per page of the search results
/****
* Purpose: paginate a result set
* Precondition: current page, total records, extra variables to pass in the page string
* Postcondition: pagination is displayed
****/
function pagination($current_page_number, $total_records_found, $query_string = null)
{
$page = 1;
echo "Page: ";
for ($total_pages = ($total_records_found/NUMBER_PER_PAGE); $total_pages > 0; $total_pages--)
{
if ($page != $current_page_number)
echo "<a href=\"?page=$page" . (($query_string) ? "&$query_string" : "") . "\">";
echo "$page ";
if ($page != $current_page_number)
echo "</a>";
$page++;
}
}
echo "<h2>Search Members</h2>
<form action='{$_SERVER['PHP_SELF']}' method='post'>
<p>ID: <input type='text' name='id' /></p>
<p>Username: <input type='text' name='username' /></p>
<p>Email: <input type='text' name='email' /></p>
<p align='center'>
<input type='submit' name='submit' value='Search' />
</p>
</form>";
/**
* Display Search Results Below Here
**/
//load the current paginated page number
$page = ($_GET['page']) ? $_GET['page'] : 1;
$start = ($page-1) * NUMBER_PER_PAGE;
/**
* if we used the search form use those variables, otherwise look for
* variables passed in the URL because someone clicked on a page number
**/
$id = ($_POST['id']) ? $_POST['id'] : $_GET['id'];
$username = ($_POST['username']) ? $_POST['username'] : $_GET['username'];
$email = ($_POST['email']) ? $_POST['email'] : $_GET['email'];
$sql = "SELECT * FROM members WHERE 1=1";
if ($id)
$sql .= " AND id='" . mysql_real_escape_string($id) . "'";
if ($username)
$sql .= " AND username='" . mysql_real_escape_string($username) . "'";
if ($email)
$sql .= " AND email='" . mysql_real_escape_string($email) . "'";
//this return the total number of records returned by our query
$total_records = mysql_num_rows(mysql_query($sql));
//now we limit our query to the number of results we want per page
$sql .= " LIMIT $start, " . NUMBER_PER_PAGE;
/**
* Next we display our pagination at the top of our search results
* and we include the search words filled into our form so we can pass
* this information to the page numbers. That way as they click from page
* to page the query will pull up the correct results
**/
pagination($page, $total_records, "id=$id&username=$username&email=$email");
$loop = mysql_query($sql)
or die ('cannot run the query because: ' . mysql_error());
while ($record = mysql_fetch_assoc($loop))
echo "<br/>{$record['id']}) " . stripslashes($record['username']) . " - {$record['email']}";
echo "<center>" . number_format($total_records) . " search results found</center>";
pagination($page, $total_records, "id=$id&username=$username&email=$email");
?>

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

70
view.php Normal file
View File

@ -0,0 +1,70 @@
<?php
include 'query.php';
$result = slcmhs($_GET['id']);
while($data = mysql_fetch_array($result)) {
$nim = $data['nim'];
$nama = $data['nama'];
$alamat = $data['alamat'];
$telp = $data['telp'];
$asal = $data['asal'];
$foto = $data['foto'];
}
?>
<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($foto)) echo $foto; else echo 'upload/nophoto.jpg'; ?>" width="200" height="300"/></td>
<td width="100">Nama</td>
<td width="10">:</td>
<td><?php echo $nama; ?></td>
</tr>
<tr>
<td>NIM</td>
<td>:</td>
<td><?php echo $nim; ?></td>
</tr>
<tr>
<td>Alamat</td>
<td>:</td>
<td><?php echo $alamat; ?></td>
</tr>
<tr>
<td>Telp</td>
<td>:</td>
<td><?php echo $telp; ?></td>
</tr>
<tr>
<td>Sekolah Asal</td>
<td>:</td>
<td><?php echo $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='.$nim ?>"><button class="btn"><i class="icon-edit"></i> Update</button></a>
</div>
</div>
</body>
</html>