<?
//______________________________________________________________________
//
// OOP Warenkorb || [ c] 2006 Andulus
//______________________________________________________________________
class cart
{
var $aid;
var $anzahl;
function insert($id, $zahl) {
session_start();
$this->aid = $id;
$this->anzahl = $zahl;
$_SESSION['cart'][] = $this->aid;
$_SESSION['anzahl'][] = $this->anzahl;
header("LOCATION: ".$_SERVER['PHP_SELF']."?aktion=show&sid=".session_id()."");
}
function show() {
session_start();
$count = count($_SESSION['cart']);
if($count > 0) {
for($i = 0; $i < $count; $i++) {
echo "AID: ".$_SESSION['cart'][$i]." | ";
echo "ANZAHL: ".$_SESSION['anzahl'][$i]." | ";
echo "<a href='?aktion=loeschen&arraynr=".$i."&sid=".session_id()."'>";
echo "loeschen</a> | ";
echo "<a href='?aktion=edit&arraynr=".$i."&ok=false&sid=".session_id()."'>";
echo "aendern</a><br></n>";
}
echo "<a href='?aktion=leeren&sid=".session_id()."'>Warenkorb leeren</a><br>\n";
echo "<a href='?aktion=artikel&sid=".session_id()."'>Weiter Shoppen</a><br>\n";
} else {
echo "Ihr Warenkorb ist leer!";
} }
function delete($count) {
session_start();
unset($_SESSION['cart'][$count]);
unset($_SESSION['anzahl'][$count]);
header("LOCATION: ".$_SERVER['PHP_SELF']."?aktion=show&sid=".session_id()."");
}
function destroy() {
session_start();
session_unset($_SESSION['cart']);
session_destroy();
header("LOCATION: ".$_SERVER['PHP_SELF']."?aktion=show&sid=".session_id()."");
}
function artikel() {
$i = 0;
while($i < 3) {
$i++;
echo "AID: ".$i."<br><form action='?aktion=insert&aid=".$i."&sid=".session_id()."' method='post'>";
echo "<input type='text' name='zahl' value='1'>";
echo "<input type=submit value='in den warenkorb legen!'></form><br>\n";
}
}
function aendern() {
session_start();
$nr = $_GET['arraynr'];
$aid = $_SESSION['cart'][$nr];
echo "AID: ".$aid."<br><form action='?aktion=edit&arraynr=".$nr."&ok=true&sid=".session_id()."' method='post'>";
echo "<input type='text' name='zahl'><input type=submit value='aendern!'></form><br>\n";
if($_GET['ok']=='true') {
$_SESSION['anzahl'][$nr] = $_POST['zahl'];
header("LOCATION: ".$_SERVER['PHP_SELF']."?aktion=show&sid=".session_id()."");
}
}
}
$cart = new cart;
$action = $_GET['aktion'];
switch($action) {
//______________________________________________________________________
//
// aktion = artikel
//______________________________________________________________________
case '': $cart->artikel(); break;
case 'artikel': $cart->artikel(); break;
//______________________________________________________________________
//
// aktion = show
//______________________________________________________________________
case 'show': $cart->show(); break;
//______________________________________________________________________
//
// aktion = insert
//______________________________________________________________________
case 'insert': $cart->insert($_GET['aid'], $_POST['zahl']); break;
//______________________________________________________________________
//
// aktion = loeschen
//______________________________________________________________________
case 'loeschen': $cart->delete($_GET['arraynr']); break;
//______________________________________________________________________
//
// aktion = leeren
//______________________________________________________________________
case 'leeren': $cart->destroy(); break;
//______________________________________________________________________
//
// aktion = edit
//______________________________________________________________________
case 'edit': $cart->aendern(); break;
}
?>