Ver Mensaje Individual
  #5 (permalink)  
Antiguo 09/09/2007, 13:02
lisette
 
Fecha de Ingreso: septiembre-2007
Mensajes: 7
Antigüedad: 16 años, 8 meses
Puntos: 0
Re: Problema con cesta de la compra

A ver si se eexplicarlo por ejemplo yo pienso que lo mejor es como un diseño a tres capas a continuación te pongo la clase producto:

<?php

class Productes extends DB
{
var $ErrorMsg;
var $pro_codi;
var $pro_fam_codi;
var $pro_sub_codi;
var $pro_nom;
var $pro_descripcio;
var $pro_fotografia;
var $pro_preu1;
var $pro_preu2;
var $pro_preu3;
var $pro_oferta;
var $pro_novetat;
var $pro_pes;
function getError(){
return $this->ErrorMsg;
}

function setError($value){
$this->ErrorMsg=$value;
}

function getpro_codi(){
return $this->pro_codi;
}

function setpro_codi($value){
$this->pro_codi=intval($value);
}

function getpro_fam_codi(){
return $this->pro_fam_codi;
}

function setpro_fam_codi($value){
$this->pro_fam_codi=intval($value);
}
function getpro_sub_codi(){
return $this->pro_sub_codi;
}

function setpro_sub_codi($value){
$this->pro_sub_codi=intval($value);
}


function getpro_nom(){
return $this->pro_nom;
}

function setpro_nom($value){
$this->pro_nom=trim($value);
}
function getpro_descripcio(){
return $this->pro_descripcio;
}

function setpro_descripcio($value){
$this->pro_descripcio=trim($value);
}
function getpro_fotografia(){
return $this->pro_fotografia;
}

function setpro_fotografia($value){
$this->pro_fotografia=trim($value);
}
function getpro_preu1(){
return $this->pro_preu1;
}

function setpro_preu1($value){
$this->pro_preu1=doubleval($value);
}
function getpro_preu2(){
return $this->pro_preu2;
}

function setpro_preu2($value){
$this->pro_preu2=doubleval($value);
}
function getpro_preu3(){
return $this->pro_preu3;
}

function setpro_preu3($value){
$this->pro_preu3=doubleval($value);
}
function getpro_oferta(){
return $this->pro_oferta;
}

function setpro_oferta($value){
$this->pro_oferta=intval($value);
}
function getpro_novetat(){
return $this->pro_novetat;
}

function setpro_novetat($value){
$this->pro_novetat=intval($value);
}
function getpro_pes(){
return $this->pro_pes;
}

function setpro_pes($value){
$this->pro_pes=doubleval($value);
}





function ExistCodi($value,$taula = "productes"){
$this->setError("");
$s = "SELECT pro_codi FROM ".$taula ;
$s .= " WHERE pro_codi = ".intval($value) ;
$this->query($s);

$returnTrueFalse = false;
if ($this->resultExist()) {
$returnTrueFalse = true ;
$this->setError("El codigo de producto '$value' ya existe.");
}
return $returnTrueFalse;

}


function Select($WHERE){
$this->setError("");
$s = "SELECT pro_codi,pro_fam_codi,pro_sub_codi,pro_nom,pro_des cripcio,pro_preu1,pro_preu2,pro_preu3,pro_oferta,p ro_novetat,pro_pes";
$s .= " FROM productes " ;
$s .= $WHERE ;
$this->query($s);
if ($this->resultExist()) {
$returnTrueFalse = true ;
}
else {
$this->setError("Sin registros.");
$returnTrueFalse = false ;
}
return $returnTrueFalse ;
}

function Delete($value){
$this->setError("");
$s = "DELETE FROM productes WHERE" ;
$s .= " pro_codi = '".trim($value)."'";
//echo ($s);
$this->query($s) ;
if ( $this->hasErrors() ) {
$this->setError("Error al borrar producto:<br>".$s."<br>".$this->ShowErrors().".");
$returnTrueFalse = false;
}// if
else {
$returnTrueFalse = true;
} //else
return $returnTrueFalse;
}

function Insert($aValues,$taula = "productes") {
$this->setError("");
if (!$this->ExistCodi($aValues["pro_codi"],$taula) ){
$this->setpro_codi($aValues["pro_codi"]) ;
$this->setpro_fam_codi($aValues["pro_fam_codi"]) ;
$this->setpro_sub_codi($aValues["pro_sub_codi"]) ;
$this->setpro_nom($aValues["pro_nom"]) ;
$this->setpro_descripcio($aValues["pro_descripcio"]) ;
$this->setpro_fotografia($aValues["pro_fotografia"]) ;
$this->setpro_preu1($aValues["pro_preu1"]) ;
$this->setpro_preu2($aValues["pro_preu2"]) ;
$this->setpro_preu3($aValues["pro_preu3"]) ;
$this->setpro_oferta($aValues["pro_oferta"]) ;
$this->setpro_novetat($aValues["pro_novetat"]) ;
$this->setpro_pes($aValues["pro_pes"]) ;
$s = "INSERT INTO ".$taula." (" ;
$s .= " pro_codi " ;
$s .= ", pro_fam_codi " ;
$s .= ", pro_sub_codi ";
$s .= ", pro_nom " ;
$s .= ", pro_descripcio " ;
$s .= ", pro_fotografia " ;
$s .= ", pro_preu1 " ;
$s .= ", pro_preu2 " ;
$s .= ", pro_preu3 " ;
$s .= ", pro_oferta " ;
$s .= ", pro_novetat " ;
$s .= ", pro_pes " ;
$s .= " ) VALUES ( " ;
$s .= "'".$this->getpro_codi()."'" ;
$s .= ", ".$this->getpro_fam_codi() ;
$s .= ", ".$this->getpro_sub_codi();
$s .= ", '".$this->getpro_nom()."'" ;
$s .= ", '".$this->getpro_descripcio()."'";
$s .= ", '".$this->getpro_fotografia()."'";
$s .= ", ".$this->getpro_preu1();
$s .= ", ".$this->getpro_preu2();
$s .= ", ".$this->getpro_preu3();
$s .= ", ".$this->getpro_oferta();
$s .= ", ".$this->getpro_novetat();
$s .= ", ".$this->getpro_pes();
$s .= " )" ;
//echo($s."<br>") ;
$this->query($s) ;
if ( $this->hasErrors() ){
$this->setError("Error al insertar producto:<br>".$s."<br>".$this->err.".");
return false ;
}
return true ;
}//if (!$this->ExistCodi($ext_Codi) && !$this->ExistUsuariClau($ext_Clau,$ext_Usuari,$ext_Codi ))
return false ;
}

function Update($aValues){
$this->setError("");
//if ($this->ExistCodi($aValues["pro_codi"])){
$this->setpro_codi($aValues["pro_codi"]) ;
$this->setpro_fam_codi($aValues["pro_fam_codi"]) ;
$this->setpro_sub_codi($aValues["pro_sub_codi"]) ;
$this->setpro_nom($aValues["pro_nom"]) ;
$this->setpro_descripcio($aValues["pro_descripcio"]) ;
$this->setpro_fotografia($aValues["pro_fotografia"]) ;
$this->setpro_preu1($aValues["pro_preu1"]) ;
$this->setpro_preu2($aValues["pro_preu2"]) ;
$this->setpro_preu3($aValues["pro_preu3"]) ;
$this->setpro_oferta($aValues["pro_oferta"]) ;
$this->setpro_novetat($aValues["pro_novetat"]) ;
$this->setpro_pes($aValues["pro_pes"]) ;
$s = "UPDATE productes SET " ;
$s .= " pro_codi = ".$this->getpro_codi();
$s .= " pro_fam_codi = ".$this->getpro_fam_codi() ;
$s .= " pro_sub_codi = ".$this->getpro_sub_codi() ;
$s .= " pro_nom = '".$this->getpro_nom()."'" ;
$s .= " pro_descripcio = '".$this->getpro_descripcio()."'" ;
$s .= " pro_fotografia = '".$this->getpro_fotografia()."'" ;
$s .= " pro_preu1 = '".$this->getpro_preu1()."'" ;
$s .= " pro_preu2 = '".$this->getpro_preu2()."'" ;
$s .= " pro_preu3 = '".$this->getpro_preu3()."'" ;
$s .= " pro_oferta = '".$this->getpro_oferta()."'";
$s .= " pro_novetat = '".$this->getpro_novetat()."'" ;
$s .= " pro_pes = '".$this->getpro_pes()."'";
$s .= " WHERE pro_codi = ".$this->getpro_codi() ;//pongo solo pro_codi porque es la clave primaria
//echo($s) ;
$this->query($s) ;
if ( $this->hasErrors() ){
$this->setError("Error al Modificar producto:<br>".$s."<br>".$this->err.".");
return false ;
}
return true ;
//}//if ($this->ExistCodi($ext_Codi))
return false ;
}

function EsborrarTot($taula = "productes"){
$this->setError("");
$s = "DELETE FROM ".$taula ;
$this->query($s) ;
if ( $this->hasErrors() ) {
$this->setError("Error al borrar productos:<br>".$s."<br>".$this->ShowErrors().".");
$returnTrueFalse = false;
}// if
else {
$returnTrueFalse = true;
} //else
return $returnTrueFalse;
}

}
?>
Esta es la clase producto como ves la primera función es la si existe producto y luego cuando llames a insertar modificar o borar ya lo compruebas con le primer if.
Puedes llamarla desde otra página así es más fácil porque podrás realizar to´das estas acciones y es más ordenado y fácil