note 129808 added to language.functions

From: Date: Fri, 18 Oct 2024 08:09:01 +0000
Subject: note 129808 added to language.functions
Groups: php.notes 
Request: Send a blank email to php-notes+get-221597@lists.php.net to get a copy of this message
<?php // Inicializamos el array para almacenar todas las reservas $reservasCompletas = array(); // Verificamos si el formulario se ha enviado if ($_SERVER["REQUEST_METHOD"] == "POST") { // Recuperamos los datos del formulario $hab = $_POST["habitacion"]; $nochesReservadas = (int)$_POST["noches"]; $fechaReserva = $_POST["fecha"]; // Crear un array de reserva individual $reserva = [ 'habitacion' => $hab, 'noches' => $nochesReservadas, 'fecha' => $fechaReserva, ]; // Si ya hay reservas previas, las recuperamos y reconstruimos los arrays if (!empty($_POST['reservas_previas'])) { $reservasPrevias = explode(",", $_POST['reservas_previas']); foreach ($reservasPrevias as $reservaStr) { // Convertimos cada reserva de cadena a array asociativo $reservaArray = explode("|", $reservaStr); $reservasCompletas[] = [ 'habitacion' => $reservaArray[0], 'noches' => $reservaArray[1], 'fecha' => $reservaArray[2], ]; } } // Añadimos la nueva reserva $reservasCompletas[] = $reserva; } ?> <!DOCTYPE html> <html lang="es"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Reserva de Hotel</title> </head> <body> <h1>Reserva de Habitación</h1> <form action="" method="post"> <!-- Menú desplegable para seleccionar tipo de habitación --> <label for="habitacion">Tipo de habitación: </label> <select name="habitacion" id="habitacion" required> <option value="Sencilla">Sencilla</option> <option value="Doble">Doble</option> <option value="Suite">Suite</option> </select> <!-- Campo para la cantidad de noches --> <label for="noches">Número de noches: </label> <input type="number" id="noches" name="noches" min="1" step="1" value="1" required> <!-- Campo para la fecha de llegada --> <label for="fecha">Fecha de llegada: </label> <input type="date" id="fecha" name="fecha" required> <!-- Campo oculto para almacenar las reservas previas, usamos implode para convertir el array en cadena --> <input type="hidden" name="reservas_previas" value='<?php // Convertimos las reservas en cadenas usando | para separar valores y , para separar reservas $reservasStr = array_map(function($reserva) { return implode("|", $reserva); }, $reservasCompletas); echo implode(",", $reservasStr); ?>'> <!-- Botón para enviar el formulario --> <button type="submit">Agregar reserva</button> </form> <h2>Resumen de la reserva</h2> <?php if (!empty($reservasCompletas)): ?> <?php foreach ($reservasCompletas as $reserva): ?> <p> Habitación: <?= $reserva['habitacion'] ?> <br> Noches: <?= $reserva['noches'] ?> <br> Fecha de llegada: <?= $reserva['fecha'] ?> <br> </p> <?php endforeach; ?> <?php else: ?> <p>No hay reservas realizadas aún.</p> <?php endif; ?> </body> </html> ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 193.146.182.133) ---- Manual Page -- https://php.net/manual/en/language.functions.php Edit -- https://main.php.net/note/edit/129808 Del: integrated -- https://main.php.net/note/delete/129808/integrated Del: useless -- https://main.php.net/note/delete/129808/useless Del: bad code -- https://main.php.net/note/delete/129808/bad+code Del: spam -- https://main.php.net/note/delete/129808/spam Del: non-english -- https://main.php.net/note/delete/129808/non-english Del: in docs -- https://main.php.net/note/delete/129808/in+docs Del: other reasons-- https://main.php.net/note/delete/129808 Reject -- https://main.php.net/note/reject/129808 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221597) next »