Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created October 25, 2022 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/2ddeb50185db5be2ea0c1ce318fb8b00 to your computer and use it in GitHub Desktop.
Save parzibyte/2ddeb50185db5be2ea0c1ce318fb8b00 to your computer and use it in GitHub Desktop.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nombres = $_POST["nombres"];
$marcas = $_POST["marcas"];
$precios = $_POST["precios"];
$subtotal = 0;
for ($i = 0; $i < count($nombres); $i++) {
$nombre = $nombres[$i];
$marca = $marcas[$i];
$precio = floatval($precios[$i]);
$subtotal += $precio;
printf("%s ($%s) marca %s<br>", $nombre, number_format($precio, 2), $marca);
}
$porcentajeIva = 0.16;
$iva = $subtotal * $porcentajeIva;
$total = $subtotal + $iva;
echo "<br><strong>Subtotal: </strong>$" . number_format($subtotal, 2) . "<br>";
echo "<strong>IVA: </strong>$" . number_format($iva, 2) . "<br>";
echo "<strong>TOTAL: </strong>$" . number_format($total, 2) . "<br>";
echo "<br><a href=''>Volver al formulario</a>";
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Productos</title>
</head>
<body>
<form action="" method="post">
<?php
$cantidadProductos = 5;
?>
<?php for ($i = 0; $i < 5; $i++) { ?>
<p>Producto <?php echo $i + 1 ?></p>
<label for="">Nombre: </label>
<input type="text" name="nombres[]" required>
<label for="">Marca:</label>
<select name="marcas[]" required>
<option selected value="Marca 1">Marca 1</option>
<option value="Marca 2">Marca 2</option>
<option value="Marca 3">Marca 3</option>
</select>
<label for="">Precio:</label>
<input type="number" name="precios[]" required>
<br>
<?php } ?>
<br>
<input type="submit">
<input type="reset">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment