Regular Expressions 101

Community Patterns

There does not seem to be anything here

Community Library Entry

1

Regular Expression
PCRE2 (PHP >=7.3)

/
gamgamtestplf grundlegend
/
gm

Description

<?php // Session MUSS als erstes starten session_start(); // Session-Array initialisieren if (!isset($_SESSION['products'])) { $_SESSION['products'] = []; } // ---------- KLASSENDEFINITION ---------- class Product { // Private Membervariablen für alle Eingabefelder private string $name; private float $price; private string $category; // Konstruktor, der ausschließlich die Setter verwendet public function __construct(string $name, float $price, string $category) { $this->setName($name); $this->setPrice($price); $this->setCategory($category); } // Getter und Setter public function getName(): string { return $this->name; } public function setName(string $name): void { $this->name = trim($name); } public function getPrice(): float { return $this->price; } public function setPrice(float $price): void { // einfache Validierung, minimum 0 if ($price < 0) { $price = 0; } $this->price = $price; } public function getCategory(): string { return $this->category; } public function setCategory(string $category): void { $this->category = trim($category); } } // ---------- FORMULAR-AKTIONEN ---------- if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Aktion unterscheiden: speichern oder alles löschen $action = $_POST['action'] ?? 'save'; if ($action === 'clear') { // Alle löschen $_SESSION['products'] = []; } else { // Speichern $name = $_POST['name'] ?? ''; $priceRaw = $_POST['price'] ?? '0'; $category = $_POST['category'] ?? ''; // number-Input kommt als String, in float wandeln $price = (float) str_replace(',', '.', $priceRaw); // Neues Objekt der Basisklasse erzeugen $product = new Product($name, $price, $category); // In Session-Array speichern, Key = uniqid() $id = uniqid('p_', true); $_SESSION['products'][$id] = $product; } // Optional: Redirect, um Formular-Resubmits zu vermeiden header('Location: ' . $_SERVER['PHP_SELF']); exit; } ?> <!DOCTYPE html> <html lang="de"> <head> <meta charset="UTF-8"> <title>Product Webapp</title> <style> body { font-family: Arial, sans-serif; margin: 20px; } form { margin-bottom: 20px; } label { display: block; margin-top: 10px; } input, select { padding: 5px; width: 250px; max-width: 100%; } button { margin-top: 15px; padding: 6px 12px; cursor: pointer; } table { border-collapse: collapse; margin-top: 20px; width: 100%; max-width: 600px; } th, td { border: 1px solid #ccc; padding: 8px; } th { background: #eee; } </style> </head> <body> <h1>Produktverwaltung (Session-Demo)</h1> <form method="post" action=""> <!-- 2 unterschiedliche HTML-Types, eines davon number --> <label for="name">Produktname (type="text")</label> <input type="text" id="name" name="name" value="Standard Produkt" required >
<label for="price">Preis in € (type="number")</label>
<input
    type="number"
    id="price"
    name="price"
    value="9.99"
    min="0"
    step="0.01"
    required
>

<label for="category">Kategorie</label>
<select id="category" name="category">
    <option value="Elektronik" selected>Elektronik</option>
    <option value="Haushalt">Haushalt</option>
    <option value="Bücher">Bücher</option>
    <option value="Sonstiges">Sonstiges</option>
</select>

<div>
    <!-- Buttons/Aktionen -->
    <button type="submit" name="action" value="save">Speichern</button>
    <button type="submit" name="action" value="clear">Alle löschen</button>
</div>
</form> <h2>Gespeicherte Produkte (nur ein Feld wird ausgegeben)</h2> <?php if (!empty($_SESSION['products'])): ?>
<table>
    <thead>
        <tr>
            <th>#</th>
            <th>Produktname (eine Membervariable)</th>
        </tr>
    </thead>
    <tbody>
    <?php
    $counter = 1;
    foreach ($_SESSION['products'] as $id => $product): ?>
        <tr>
            <td><?php echo $counter++; ?></td>
            <td><?php echo htmlspecialchars($product->getName(), ENT_QUOTES, 'UTF-8'); ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
<?php else: ?>
<p>Aktuell sind keine Produkte in der Session gespeichert.</p>
<?php endif; ?> </body> </html>
Submitted by anonymous - 8 days ago