Domanda

hello there people im kind of a newbie in php i was told to write a shopping cart system to add and remove items.i rote two classes one for each.the item class works well but the basket class keeps crashing at some point.here is the code:

<?php
class produit
{
private $nom;
private $prix;
private $id;
private $categorie;
private $qty;
public function setProduit($nom,$prix,$id,$cat)
{
    $this->nom=$nom;
    $this->prix=$prix;
    $this->id=$id;
    $this->categorie=$cat;
    $this->qty=1;
}
public function getNom()
{
    return $this->nom;
}
public function getPrix()
{
    return $this->prix;
}
public function getCategorie()
{
    return $this->categorie;
}
public function getID()
{
    return $this->id;
}
public function getQty()
{
    return $this->qty;
}
public function changeQty()
{
    $this->qty+=1;
}
public function getThumb()
{
    try
    {
        require"connectionbd.php";
        $req="select images from bs_produits where id=:i";
        $res=$bdd->prepare($req);
        $res->execute(array('i'=>$this->id));
        $v=$res->fetch(PDO::FETCH_OBJ);
        $v->images=str_replace("../","",$v->images);
        $v->images.="/thumb1.png";
        return $v->images;

    }
    catch(Exception $e)
      {
        die('Erreur : ' . $e->getMessage().' a la ligne '.$e->getLine().' dans le fichier '.$e->getFile());
      }
}
  }
 ?>

the following is the basket class code:

<?php
 @session_start();
 include"prduitclass.php";
 class panier
  {
//le panier a un tableau de 10 produits

public function setPanier()
{
    if(!isset($_SESSION['basket']))
        $_SESSION['basket']=array();

}
private function lookforduplicate( $prod)
{
    echo"<script>console.log('".var_dump($prod->getNom())."')</script>";
    $i=0;
    $exit=0;
    if(empty($_SESSION['basket']))
    {
        return $exit;
    }
    else
    {
        foreach($_SESSION['basket'] as $produit)
            {
                if($produit->getID()===$prod->getID())
                {
                    $_SESSION['basket'][$i]->changeQty();
                    $exit=1;
                }
                $i++;
            }
        }
    return $exit;
}
public function addProduit( $prod)
{

    $v=0;
    if(!$this->lookforduplicate($prod))
    {

        array_push($_SESSION['basket'], $prod);
        $v=1;
    }
    else
        $v=0;
    return $v;
}
public function removeProduit( $prod)
{
    $i=0;
    foreach($_SESSION['basket'] as $produit)
    {
        if($produit->getID()===$prod->getID())
        {
            unset($_SESSION['basket'][$i]);
        }
    }
}


 }

 ?>

finally heres the error i get from php:

Fatal error: panier::lookforduplicate(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "produit" of the object you are trying to operate on was loaded before unserialize() gets called or provide a __autoload() function to load the class definition in C:\wamp\www\bwanshop\panierclass.php on line 27

i do not know how to precise that my session should be a array of type "produit" the foreach loops in the basket class will give me an error please help me solve this riddle thank you

È stato utile?

Soluzione

Try putting the line including the class def before starting the session; maybe at session start, the session variables are deserialized and need the definition (error messaage sounds alike) - like this:

require_once "prduitclass.php";
session_start();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top