Commit 07207015 authored by Regita Fridawati's avatar Regita Fridawati
Browse files

Form Online Order

parents
No related merge requests found
Pipeline #3509 failed with stages
in 1 minute and 7 seconds
Showing with 265 additions and 0 deletions
+265 -0
<?php
class Form {
public function displayForm($action, $submit, $fields) {
echo "<form action='$action' method='post'>";
echo "<table>";
foreach ($fields as $field) {
echo "<tr>
<td>{$field['label']}</td>
<td>{$field['input']}</td>
</tr>";
}
echo "<tr><td><input type='submit' value='$submit'></td></tr>";
echo "</table></form>";
}
public function addTextField($name, $label) {
return ['label' => $label, 'input' => "<input type='text' name='$name'>"];
}
public function addSelectField($name, $label, $options) {
$input = "<select name='$name'>";
foreach ($options as $value => $display) {
$input .= "<option value='$value'>$display</option>";
}
$input .= "</select>";
return ['label' => $label, 'input' => $input];
}
public function addCheckboxField($name, $label, $options) {
$input = "";
foreach ($options as $value => $display) {
$input .= "<input type='checkbox' name='{$name}[]' value='$value'> $display ";
}
return ['label' => $label, 'input' => $input];
}
public function addTextareaField($name, $label) {
return ['label' => $label, 'input' => "<textarea name='$name'></textarea>"];
}
}
?>
<?php
class Database {
private $host = 'localhost';
private $db_name = 'slice_bread';
private $username = 'root';
private $password = '';
public $conn;
public function __construct() {
$this->conn = null;
try {
$this->conn = new PDO("mysql:host={$this->host};dbname={$this->db_name}", $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
}
}
}
?>
<?php
require_once 'Database.php';
require_once 'Form.php';
require_once 'RotiTawar.php';
require_once 'RotiManis.php';
require_once 'RotiAsin.php';
require_once 'Pesanan.php';
$database = new Database();
$form = new Form();
// Daftar produk
$products = [
new RotiTawar(),
new RotiManis(),
new RotiAsin()
];
// Menyiapkan fields untuk form
$fields[] = $form->addTextField('nama', 'Nama Pembeli');
$fields[] = $form->addSelectField('produk', 'Pilih Produk', array_map(fn($product) => $product->getNama(), $products));
$fields[] = $form->addTextField('jumlah', 'Jumlah');
$fields[] = $form->addCheckboxField('toppings', 'Toppings', ['Keju', 'Coklat', 'Kismis']);
$fields[] = $form->addTextareaField('catatan', 'Catatan');
// Tampilkan form pemesanan
$form->displayForm('proses_pesanan.php', 'Buat Pesanan', $fields);
?>
<?php
require_once 'Database.php';
require_once 'RotiTawar.php';
require_once 'RotiManis.php';
require_once 'RotiAsin.php';
class Pesanan {
private $conn;
private $produk;
private $jumlah;
private $toppings;
private $catatan;
public function __construct(Database $database, Produk $produk, $jumlah, $toppings, $catatan) {
$this->conn = $database->conn;
$this->produk = $produk;
$this->jumlah = $jumlah;
$this->toppings = $toppings;
$this->catatan = $catatan;
}
public function simpanPesanan() {
$totalHarga = $this->produk->getHarga() * $this->jumlah;
$query = $this->conn->prepare("INSERT INTO pesanan (nama_produk, jumlah, topping, catatan, total_harga) VALUES (?, ?, ?, ?, ?)");
$query->execute([$this->produk->getNama(), $this->jumlah, implode(',', $this->toppings), $this->catatan, $totalHarga]);
return $this->conn->lastInsertId();
}
}
?>
<?php
class Produk {
protected $nama;
protected $harga;
public function __construct($nama, $harga) {
$this->nama = $nama;
$this->harga = $harga;
}
public function getNama() {
return $this->nama;
}
public function getHarga() {
return $this->harga;
}
}
?>
<?php
require_once 'Database.php';
require_once 'RotiTawar.php';
require_once 'RotiManis.php';
require_once 'RotiAsin.php';
require_once 'Pesanan.php';
$database = new Database();
// Ambil data dari form
$produkIndex = $_POST['produk'];
$jumlah = $_POST['jumlah'];
$toppings = isset($_POST['toppings']) ? $_POST['toppings'] : [];
$catatan = $_POST['catatan'];
// Pilih produk berdasarkan index
$products = [
new RotiTawar(),
new RotiManis(),
new RotiAsin()
];
$produk = $products[$produkIndex];
// Buat pesanan dan simpan
$pesanan = new Pesanan($database, $produk, $jumlah, $toppings, $catatan);
$pesananId = $pesanan->simpanPesanan();
echo "Pesanan Anda dengan ID $pesananId telah berhasil disimpan.";
?>
<?php
require_once 'Produk.php';
class RotiAsin extends Produk {
public function __construct() {
parent::__construct('Roti Asin', 18000);
}
}
?>
<?php
require_once 'Produk.php';
class RotiManis extends Produk {
public function __construct() {
parent::__construct('Roti Manis', 20000);
}
}
?>
<?php
require_once 'Produk.php';
class RotiTawar extends Produk {
public function __construct() {
parent::__construct('Roti Tawar', 15000);
}
}
?>
-- phpMyAdmin SQL Dump
-- version 5.2.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Sep 29, 2024 at 09:04 PM
-- Server version: 10.4.32-MariaDB
-- PHP Version: 8.2.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `slice_bread`
--
-- --------------------------------------------------------
--
-- Table structure for table `pesanan`
--
CREATE TABLE `pesanan` (
`id` int(11) NOT NULL,
`nama_produk` varchar(255) DEFAULT NULL,
`jumlah` int(11) DEFAULT NULL,
`topping` text DEFAULT NULL,
`catatan` text DEFAULT NULL,
`total_harga` decimal(10,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
--
-- Dumping data for table `pesanan`
--
INSERT INTO `pesanan` (`id`, `nama_produk`, `jumlah`, `topping`, `catatan`, `total_harga`) VALUES
(1, 'Roti Manis', 10, '1', 'hai', 200000.00),
(2, 'Roti Manis', 10, '1', 'hai2', 200000.00);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `pesanan`
--
ALTER TABLE `pesanan`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `pesanan`
--
ALTER TABLE `pesanan`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment