Ver Mensaje Individual
  #1 (permalink)  
Antiguo 13/10/2010, 05:44
surux
 
Fecha de Ingreso: octubre-2010
Mensajes: 9
Antigüedad: 13 años, 7 meses
Puntos: 0
Ayuda con carro compra online con PHP

Hola, estoy implementando una tienda online con php. He seguido varios manuales y libros para poder realizarlo de forma autonoma. Me encuentro con un problema con el carro de la compra y su implementacion. No estoy acostumbrado en el uso del lenguaje php en relacion a objetos y una vez añadido el producto no se sacar la informacion de este.

Muchas gracias de antemano!!

Shoppingcart.php
Código PHP:
<?php
class shoppingcart {
    private 
$cart;
    function 
__construct(){
        
$this->cart = array();
    }
    public function 
additem($id$name$cost) {
        foreach(
$this->cart as $key=>$items) {
            if(
$items['id'] == $id) {
                
$this->cart[$key]['quantity']++;
                return;
            }
        }
        
$this->cart[] = array('id' => $id
        
'name' => $name
        
'cost' => $cost
        
'quantity' => 1);
    }
    public function 
dellitem($id) {
        foreach(
$this->cart as $key=>$items) {
            if(
$items['id'] == $id) {
                if(
$items['quantity'] > 1) {
                    
$this->cart[$key]['quantity']--;
                } else {
                    unset(
$this->cart[$key]);
                }
                return 
true;
            }
        }
        return 
false;
    }
    public function 
getcart() {
        return 
$this->cart;
    }
    public function 
getitem() {
        return 
$this->item['id'];
    }
    public function 
clearcart() {
        
$this->cart = array();
    }
}
?>
carrocompra.php
Código PHP:
<?php
require_once("shoppingcart.php");
session_start();

    
//$nom = consultarnom();
    
$id $_POST["id"];
    
$nom $_POST["nom"];
    
$preu $_POST["preu"];
    
$stock $_POST["stock"];
    
$quantitat $_POST["quantitat"];
    
$contador=0;
    
//if(!isset($_SESSION['cart']) || !is_object($_SESSION['cart'])) {
        
$_SESSION['cart'] = new shoppingcart();
        
//Agegir un producte al carro(item 43, preu=49.99
        
while($contador<$quantitat && $stock!=0){
        
$_SESSION['cart']->additem($id$nom$preu);
        
$contador=$contador+1;
        
$stock=$stock-1;
         
        }        
        echo 
'No hi ha stock';
        
$items $_SESSION['cart']->getCart();
        echo 
$_SESSION['cart']->getCart();


    
//}
?>


<table width="300" border="1">
  <tr>
    <td width="219">Producte</td>
    <td width="65">Cost</td>
  </tr>
  <tr>
    <td><?php echo $nom ?></td>
    <td><?php echo $preu ?></td>
  </tr>
</table>

Última edición por surux; 13/10/2010 a las 06:12