Page 1 of 1

question about a class from phpclasses.org

Posted: Wed Jul 02, 2003 2:43 pm
by RobbertvanOs
Hi there, i lost track on a project of mine. I tried to build a shop according to the merchant class that's on phpclasses.org. I followed the directions how to build it for php 4 and up, but didn't succeed. When i add a product to the basket, it works and when i add the same product again, it doesn't anymore. Because it just gives a blank cart, although there are items in the basket, because if i add another product to the basket it shows up again and with the product 1 submitted twice. Could any please help me out, why this happends? I used the basket class before, but saw the ammendments to this class and i wanted to use those "feautures". I do want to have a "cookie-less" cart system, so that's why i had to rewrite some code. Perhaps something went wrong in there? I someone could help me out that would be great!

Thanx in advance and sorry for the big post,

Kind regards Robbert

Code: Select all

<?php
<?

error_reporting(0);

require("merchantfront.php");

// session_name("mysession");
// session_start();
// if (!session_is_registered("basket")) {
// 	$basket=new Basket;
// 	session_register("basket");
// }

session_name("mysession");
session_start();
if (! $_SESSION['basket']) {
	$_SESSION['basket']=new Basket;
}


// Perform Cart Actions
if (isset($A))
{

if ($A=="Remove")
   {
   //$basket->Del_Item($P);
   $_SESSION['basket']->Del_Item($P);
   }
}


if ($A=="Add") {

$myid="";
$myquant="";
$zpos="";

  if ($_SESSION['basket']->Get_Basket_Count()>0) {  # are there items in the basket
	$pos = $_SESSION['basket']->Enum_Items(true);
	while ($pos>=0) {



           if ($_SESSION['basket']->Get_Item_ID($pos)==$itemid)
              {
	          $temp = $_SESSION['basket']->Get_Item_Quantity($pos);    
              $_SESSION['basket']->Set_Item_Quantity($pos,($temp+1));
              $addprod="no";
              }


           $myid=$_SESSION['basket']->Get_Item_ID($pos);
           $myquant=$_SESSION['basket']->Get_Item_Quantity($pos);
           $zpos=$pos;

           $pos = $_SESSION['basket']->Enum_Items();
	              }
}

if ($addprod!="no")
              {
              $_SESSION['basket']->Add_Item($itemid,"George Foreman Grill",1,19.95,"YadaYada",".0675","black","XXL","1");
              }
                            }
