Chcialbym dac wam pod ostrzal moja druga klasunie. Tym razem ma to byc koszyk zakupow.
Dane koszyka trzymane sa w tablicy z $_SESSION['koszyk'][ID_ELEMENTU] a wartosc poszczegolnej komorki okresla ZAMOWIONA_ILOSC. Np.
<?php
$_SESSION['koszyk'][1] = 20;
$_SESSION['koszyk'][2] = 10;
?>
Nie wiedzialem za bardzo jak rozwiazac kwestie pobierania danych o towarach bedacych w koszyku wiec getData() i polaczenie ze SQL w niej jest tymczasowe

.
Oto moja klasa:
<?php
class Basket
{
public $basket;
public $sum;
public $countItems;
public $basketHandler;
public function Basket()
{
$this->__construct();
}
public function __construct()
{
$this->basketHandler = &$_SESSION['koszyk'];
}
public function addItem($itemID, $itemAmount)
{
if (isset($this->basketHandler[$itemID])) $this->basketHandler[$itemID] += $itemAmount;
else
$this->basketHandler[$itemID] = $itemAmount;
}
public function getData()
{
$this->sum = 0;
if (count($this->basketHandler) > 0
) foreach ($this->basketHandler as $index => $value)
$arrayTemp[] = $index;
if (count($arrayTemp) > 0
) {
$query = "SELECT id, nazwa, cena, vat FROM produkty WHERE id IN (".implode(", ", $arrayTemp).")";
{
$i = 0;
{
$this->basket[$i]['id'] = $record[0];
$this->basket[$i]['name'] = $record[1];
$this->basket[$i]['amount'] = $this->basketHandler[$record[0]];
$this->basket[$i]['vat'] = $record[3];
$this->basket[$i]['grossPrice'] = number_format(($this->basket[$i]['netPrice'] + $this->basket[$i]['netPrice'] * $this->basket[$i]['vat'] / 100
), 2
);
$this->basket[$i]['netValue'] = number_format(($this->basket[$i]['netPrice'] * $this->basket[$i]['amount']), 2
);
$this->basket[$i]['grossValue'] = number_format(($this->basket[$i]['grossPrice'] * $this->basket[$i]['amount']), 2
);
$this->sum += $this->basket[$i]['grossValue'];
$i++;
}
$this->countItems = $i;
}
}
}
public function showBasket()
{
if ($this->countItems() > 0)
{
$return = "<table border="1">n";
$return .= "<tr><td>Nazwa:</td><td>Cena netto:</td><td>VAT:</td><td>Cena brutto:</td><td>Ilosc:</td><td>Wartosc netto:</td><td>Wartosc brutto:</td></tr>n";
foreach ($this->basket as $index => $value)
$return .= "<tr><td>".$this->basket[$index]['name']."</td><td>".$this->basket[$index]['netPrice']."</td><td>".$this->basket[$index]['vat']."</td><td>".$this->basket[$index]['grossPrice']."</td><td>".$this->basket[$index]['amount']."</td><td>".$this->basket[$index]['netValue']."</td><td>".$this->basket[$index]['grossValue']."</td></tr>n";
$return .= "<tr><td colspan="6" style="text-align: right;">Do zaplaty:</td><td>".$this->sum."</td></tr>n";
$return .= "</table>";
return $return;
}
}
public function countItems()
{
$return = (isset($this->countItems)) ?
$this->countItems : count($this->basketHandler);
return $return;
}
public function getSum()
{
$this->getData();
return $this->sum;
}
public function emptyBasket()
{
foreach ($this->basketHandler as $index => $value)
unset($this->basketHandler[$index]); }
}
?>
Prosze o porady i sugestie co w niej zmienic, co dopisac itp. Z gory serdeczne dzieki!