Muchas gracias de antemano!!
Shoppingcart.php
Código PHP:
<?php
class shoppingcart {
private $cart;
function __construct(){
$this->cart = array();
}
public function additem($id, $name, $cost) {
foreach($this->cart as $key=>$items) {
if($items['id'] == $id) {
$this->cart[$key]['quantity']++;
return;
}
}
$this->cart[] = array('id' => $id,
'name' => $name,
'cost' => $cost,
'quantity' => 1);
}
public function dellitem($id) {
foreach($this->cart as $key=>$items) {
if($items['id'] == $id) {
if($items['quantity'] > 1) {
$this->cart[$key]['quantity']--;
} else {
unset($this->cart[$key]);
}
return true;
}
}
return false;
}
public function getcart() {
return $this->cart;
}
public function getitem() {
return $this->item['id'];
}
public function clearcart() {
$this->cart = array();
}
}
?>
Código PHP:
<?php
require_once("shoppingcart.php");
session_start();
//$nom = consultarnom();
$id = $_POST["id"];
$nom = $_POST["nom"];
$preu = $_POST["preu"];
$stock = $_POST["stock"];
$quantitat = $_POST["quantitat"];
$contador=0;
//if(!isset($_SESSION['cart']) || !is_object($_SESSION['cart'])) {
$_SESSION['cart'] = new shoppingcart();
//Agegir un producte al carro(item 43, preu=49.99
while($contador<$quantitat && $stock!=0){
$_SESSION['cart']->additem($id, $nom, $preu);
$contador=$contador+1;
$stock=$stock-1;
}
echo 'No hi ha stock';
$items = $_SESSION['cart']->getCart();
echo $_SESSION['cart']->getCart();
//}
?>
<table width="300" border="1">
<tr>
<td width="219">Producte</td>
<td width="65">Cost</td>
</tr>
<tr>
<td><?php echo $nom ?></td>
<td><?php echo $preu ?></td>
</tr>
</table>