questo è il codice di un progetto che ho fatto tempo fa....era per una videoteca online
film.class.php
Codice PHP:
<?php
class Film
{
private $id_film;
private $titolo;
private $regista;
private $anno;
private $genere;
private $durata;
private $prezzo;
private $n_copie;
public function __construct() {}
//set get id_film
public function setIDfilm($id)
{
$this->id_film=$id;
}
public function getIDfilm()
{
return $this->id_film;
}
//set get titolo
public function setTitolo($tit)
{
if ($tit=="") {
$this->titolo="undefined";
}
else $this->titolo=$tit;
}
public function getTitolo()
{
return $this->titolo;
}
//set get regista
public function setRegista($reg)
{
if ($reg=="") {
$this->regista="undefined";
}
else $this->regista = $reg;
}
public function getRegista ()
{
return $this->regista;
}
//set get anno
public function setAnno($year)
{
if ($year=="") {
$this->anno="undefined";
}
else $this->anno=$year;
}
public function getAnno()
{
return $this->anno;
}
//set get genere
public function setGenere($tipologia)
{
if ($tipologia=="") {
$this->genere="undefined";
}
else $this->genere=$tipologia;
}
public function getGenere()
{
return $this->genere;
}
//set get durata
public function setDurata($min)
{
if ($min=="") {
$this->durata="undefined";
}
else $this->durata=$min;
}
public function getDurata()
{
return $this->durata;
}
//set e get copie
public function setCopie($copie)
{
if ($copies=="") {
$this->n_copie="no quantit‡";
}
else $this->n_copie=$copie;
}
public function getCopies()
{
return $this->n_copie;
}
//set e get prezzo
public function setPrezzo($eur)
{
$this->prezzo=$eur;
}
public function getPrezzo()
{
return $this->prezzo;
}
}
?>
cart.class.php
Codice PHP:
<?php
//inclusione della classe film
require_once('film.class.php');
class cart {
private $mov = array();
private $qt = array();
private $numeroarticoli;
public function __construct() {
$this->numeroarticoli = 0;
}
public function getNumeroarticoli() { return $this->numeroarticoli; }
public function getMov() { return $this->mov; }
public function getQt() { return $this->qt; }
public function add_film($mov) {
//Ottengo la lunghezza dell'array
$position=count($this->mov);
$trovato=0;
for ($i=0;$i<count($this->mov);$i++) {
//Verifico se il prodotto è presente nel carrello
if ($this->mov[$i]==$mov) $trovato=1;
}
//Se il prodotto è già presente aggiorno quantità e variante
if ($trovato==1) $this->aggiungi_copia($mov);
else {//altrimenti aggiungo il prodotto al carrello
$this->mov[$position]=$mov;
$this->qt[$position]=1;
$this->numeroarticoli = $this->numeroarticoli + 1;
}
}
public function aggiungi_copia($mov) {
$position = -1;
for ($i=0;$i<count($this->mov);$i++) {
//Prelevo la posizione del prodotto nell'array
if ($this->mov[$i]==$mov) $position=$i;
}
if ($position==-1) echo "Impossibile aggiornare il prodotto,prodotto non trovato!
";
else {
//Aggiorno le informazioni del prodotto
$this->qt[$position]++;
$this->numeroarticoli = $this->numeroarticoli + 1;
}
}
public function delete_film($mov) {
$position = -1;
for ($i=0;$i<count($this->mov);$i++) {
//Prelevo la posizione del prodotto nell'array
if ($this->mov[$i]==$mov) $position=$i;
}
if ($position!=-1) {
$this->numeroarticoli = $this->numeroarticoli -($this->qt[$position]);
$cont=0;
for ($i=0;$i<count($this->mov);$i++) {
if ($this->mov[$i]!=$mov) {
//creo 2 nuovi array in cui andro a copiare $mov, e $qt tutti gli indici meno l'indice da eliminare
$app_mov[$cont]=$this->mov[$i];
$app_qt[$cont]=$this->qt[$i];
$cont++;
}
}
//rimuovo il contenuto degli array
unset($this->mov);
unset($this->qt);
for ($i=0;$i<count($app_mov);$i++) {
//riverso il contenuto degli array sopra creati in $mov e $qt
$this->mov[$i]=$app_mov[$i];
$this->qt[$i]=$app_qt[$i];
}
}
else echo "Impossibile cancellare il prodotto,prodotto non trovato!
";
}
}
?>
mostra_carrello.php
Codice PHP:
<?php
//avvio della sessione
session_start();
//inclusione file impostazione smarty e creazione oggetto smarty
require('setup.php');
$smarty = new Smarty_OMR;
require_once ('blowfish.php');
require_once('film.class.php');
require_once('cart.class.php');
//inclusione del file redirect
require('redirect.php');
if (isset($_SESSION['spesa'])){
// trasformazione della stringa variabile di sessione nell'oggetto carrello
$car = unserialize($_SESSION['spesa']);
if ($car->getNumeroarticoli()==0) {
$smarty->display('index_header.tpl');
$smarty->display('index_left.tpl');
$smarty->display('carrellovuoto.tpl');
if(isset($_COOKIE['login'])) {
$user = $_COOKIE['login'];
$key="OMR";
$username = PMA_blowfish_decrypt($user, $key);
$smarty->assign ('usernameonline', $username);
$smarty->caching = false;
$smarty->display('index_right_login.tpl');
}
else {
$smarty->display('index_right.tpl');
}
$smarty->display('index_bottom.tpl');
}
else {
$smarty->assign('numeroarticoli',$car->getNumeroarticoli());
$f=$car->getMov(); //contiene l'array dei film memorizzati nel carrello e quindi un array di oggetti di classe film
$qta=$car->getQt(); //contiene l'array relativo alle quantita di ogni film
$numerocopie = 0;
$totaledapagare = 0;
$numerofilm = 0;
foreach ($f as $j => $value ) { //j contiene l'indice della variabile corrente mentre value contiene il valore della variabile corrente
$id_film[]= $value->getIDfilm();
$titolo[] = $value->getTitolo();
$regista[] = $value->getRegista();
$genere[] = $value->getGenere();
$anno[] = $value->getAnno();
$prezzo[] = $value->getPrezzo();
$durata[] = $value->getDurata();
$copie[]=$qta[$j];
$totprezzo[]=$qta[$j]*$value->getPrezzo();
$totaledapagare = $totaledapagare + ($qta[$j]*$value->getPrezzo());
$numerocopie = $numerocopie + ($qta[$j]);
$numerofilm = $numerofilm + 1;
$smarty->assign('id_film',$id_film);
$smarty->assign('titolo',$titolo);
$smarty->assign('genere',$genere);
$smarty->assign('anno',$anno);
$smarty->assign('prezzo',$prezzo);
$smarty->assign('totaledapagare',$totaledapagare);
$smarty->assign('durata',$durata);
$smarty->assign('regista',$regista);
$smarty->assign('copie',$copie);
$smarty->assign('totprezzo',$totprezzo);
$smarty->assign('numerocopie',$numerocopie);
$smarty->assign('numerofilm',$numerofilm);
}
$smarty->display('index_header.tpl');
$smarty->display('index_left.tpl');
$smarty->caching = false;
$smarty->display('carrello.tpl');
if(isset($_COOKIE['login'])) {
$user = $_COOKIE['login'];
$key="OMR";
$username = PMA_blowfish_decrypt($user, $key);
$smarty->assign ('usernameonline', $username);
$smarty->caching = false;
$smarty->display('index_right_login.tpl');
}
else {
$smarty->display('index_right.tpl');
}
$smarty->display('index_bottom.tpl');
}
}
else {
$smarty->display('index_header.tpl');
$smarty->display('index_left.tpl');
$smarty->display('carrellovuoto.tpl');
if(isset($_COOKIE['login'])) {
$user = $_COOKIE['login'];
$key="OMR";
$username = PMA_blowfish_decrypt($user, $key);
$smarty->assign ('usernameonline', $username);
$smarty->caching = false;
$smarty->display('index_right_login.tpl');
}
else {
$smarty->display('index_right.tpl');
}
$smarty->display('index_bottom.tpl');
}
?>
in questo progetto avevo fatto uso di un motore per la gestione dei template (SMARTY) e di una libreria per la criptazione dei dati (BLOWFISH), quindi se non ti è chiaro tutto quello che c'è nel file mostra_carrello.php è normale