Advertisement
rkhaidir16

KisiKisiUjian.php

Nov 18th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.84 KB | Source Code | 0 0
  1. <?php
  2.  
  3. /**
  4.  * awal program ada input login, login statis
  5.  * username: admin
  6.  * password: admin123
  7.  */
  8.  
  9.  /**
  10.   * Setelah login tampil menu utama program
  11.   * terdapat array, perungalan, percabangan, function (optional)
  12.   */
  13.  
  14.  
  15. $username = "admin";
  16. $password = "admin123";
  17.  
  18. $dataBarang = [
  19.   [
  20.     "nama_barang" => "RAM DDR4 8GB",
  21.     "stok"        => 20
  22.   ],
  23.   [
  24.     "nama_barang" => "Flashdisk 64GB",
  25.     "stok"        => 35
  26.   ],
  27.   [
  28.     "nama_barang" => "SSD 128 GB",
  29.     "stok"        => 10
  30.   ],
  31. ];
  32.  
  33. // Memasukkan username dan password
  34. echo "Masukkan username: ";
  35. $inputUsername = trim(fgets(STDIN));
  36. echo "Masukkan password: ";
  37. $inputPassword = trim(fgets(STDIN));
  38.  
  39. if ($inputUsername === $username && $inputPassword === $password) {
  40.   while (true) {
  41.     echo "Menu Utama: \n";
  42.     echo "1. Lihat data barang\n2. Tambah barang baru\n3. Keluar Program\n";
  43.     echo "Masukkan pilihan menu: ";
  44.     $pilih = trim(fgets(STDIN));
  45.    
  46.     if ($pilih == 1) {
  47.       tampilData($dataBarang);
  48.     } elseif ($pilih == 2) {
  49.       tambahBarang($dataBarang);
  50.     } elseif ($pilih == 3) {
  51.       echo "Terima kasih\n";
  52.       break;
  53.     } else {
  54.       echo "Menu tidak ada! ulangi. \n";
  55.     }
  56.   }
  57. } else {
  58.   echo "Username dan Password yang anda masukkan salah! ulangi\n";
  59. }
  60.  
  61.  
  62. // function menampilkan data barang
  63. function tampilData($data) {
  64.   echo "Data Barang\n\n";
  65.   foreach ($data as $index => $tampil) {
  66.     echo ($index+1) .". $tampil[nama_barang] sisa stok $tampil[stok]\n";
  67.   }
  68.   echo "\n";
  69. }
  70.  
  71. // function tambah barang
  72. function tambahBarang(&$data) {
  73.   echo "Masukkan nama barang: ";
  74.   $namaBarang = trim(fgets(STDIN));
  75.   echo "Masukkan stok barang: ";
  76.   $stokBarang = trim(fgets(STDIN));
  77.  
  78.   $data[] = [
  79.     "nama_barang" => $namaBarang,
  80.     "stok" => $stokBarang
  81.   ];
  82.   echo "Data berhasil ditambah \n\n";
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement