tooted=array(); $this->id=$id; $this->andmefail=$andmefail; $this->loeTooted(); $this->loeKogusedSessioonist(); $this->tootleURL(); $this->salvestaSessiooni(); } function sessiooniMuutujaNimi(){ return $this->id."_kogused"; } function lisamisParameetriNimi(){ return $this->id."_lisa"; } function eemaldamisParameetriNimi(){ return $this->id."_eemalda"; } function loeTooted(){ $f=fopen($this->andmefail, "r"); while($rida=fgets($f, 100)){ $m=explode(",", $rida); array_push($this->tooted, array($m[0], floatval($m[1]), 0)); } } function kysiTooted(){ return $this->tooted; } function loeKogusedSessioonist(){ if(isSet($_SESSION[$this->sessiooniMuutujaNimi()])){ $m=explode(",", $_SESSION[$this->sessiooniMuutujaNimi()]); for($i=0; $itooted[$i][2]=intval($m[$i]); } } } function tootleURL(){ if(isSet($_REQUEST[$this->lisamisParameetriNimi()])){ $this->tooted[intval($_REQUEST[$this->lisamisParameetriNimi()])][2]++; } if(isSet($_REQUEST[$this->eemaldamisParameetriNimi()])){ if($this->tooted[intval($_REQUEST[$this->eemaldamisParameetriNimi()])][2]>0){ $this->tooted[intval($_REQUEST[$this->eemaldamisParameetriNimi()])][2]--; } } } function salvestaSessiooni(){ $abi=array(); foreach($this->tooted as $toode){ array_push($abi, $toode[2]); } $_SESSION[$this->sessiooniMuutujaNimi()]=join(",", $abi); } function teave(){ return "Ostuandmete klass"; } function toodeteLoetelu(){ $t=""; $nr=0; foreach($this->tooted as $toode){ $t.="\n"; $nr++; } $t.="
".htmlspecialchars($toode[0])." $toode[1]$toode[2] + -
"; return $t; } function toodeteLoeteluSmarty($smarty){ $smarty->assign("tooted", $this->tooted); $smarty->display("toodeteleht.tpl"); } function toodeteSumma(){ $sum=0; foreach($this->tooted as $toode){ $sum+=$toode[1]*$toode[2]; } return $sum; } } require("libs/Smarty.class.php"); $smarty=new Smarty; $smarty->debugging=true; $korv1=new Ostukorv("esimene", "http://localhost/kursus/algus/tooted2.txt"); $korv1->toodeteLoeteluSmarty($smarty); echo "Kokku: ".$korv1->toodeteSumma();