warenkorb xml teil 2
Hier kommt mein Code
Index.php
<?php
include("artikelliste.php");
$art = new Artikelparser($datei);
$Artikel = array();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- @(#) $Id$ -->
<head>
<title>HTML Template</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Generator" content="Dev-PHP 2.3.0" />
<meta name="Keywords" content="your,keywords,here" />
<meta name="Description" content="." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="stdStyle.css" />
<style type = text/css>
.nummer{
text-align:right;
width:40px;
}
.name{
text-align:left;
width:200px;
}
.anzahl{
text-align:right;
width:40px
}
.preis{
text-align:right;
width:70px;
}
</style>
</head>
<body>
<h1>Folgende Bücher können Sie bestellen</h1>
<table>
<tr>
<th class='nummer'>ArtNr</th>
<th class='name'>Name</th>
<th class='anzahl'>Anzahl</th>
<th class='preis'>Preis</th>
</tr>
<?php
foreach($art->art as $elemente){
$Artikel = $elemente->Ausgabe();
echo "<tr>\n";
}
foreach($Artikel as $feld=>$wert){
echo"<td class='".$feld."'>".$wert."</td>\n";
}
echo"</tr>\n";
}
?>
</table>
</body>
</html>
artikel.inc
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- @(#) $Id$ -->
<head>
<title>HTML Template</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Generator" content="Dev-PHP 2.3.0" />
<meta name="Keywords" content="your,keywords,here" />
<meta name="Description" content="." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="stdStyle.css" />
</head>
<body>
<?php
class Artikel
{
var $nummer;
var $name;
var $anzahl;
var $preis;
function Artikel($nummer,$name,$preis)
{
$this->nummer=$nummer;
$this->name=$name;
$this->anzahl=1;
$this->preis=$preis;
}
function Ausgabe()
{
$arArtikel=array();
$arArtikel=[nummer]=$this->nummer;
$arArtikel=[name]=$this->name;
$arArtikel=[anzahl]=$this->anzahl;
$arArtikel=[preis]=$this->preis;
return $arArtikel;
}
}
?>
</body>
</html>
artikeldaten.xml
<?xml version="1.0" encoding="windows-1250"?>
<artikeldaten>
<artikel nummer="1001">
<name>Der Herr der Ringe</name>
<preis>39.90</preis>
</artikel>
<artikel nummer="2002">
<name>Der kleine Hobbit</name>
<preis>19.90</preis>
</artikel>
<artikel nummer="3003">
<name>Grimms Märchen</name>
<preis>25.90</preis>
</artikel>
<artikel nummer="4004">
<name>Elric von Melnibonee</name>
<preis>29.90</preis>
</artikel>
</artikeldaten>
artikelliste.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Unbenanntes Dokument</title>
</head>
<body>
<?php
include_once("artikel.inc");
class Artikelparser{
var $nummer;
var $art;
var $aktuell;
var $titel;
var $preis;
function Artikelparser($datei)
{
$this->art=Array();
$xmlFile=implode("",file("artikeldaten.xml"));
$parser=xml_parser_create();
xml_set_object($parser,$this);
xml_set_element_handler($parser,"startElement","endElement");
xml_set_character_data_handler($parser,"cdata");
xml_parse($parser,$xmlFile);
xml_parser_free($parser);
}
function startElement($parser,$name,$atts){
$this->aktuell=$name;
if($name=="ARTIKEL"){
$this->nummer=$atts[NUMMER];
}
}
function endElement($parser,$name){
if($name=="ARTIKEL"){
$this->art[$this->nummer]=new Artikel($this->nummer,$this->titel,$this->preis);
}
$this->aktuell="";
}
function cdata($parser,$text){
if($this->aktuell=="PREIS")$this->preis=$text;
if($this->aktuell=="NAME")$this->titel=$text;
}
function getArtikelArray(){
return$this->art;
}
}
?>
</body>
</html>
bestellung.php
<?php include ("warenkorb.inc");
// Start Session, falls nicht schon passiert
session_start();
// Hole Warenkorb-Objekt aus der Session
$korb = $_SESSION['korb'];
// Falls es der erste Aufruf ist, muss zuerst ein neuer Warenkorb angelegt werden
if(!is_object($korb)){
$korb = new Bestellung();
$_SESSION['korb'] = $korb;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- @(#) $Id$ -->
<head>
<title>HTML Template</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Generator" content="Dev-PHP 2.3.0" />
<meta name="Keywords" content="your,keywords,here" />
<meta name="Description" content="." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="stdStyle.css" />
</head>
<body>
<?php
//Handle erstellen
$datei = fopen("bestellung.xml","w");
// Startzeile schreiben
fputs($datei, "<bestellung>\n");
// Artikel aus Array in xml-Datei schreiben
foreach ($korb->inhalt as $artikel=>$anzahl){
fputs($datei, " <artikel nummer=' ".$artikel." ' anzahl=' ".$anzahl." '/>\n");
}
// Endzeile schreiben
fputs($datei, "</bestellung>");
// Datei schließen
fclose($datei);
// Meldung ausgeben
print "<b>Die Datei 'bestellung.xml' wurde gespeichert!</b><br><br>";
print "<a href='index.php'><b>Ausloggen</b></a>";
// Warenkorb nach Bestellung leeren
$korb = array();
?>
</body>
</html>
bestellung.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<bestellung>
<artikel nummer="1001" anzahl="3"/>
<artikel nummer="3003" anzahl="1"/>
</bestellung>
warenkorb.inc
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- @(#) $Id$ -->
<head>
<title>HTML Template</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Generator" content="Dev-PHP 2.3.0" />
<meta name="Keywords" content="your,keywords,here" />
<meta name="Description" content="." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="stdStyle.css" />
</head>
<body>
<?php
class Warenkorb{
var $inhalt = array();
function hinzufuegen($artikel){
$this->Inhalt[$artikel]++;
}
function entfernen($artikel){
unset($this->inhalt[$artikel]);
}
funtion leeren(){
$this->inhalt = Array();
}
}
?>
</body>
</html>
warenkorb.php
<?php
include("warenkorb.inc");
// starte Session,falls nicht schon passiert
session_start();
//Hole Warenkorb-Objekt aus der Session
$korb = $_SESSION['korb'];
// Falls es der erste Aufruf ist, muss zuerst
//ein neuer Warenkorb angelegt werden
if (!is_object($korb)){
$korb = new Warenkorb();
$_SESSION['korb'] = $korb;
}
//Artikel hinzufügen?
if (isset($artikel)) $korb->hinzufuegen($artikel);
//Artikel entfernen?
if (isset($entf)) $korb->entfernen($entf);
//Gesamten Warenkorb löschen?
if (isset($loeschen)) $korb->leeren();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Unbenanntes Dokument</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Warenkorb</h1>
<a href="warenkorb.php?loeschen=1">
Warenkorb löschen
</a><p>
<a href="javascript:history.back()">Zurück</a><p>
Sie haben bisher folgende Artikel ausgewählt:<p>
<?php
if (empty($korb)){
//zeige alle bisher ausgewählten Artikel an
foreach ($korb->inhalt as $artikel=>$anzahl){
print "Artikelnummer: $artikel ";
print"<a href='warenkorb.php?entf=$artikel'>";
print "Artikel entfernen</a><br>";
print "Anzahl: $anzahl<br>";
if ($artikel == 1001)
print "Der Herr der Ringe<p>";
if ($artikel == 2002)
print "Der kleine Hobbit<p>";
if ($artikel == 3003)
print "Grimms Märchen<p>";
if ($artikel == 4004)
print "Elric von Melniboee<p>";
}
}
else {
print "Bisher nichts ausgewählt";
}
?>
</body>
</html>
So das war der Code ich hoffe ihr könnt damit etwas anfangen und mir helfen.
DANKE in vorraus.
|