cesta.php:
<?php
class cesta {
private $cesta = [];
function anyadir($producto,$precio){
$this->cesta[]=['producto'=>$producto,'precio'=>$precio];
}
function verCesta() {
?>
<table border="1">
<tr><td>Producto</td><td>Precio</td></tr>
<?php foreach ($this->cesta as $elemento) {
?>
<tr><td><?= $elemento['producto'] ?></td><td><?= $elemento['precio'] ?></td>
</tr> <?php } ?>
</table> <?php
}
}
index.php
<?php
include_once "cesta.php";
session_start();
if (!isset($_SESSION['cesta'])) {
$_SESSION['cesta'] = new cesta();
}
$producto = filter_input(INPUT_GET, 'producto');
$precio = filter_input(INPUT_GET, 'precio', FILTER_VALIDATE_INT);
if (!empty($producto) && !empty($precio)) {
$_SESSION['cesta']->anyadir($producto, $precio);
}
$_SESSION['cesta']->verCesta();
?>
<form>
<p>Producto:<input type="text" name="producto"></p>
<p>Precio:<input type="text" name="precio"></p>
<input type="submit">
</form>