Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Koneksi
- {
- private $servername = "localhost";
- private $dbname = "dakademik";
- private $username = "root";
- private $password = "";
- /** Koneksi **/
- private $conn;
- /** Konstruktor **/
- function __construct()
- {
- $this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname); // Buka Koneksi
- if($this->conn->connect_error)
- {
- die("Connection Failed : ". $conn->connect_error);
- }
- }
- function __destruct()
- {
- if(isset($this->conn))
- $this->conn->close(); // Tutup Koneksi
- }
- /** Getter Koneksi **/
- protected function getKoneksi()
- {
- return $this->conn;
- }
- }
- class Mahasiswa extends Koneksi
- {
- private $NIM;
- private $Nama;
- private $IP;
- private $stmt;
- function __construct()
- {
- parent::__construct();
- }
- function __destruct()
- {
- if(isset($this->stmt))
- $this->stmt->close();
- parent::__destruct();
- }
- public function __call($name, $arguments)
- {
- switch ($name)
- {
- /** SETTER **/
- case 'setNIM' :
- $this->NIM = $arguments[0];
- break;
- case 'setNama' :
- $this->Nama = $arguments[0];
- break;
- case 'setIP' :
- $this->IP = $arguments[0];
- break;
- case 'setData' :
- if(sizeof($arguments) == 3)
- {
- $this->NIM = $arguments[0];
- $this->Nama = $arguments[1];
- $this->IP = $arguments[2];
- }
- break;
- /** GETTER **/
- case 'getNIM' :
- return $this->NIM;
- break;
- case 'getNama' :
- return $this->Nama;
- break;
- case 'getIP' :
- return $this->IP;
- break;
- }
- }
- /** ALL DATA **/
- public function getListResultQuery()
- {
- $conn = parent::getKoneksi();
- $sql = "SELECT * FROM mahasiswa";
- $result = $conn->query($sql);
- return $result;
- }
- public function saveData()
- {
- if(isset($this->NIM) and isset($this->Nama) and isset($this->IP))
- {
- $conn = parent::getKoneksi();
- $this->stmt = $conn->prepare("INSERT INTO `mahasiswa`(`NIM`, `Nama`, `IP`) VALUES (?,?,?)");
- $this->stmt->bind_param('sss',$this->NIM, $this->Nama, $this->IP);
- $this->stmt->execute();
- return $this->stmt->affected_rows > 0;
- }
- return false;
- }
- public function searchNama()
- {
- if(isset($this->Nama))
- {
- $conn = parent::getKoneksi();
- $this->stmt = $conn->prepare("SELECT `NIM`, `Nama`, `IP` FROM `mahasiswa` WHERE `Nama` LIKE ? ");
- $param = '%'.$this->Nama.'%';
- $this->stmt->bind_param('s', $param);
- $this->stmt->execute();
- return $this->stmt->get_result();
- }
- else
- return null;
- }
- public function deleteData()
- {
- if(isset($this->NIM))
- {
- $conn = parent::getKoneksi();
- $this->stmt = $conn->prepare("DELETE FROM `mahasiswa` WHERE `NIM` = ?");
- $param = $this->NIM;
- $this->stmt->bind_param('s', $param);
- $this->stmt->execute();
- return $this->stmt->get_result();
- }
- else
- return false;
- }
- }
- $mhs = new Mahasiswa();
- $msg = null;
- $aksi = isset($_POST['Aksi']) ? $_POST['Aksi'] : null;
- $listquery = null;
- switch($aksi)
- {
- case 'input' :
- if(isset($_POST['NIM']) and isset($_POST['Nama']) and isset($_POST['IP']))
- {
- $mhs->setData($_POST['NIM'], $_POST['Nama'], $_POST['IP']);
- if($mhs->saveData())
- $msg = 'Insert Data '.$mhs->getNIM().' Sukses';
- else
- $msg = 'Insert Data '.$mhs->getNIM().' Gagal';
- }
- $listquery = $mhs->getListResultQuery();
- break;
- case 'search':
- $mhs->setNama($_POST['Nama']);
- $listquery = $mhs->searchNama();
- break;
- case 'delete' :
- $mhs->setNIM($_POST['NIM']);
- $status = $mhs->deleteData();
- if(isset($status))
- $msg = 'Data '.$mhs->getNIM().' Sukses di Delete';
- else
- $msg = 'Data '.$mhs->getNIM().' Gagal di Delete';
- $listquery = $mhs->getListResultQuery();
- break;
- default :
- $listquery = $mhs->getListResultQuery();
- break;
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Latihan 20</title>
- <style>
- table {
- font-family: arial, sans-serif;
- border-collapse: collapse;
- width: 100%;
- }
- td, th {
- border: 1px solid #dddddd;
- text-align: left;
- padding: 8px;
- }
- tr:nth-child(even) {
- background-color: #dddddd;
- }
- ul {
- font-family: arial, sans-serif;
- list-style-type: none;
- width: 100%;
- }
- li {
- text-align: left;
- padding: 8px;
- }
- input[type=text], select {
- width: 100%;
- padding: 12px 20px;
- margin: 8px 0;
- display: inline-block;
- border: 1px solid #ccc;
- border-radius: 4px;
- box-sizing: border-box;
- }
- input[type=submit] {
- width: 100%;
- background-color: #4CAF50;
- color: white;
- padding: 14px 20px;
- margin: 8px 0;
- border: none;
- border-radius: 4px;
- cursor: pointer;
- }
- input[type=submit]:hover {
- background-color: #45a049;
- }
- div {
- border-radius: 5px;
- background-color: #f2f2f2;
- padding: 20px;
- }
- .container-center{
- width: 80%;
- margin-left: 10%;
- margin-right: 10%;
- margin-top: 10px;
- margin-bottom: 10px;
- display: inline-block;
- }
- .container-half{
- width: 40%;
- margin-left: 1%;
- margin-right: 1%;
- margin-top: 10px;
- margin-bottom: 10px;
- }
- .text-center
- {
- text-align:center;
- }
- .left{
- float:left;
- }
- .right{
- float:right;
- }
- </style>
- </head>
- <body>
- <div class="container-center">
- <h2 class="text-center"> Latihan 20 </h2>
- <?php
- if(isset($msg))
- echo '<a class="text-center">'.$msg.'</a><br/>';
- ?>
- </div>
- <div class="container-center">
- <div class="container-half left">
- <div>
- <h2 class="text-center">Search Mahasiswa</h2>
- <form action="" method="post">
- <label for="Nama">Nama</label>
- <input type="text" name="Nama" placeholder="Nama">
- <input type="hidden" name="Aksi" value="search">
- <input type="submit" value="Submit">
- </form>
- </div>
- <div>
- <h2 class="text-center">Input Mahasiswa</h2>
- <form action="" method="post">
- <label for="NIM">NIM</label>
- <input type="text" name="NIM" placeholder="NIM">
- <label for="Nama">Nama</label>
- <input type="text" name="Nama" placeholder="Nama">
- <label for="IP">IP</label>
- <input type="text" name="IP" placeholder="IP">
- <input type="hidden" name="Aksi" value="input">
- <input type="submit" value="Submit">
- </form>
- </div>
- </div>
- <div class="container-half right">
- <h2 class="text-center">List Mahasiswa</h2>
- <table>
- <tr>
- <th>NIM</th>
- <th>Nama</th>
- <th>IP</th>
- <th>Aksi</th>
- </tr>
- <?php
- if( $listquery->num_rows > 0)
- {
- while($row = $listquery->fetch_assoc())
- {
- ?>
- <tr>
- <td><?=$row['NIM'];?></td>
- <td><?=$row['Nama'];?></td>
- <td><?=$row['IP'];?></td>
- <td>
- <form action="" method="post">
- <input type="hidden" name="NIM" value="<?=$row['NIM'];?>">
- <input type="hidden" name="Aksi" value="delete">
- <button type="submit" value="Delete">Delete</button>
- </form>
- </td>
- </tr>
- <?php
- }
- }
- ?>
- </table>
- </div>
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment