<?php
require_once ("class_sitzungsSeite.php");
class artikel extends sitzungsSeite
{
private $artikelArray = array();
private $DB = array('database' => 'u12345678',
'user' => 'abc',
'password' => 'geheim');
protected $dbh; # Database-Handle
public function __construct()
{
parent::__construct();
if(!is_array($this->DB) && empty($this->DB['database']))
throw new Exception("Daten für Datenbankverbindung fehlen!");
else
{
$connectString = "mysql:dbname=".$this->DB['database'].
";host=localhost";
try
{
$this->dbh = new PDO($connectString, $this->DB['user'],
$this->DB['password'] );
$this->dbh->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$sql = "SELECT a_artikelnr, a_name AS_veranstaltung, a_preis, a_menge
FROM ws_artikel
WHERE a_menge > 0";
$result = $this->dbh->query($sql);
$tmp = $result->fetchAll(PDO::FETCH_ASSOC);
foreach ($tmp as $key => $value)
{
foreach($value as $k1 => $v1)
{
$name = split("_", $k1);
if ($k1 != 'a_artikelnr')
$this->artikelArray[$value['a_artikelnr']][$name[1]] = $v1;
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
public function anzeigen()
{
print " <table cellpadding=\"5\">\n";
print " </tr>\n ";
#holen des numerischen Indizes
$tmp = array_keys($this->artikelArray);
#Durchsuchen nach den assoziativen Schlüsseln
foreach($this->artikelArray[$tmp[0]] as $key =>
$value)
{
#Ersten Buchstabn groß sezen
print "<th>".ucwords($key)."</th>";
}
print "\n</tr>\n";
#Alle Datenfelder ausgeben
foreach($this->artikelArray as $key => $value)
{
print "<tr style=\"text-align: center;\">\n";
foreach($value as $subKey => $subVal)
{
if ($subKey == "preis") #prüfen, um Euro-Text anzuhängen
print "<td align=\right\">".$subVal." Euro
</td>\n";
else
print "<td>".$subVal."</td>\n";
}
print "<td><a href=\"".$_SERVER['PHP_SELF']."?id=".
$key.
"\">In den Warenkorb</a></td>\n";
print " </tr>\n";
}
print "</table>\n";
}
public function waehlen($artikelnummer, $kunde)
{
if(!empty ($artikelnummer) && !empty($kunde))
{
try
{
$sql = "UPDATE ws_warenkorb
SET w_menge = w_menge + 1
WHERE w_artikelnr = :nummer
AND w_kunde = :kunde";
$result = $this->dbh->prepare($sql);
$result->bindParam(':nummer', $artikelnummer, PDO::PARAM_INT);
$result->bindParam(':kunde', $kunde, PDO::PARAM_INT);
$result->execute();
if ($result->rowCount() == 1)
return true;
$sql ="INSERT INTO ws_warenkorb
(w_artikelnr, w_kunde, w_menge)
VALUES (:nummer, :kunde, 1)";
$result = $this->dbh->prepare($sql);
$result->bindParam(':nummer', $artikelnummer, PDO::PARAM_INT);
$result->bindParam(':kunde', $kunde, PDO::PARAM_INT);
$result->execute();
if($result->rowCount()== 1)
{
print "Datensatz wurde eingetragen.";
return true;
}
return false;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}}
public function bestellen($kunde)
{
if(!is_integer($kunde)) # Gültige Kundennummer?
throw new Exception('Keine Kundennummer'); # Fehler ausgeben
else
{
try
{
#in Datenbank schreiben
$sql = "SELECT w_kunde, w_artikelnr, w_menge
FROM ws_warenkorb
WHERE w_kunde = :kunde";
$result = $this->dbh->prepare($sql);
$result->bindParam(':kunde', $kunde, PDO::PARAM_INT);
$result->execute();
$tmp = $result->fetchAll(PDO::FETCH_ASSOC);
foreach($tmp as $key => $value)
{
$sql = "INSERT INTO ws_bestellung (b_kunde,
b_artikelnr,
b_menge)
VALUES (:kunde, :artikel, :menge)";
$result = $this->dbh->prepare($sql);
$result->execute(array(':kunde' => (int)$value['w_kunde'],
':artikel' => (int)$value['w_artikelnr'],
':menge' => $value['w_menge']));
if($result->rowCount() == 1)
{
$sql1 = "UPDATE ws_artikel
SET a_menge = a_menge - :anzahl
WHERE a_artikelnr = :artikel";
$result1 = $this->dbh->prepare($sql1);
$result1->bindParam(':anzahl', $value['w_menge'],
PDO::PARAM_INT);
$result1->bindParam(':artikel', $value['w_artikelnr']);
$result1->execute();
if ($result1->rowCount() == 1)
{
$sql2 = "DELETE FROM ws_warenkorb
WHERE w_kunde = :kunde
AND w_artikelnr = :artikel";
$sql2 = $this->dbh->prepare($sql2);
$sql2->execute(array(':kunde' => (int)$kunde,
':artikel' => (int)$value['w_artikelnr']));
}
$error = true;
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
session_destroy();
return $error;
}
}
?>
#class webshop
<?php
require_once ("class_artikel.php");
require_once ("class_pdf.php");
require_once ("class_kunde.php");
class webshop extends artikel
{
private $pdf;
private $kunde;
public function __construct()
{
parent::__construct();
$this->pdf = new pdf();
$this->kunde = new kunde($this->dbh);
}
public function setKundenNummer($kennung, $passwort)
{
$this->KndNr = 1;
if(!empty($kennung) && !empty($passwort))
{
try
{
$sql = "SELECT k_kundennummer
From ws_kunde
WHERE k_kennung = :kennung
AND k_passwort = :passwort";
$result = $this->dbh->prepare($sql);
$result->bindParam(':kennung', $kennung,
PDO::PARAM_STR, 20);
$result->bindParam(':passwort', $passwort,
PDO::PARAM_STR, 32);
$result->execute();
$tmp = $result->fetchAll();
if(isset($tmp[0]['k_kundennummer']) &&
!empty($tmp[0]['k_kundennummer']))
{
$this->KndNr =
(integer)$tmp[0]['k_kundennummer'];
}
$_SESSION['kunde'] = $this->KndNr;
return $this->KndNr;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
{
return $this->kunde->setKundenNummer($kennung,
$passwort);
}}}
public function auswahl($kunde) # zeigt ausgewählte Artikel
{
$sql = "SELECT a_name, w_menge
FROM ws_warenkorb, ws_artikel
WHERE w_kunde = $kunde
AND w_artikelnr = a_artikelnr";
$result = $this->dbh->query($sql);
$tmp = $result->fetchAll(PDO::FETCH_ASSOC);
if (count($tmp) > 0)
{
print "<table>\n";
print "<tr>\n <th>Artikel</th><th>Anzahl</th>\n
</tr>\n";
foreach($tmp as $key)
{
print "<tr>\n";
print "<td>".$key['a_name'].
"</td><td>".$key['w_menge']."</td>\n";
print "</tr>\n";
}
print "</table>\n";
}
else
print "Keine Artikel im Warenkorb gefunden.";
}
private function setArtikel($daten)
{
$this->pdf->SetLeftMargin(35); # Setzt Rand
$this->pdf->Cell(30,5,"Artikelnummer",1,0,'C');
$this->pdf->Cell(60,5,"Artikel",1,0,'C');
$this->pdf->Cell(30,5,"Preis",1,0,'R');
foreach ($daten as $key => $value)
{
$this->pdf->ln();
$this->pdf->Cell(30,5,$key,1,0,'C');
$this->pdf->Cell(60,5,iconv('UTF-8', 'ISO-8859-15', $value['name']),1,0);
$this->pdf->Cell(30,5,$value['preis']." Euro",1,0,'R');
}
}
public function pdfliste()
{
$this->pdf->setTitel("Liste bestellbarer Artikel");
$this->pdf->AliasNbPages();
$this->pdf->AddPage();
$this->pdf->SetFont('Times','',12);
$text = "Sehr geehrter Kunde,\n\nvielen Dank, dass Sie sich".
" für unser Angebot interessieren. Folgende Artikel".
" können Sie in unserem Online-Shop (http://".$_SERVER['PHP_SELF'].") bestellen.\n";
$this->pdf->setText($text);
$this->setArtikel($this->artikelArray);
$text = "\n\nVielen Dank für Ihr Interesse.";
$this->pdf->setText($text);
$this->pdf->Output();
}
}
?>