php-crud-oop/mahasiswa.php
2020-01-02 23:24:47 +07:00

141 lines
3.6 KiB
PHP

<?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;
}
}
?>