Cesta de la compra sesiones

funciones.php

function verCesta($cesta) {
 ?>
 <table border="1">
 <tr><td>Producto</td><td>Precio</td></tr>
 <?php foreach ($cesta as $elemento) {
 ?>
 <tr><td><?= $elemento['producto'] ?></td><td><?= $elemento['precio'] ?></td>
 </tr> <?php } ?>
 </table> <?php
}

index.php

<?php
 include_once "funciones.php";
 session_start();
 $producto = filter_input(INPUT_GET, 'producto');
 $precio = filter_input(INPUT_GET, 'precio');
 if (!empty($producto) && !empty($precio)) {
 $_SESSION['cesta'][] = ['producto' => $producto, 'precio' => $precio];
 }
 if (isset($_SESSION['cesta'])) {
 verCesta($_SESSION['cesta']);
 }
 ?>

<form>
 <p>Producto:<input type="text" name="producto"></p>
 <p>Precio:<input type="text" name="precio"></p>
 <input type="submit">
 </form>

Publicado por

Avatar del usuario

Juan Pablo Fuentes

Formador de programación y bases de datos