// $basket->Set_Item_Quantity(0,2);
if ($A=="Empty") {
$_SESSION['basket']->Empty_Basket();
}
// End Cart Actions
?>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Shop</title>
<style>
<!--
.netscape6problem		{ font-family: Verdana,Arial,Helvetica; font-size: 10px; }
.smallnormal			{ font-family: Verdana,Arial,Helvetica; font-size: 10px; }
.smallnormaltable		{ font-family: Verdana,Arial,Helvetica; font-size: 10px; color: black; }
.smallnormaltablealt		{ font-family: Verdana,Arial,Helvetica; font-size: 10px; color: red; }
.mednormal				{ font-family: Verdana,Arial,Helvetica; font-size: 12px }
.mednormaltable		{ font-family: Verdana,Arial,Helvetica; font-size: 12px; color: black; }
.mednormalhighlight		{ font-family: Verdana,Arial,Helvetica; font-size: 12px; color: black; background: yellow; }
.medbold				{ font-family: Verdana,Arial,Helvetica; font-size: 12px; font-weight: bold }
.medboldalt				{ font-family: Verdana,Arial,Helvetica; font-size: 12px; font-weight: bold; color: red; }
.medboldtable			{ font-family: Verdana,Arial,Helvetica; font-size: 12px; font-weight: bold; color: black; }
.largebold				{ font-family: Verdana,Arial,Helvetica; font-size: 18px; font-weight: bold }
.smallcompact			{ font-family: Verdana,Arial,Helvetica; font-size: 10px }
.smallbold				{ font-family: Verdana,Arial,Helvetica; font-size: 10px; font-weight: bold }
.smallboldtable			{ font-family: Verdana,Arial,Helvetica; font-size: 10px; font-weight: bold; color: black; }
.smallboldtablealt		{ font-family: Verdana,Arial,Helvetica; font-size: 10px; font-weight: bold; color: #99CCFF; }
.smallboldtabletop		{ font-family: Verdana,Arial,Helvetica; font-size: 10px; font-weight: bold; color: black; }
.smallboldtemplate		{ font-family: Verdana,Arial,Helvetica; font-size: 10px; font-weight: bold; color: black;}
.smallboldfooter			{ font-family: Verdana,Arial,Helvetica; font-size: 10px; color: #999999;}
.locationbar				{ font-family: Verdana,Arial,Helvetica; font-size: 10px; color: black; }

A:link					{text-decoration: underline; color: black; }
A:visited				{text-decoration: underline; color: black; }
A:hover					{text-decoration: underline; color: red; }

A.minibasket:link		{text-decoration: underline; color: blue; }
A.minibasket:visited		{text-decoration: underline; color: blue; }
A.minibasket:hover		{text-decoration: underline; color: red; }

A.table:link				{text-decoration: underline; color: blue; }
A.table:visited			{text-decoration: underline; color: blue; }
A.table:hover			{text-decoration: underline; color: red; }

A.templatemenu:link			{text-decoration: none; color: white; }
A.templatemenu:visited		{text-decoration: none; color: white; }
A.templatemenu:hover		{text-decoration: underline; color: red; background: #FFDD00; }


A.locationlink:link		{text-decoration: underline; color: black; }
A.locationlink:visited		{text-decoration: underline; color: black; }
A.locationlink:hover		{text-decoration: underline; color: red; }

table.minibasket			{background-color: white; }
tr.minibasketline 		{background-color: #EEEEEE; }

hr		{ height: 1; color: #DDDDDD }
pre		{ font-family: Courier; font-size: 10px; }
select	{ font-family: Verdana; font-size: 10px; color: black; background-color: white; border-color: #DDDDDD; }

table.mainpage				{background-color: white; }
tr.mainpagetableline 			{background-color: #99CCFF; }
tr.mainpagetableline2 		{background-color: #99CCFF; }
tr.mainpagetablespecial		{background-color: #DDDDDD; }
tr.mainpageversionhighlight	{background-color: #FFFF00; }

.buttonstyle	{font-family: Verdana; font-size: 10px; font-weight: bold; color: #666666; background-color: #DDDDDD; border-style: outset; border-color: #999999; border-width: 1px }
-->
</style>
</head>
<body>


<table class="mainpage" cellSpacing="0" cellPadding="5" width="100%" border="0">
  <tbody>
    <tr>
      <td>
        <div align="center">
          <center>
        <table cellSpacing="0" cellPadding="1" width="75%" border="0">
          <tbody>
            <tr>
              <td width="95"></td>
              <td width="252"></td>
              <td class="smallboldtabletop" align="right" width="66">price ($)</td>
              <td class="smallboldtabletop" align="right" width="59">
                <p align="center">&nbsp;&nbsp;&nbsp; qty</p>
              </td>
              <td width="57"></td>
            </tr>
<form method="post" action="shop.php?A=Update">
            <?
            $count=0;
            $myid="";
            $myquantity="";
            if ($_SESSION['basket']->Get_Basket_Count()>0) {  # are there items in the basket
	$pos = $_SESSION['basket']->Enum_Items(true);
	
	while ($pos>=0) {
	    // Update Cart Quantities
         if (isset($A)) {
          if ($A=="Update")
		     {
		     $myvalue=$howmany[$pos];
		     $_SESSION['basket']->Set_Item_Quantity($pos,$howmany[$pos]);
		     }
                     }
			
           $mytext="<tr class='mainpagetableline2'>";
	       $mytext.="<td class='medboldtable' colSpan='6' width='614'> ".$_SESSION['basket']->Get_Item_Name($pos)." - ".$_SESSION['basket']->Get_Item_Color($pos)." - ".$_SESSION['basket']->Get_Item_Size($pos)."</td>";
	        $mytext.="</tr><tr>";
	        $mytext.="<td class='smallnormaltable' vAlign='center' width='95'><a href='product.php?product=WHATEVER&V=27&ph=basket'>See Item</a></td>";
            $mytext.="<td class='smallnormaltable' vAlign='center' align='left' width='252'>".$_SESSION['basket']->Get_Item_ID($pos)."</td>";
           $mytext.="<td class='smallboldtable' vAlign='center' align='right' width='66'>".$_SESSION['basket']->Get_Item_Price($pos)."</td>";
           $mytext.="<td vAlign='center' align='right' width='59'><input size='3' value='".$_SESSION['basket']->Get_Item_Quantity($pos)."' name='howmany[$pos]'></td>";
           $mytext.="<td class='smallnormaltable' vAlign='center' align='right' width='57'><a href='shop.php?A=Remove&P=".$pos."'>Remove</a></td>";
           $price=$_SESSION['basket']->Get_Item_Price($pos);
           $quantity=$_SESSION['basket']->Get_Item_Quantity($pos);
           $total=number_format(($total + ($price*$quantity)), 2, '.', '');
          print $mytext;
		$pos = $_SESSION['basket']->Enum_Items();
	}
}

           if (!isset($pos)) { ?>
           <align="center">No Items in Cart</align>
           <? }

?>


          </tbody>
        </table><BR>
              <table width="75%">
              <TR>
              <td class="smallboldtabletop" align="right" width="100%">Total($): $<? print $total; ?></td>
            </tr>
            </table><BR>
        <input type="submit" name="submit" value="Update Quantities">
<form><BR><BR>
<a href="shop.php?A=Add&itemid=FORE2">Add Product - FORE 2</a>
<BR><BR>
<a href="shop.php?A=Add&itemid=FORE1">Add Product - FORE 1</a>
<BR><BR>
<a href="shop.php?A=Add&itemid=FORE3">Add Product - FORE 3</a>
<BR><BR>
<a href="shop.php?A=Empty">Empty Cart</a>
<BR><BR>
<?PHP echo $current; ?>
          </center>
        </div>
      </td>
    </tr>
  </tbody>
</table>
<p align="center">&nbsp;</p>
</body>

</html>
?>

Code: Select all

<?
class Basket {
        var $basket_count;
        var $basket_item_id;
        var $basket_item_name;
        var $basket_item_quantity;
        var $basket_item_data;
        var $basket_item_price;
        var $basket_item_tax;
        var $basket_item_color;
        var $basket_item_size;

        function Basket() {
                $this->basket_count=0;
        }
        function Add_Item($ID,$name,$quantity=1,$price=0,$data='',$tax,$color,$size,$weight) {

                $this->basket_item_id[$this->basket_count]=$ID;
                $this->basket_item_name[$this->basket_count]=$name;
                $this->basket_item_quantity[$this->basket_count]=$quantity;
                $this->basket_item_data[$this->basket_count]=$data;
                $this->basket_item_price[$this->basket_count]=$price;
                $this->basket_item_tax[$this->basket_count]=$tax;
                $this->basket_item_color[$this->basket_count]=$color;
                $this->basket_item_size[$this->basket_count]=$size;
                $this->basket_item_weight[$this->basket_count]=$weight;

                $this->basket_count++;
                return ($this->basket_count-1);
        }
        function Del_Item($pos) {
                $this->basket_item_id[$pos]='';
        }
        function Get_Item_ID($pos) {
                return $this->basket_item_id[$pos];
        }
        function Get_Item_Name($pos) {
                return $this->basket_item_name[$pos];
        }
        function Get_Item_Price($pos) {
                return $this->basket_item_price[$pos];
        }
        function Get_Item_Quantity($pos) {
                return $this->basket_item_quantity[$pos];
        }
        function Get_Item_Data($pos) {
                return $this->basket_item_data[$pos];
        }
        function Get_Item_Tax($pos) {
		                return $this->basket_item_tax[$pos];
        }
        function Get_Item_Color($pos) {
		                return $this->basket_item_color[$pos];
        }
        function Get_Item_Size($pos) {
		                return $this->basket_item_size[$pos];
        }
        function Get_Item_Weight($pos) {
				                return $this->basket_item_weight[$pos];
        }
        function Set_Item_Quantity($pos,$quantity) {
                $this->basket_item_quantity[$pos]=$quantity;
        }
        function Set_Item_Data($pos,$data) {
                $this->basket_item_data[$pos]=$data;
        }
        function Enum_Items($start=false) {
                static $current;
                if ($current>=$this->basket_count) return -1;
                if (!$start) {
                        $current++;
                } else {
                        $current=0;
                }
                while (($this->basket_item_id[$current]=='') && ($current<$this->basket_count)) {
                        $current++;
                }
                return ($current<$this->basket_count) ? $current : -1;
        }
        function Empty_Basket() {
                $this->basket_count=0;
        }
        function Get_Basket_Count() {
            $num=0;
            for ($i=0;$i<$this->basket_count;$i++) {
                        if ($this->basket_item_id[$i]!='') $num++;
            }
            return $num;
        }
}
?>
?>