Ver Mensaje Individual
  #1 (permalink)  
Antiguo 18/06/2006, 16:38
tincho80
 
Fecha de Ingreso: junio-2006
Mensajes: 2
Antigüedad: 17 años, 11 meses
Puntos: 0
Sessiones y Objetos

Hola gente, a ver si alguien me puede ayudar:
Estoy queriendo armar un carrito simple, y el problema que tengo es que si uso un FORM no me conserva los valores del objeto. Paso los 2 archivos para que quede claro.

o si bien voy a otro archivo php tampoco me conserva los valores a pesar de hacer el include de lib_carrito y el new del objeto con el mismo nombre.

Gracias

lib_carrito.php

class carrito
{
public $items;

public function agregar_item($id, $cant){
$this->items[$id] += $cant;
}

public function retirar_item($id, $cant) {
if ($this->items[$id] > $cant) {
$this->items[$id] -= $cant;
return true;
} elseif ($this->items[$id] == $cant) {
unset($this->items[$id]);
return true;
} else {
return false;
}
}

public function imprimir_carrito(){
foreach($this->items as $id => $cant){
echo $id . ' ' . $cant . '<br>';
}
}

}
?>

carrito.php

<?
session_start();
include('lib_carrito.php');
$_SESSION['cart'] = new carrito();

if ($_POST['ok'] == 1)
$_SESSION['cart']->agregar_item($_POST['id'], $_POST['cant']);

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
</head>
<body>
<h1 align="center">CARRITO</h1>
<p align="center">
<? $_SESSION['cart']->imprimir_carrito(); ?>
</p>
<form id="form1" name="form1" method="post" action="">
ID:
<input name="id" type="text" id="id" />
CANTIDAD:
<input name="cant" type="text" id="cant" />
<input name="ok" type="hidden" value="1" />
<input type="submit" name="Submit" value="Agregar" />
</form>
<a href="ver.php">ver carrito</a>
</body>
</html>