New $_SESSION data causing a glitch in removing action?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cyberen
Forum Newbie
Posts: 13
Joined: Wed Jan 19, 2011 4:32 pm

New $_SESSION data causing a glitch in removing action?

Post by Cyberen »

This is a little difficult to explain but hopefully someone here can point me to specific phrases that can help me figure out this problem.

The Problem:

When I remove an item from my shopping cart, the following code still shows up as the price information even though I wanted the item completely gone:

Code: Select all

 <span class="style1">Price:</span> $
      <? 
if($line['ceiling']=='Y' && $line['static']=='N'){
	
	$query = "SELECT * from ceiling where deleted=0 and pid=" . $items[0];
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$ceil_cost = mysql_fetch_array($result, MYSQL_ASSOC);
	$line['price']=$ceil_cost['h' . $ceiling[0]];
	mysql_free_result($result); 
}

echo number_format($line['price'],2);
?>
      Select one of the following options to modify this item<br />
      <br />
The Chain of Events:

When I remove an item, I press the following remove items button:

Code: Select all

<? echo '<a class="sub" href="viewcart5.php?back=' . urlencode($_GET['back']) . '&action=rem&id=' . $i . '"><img src="https://www.jezebelgallery.com/images/topbuttons/removefromcart.gif" alt="Remove" /></a> '; ?><br />
      <? 
which calls the remove action outlined below:

Code: Select all

for($i=0;$i<sizeof($_SESSION['cart']);$i++){
 if(($_GET['action']=='rem'||$_GET['action']=='rev') && $_GET['id']==$i){

  if($_GET['action']=='rev'){
   $val=$_SESSION['cart'][$i];
   $items= split("\|\|",$val);
  }
  
  $_SESSION['cart'][$i]=""; 
  if($_GET['action']=='rev'){
   header("Location: pro.php?id=" . $items[0]);
   exit;  
  }
 }
 $val=$_SESSION['cart'][$i];
 $items= split("\|\|",$val);
 if($items[0]!=""){ 
  $num_items++;
  
  	unset($_SESSION['shiplength'][$i]);
  	unset($_SESSION['shipwidth'][$i]);
	unset($_SESSION['shipheight'][$i]);
	unset($_SESSION['shipweight'][$i]);
	unset($_SESSION['shiplength2'][$i]);
	unset($_SESSION['shipwidth2'][$i]);
	unset($_SESSION['shipheight2'][$i]);
	unset($_SESSION['shipweight2'][$i]);
 }


}
you'll notice the eight unset lines below. That's because I suspect it's by adding values to the $_SESSION data that caused the removal of items to function incorrectly. The adding of $_SESSION values can be seen below.

Code: Select all

if (is_numeric(substr($itemshippinginfo[0]['length'],0,1)));{
		$_SESSION['shiplength'][$g]=$itemshippinginfo[0]['length'];
		$_SESSION['shipwidth'][$g]=$itemshippinginfo[0]['width'];
		$_SESSION['shipheight'][$g]=$itemshippinginfo[0]['height'];
		$_SESSION['shipweight'][$g]=$itemshippinginfo[0]['weight'];
	if (strlen($itemshippinginfo[2]['length'])>0) {
		$g++;
		$_SESSION['shiplength'][$g]=$itemshippinginfo[2]['length'];
		$_SESSION['shipwidth'][$g]=$itemshippinginfo[2]['width'];
		$_SESSION['shipheight'][$g]=$itemshippinginfo[2]['height'];
		$_SESSION['shipweight'][$g]=$itemshippinginfo[2]['weight'];
		}
		}
	}
}
The $g value counts how many packages total appear on the page, and $i counts how many items are on the page.

When I output an array onto the page that lists all the item data available, it says two items remain in the page and echoes their values.

THE QUESTIONS:

1. Did adding values to the $_SESSION data cause this incomplete deleting of items from the shopping cart?
2. Why does that price-related chunk of code remain but the rest disappears from view? The error also offsets the count of $g and $i.
3. If #1 is true, then is my unset code correct?
4. If #2 is not true, then is it something else in the code?

I appreciate the time it took you to read this and look forward to hearing any and all suggestions or solutions you have.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: New $_SESSION data causing a glitch in removing action?

Post by Jade »

Are you removing the item before you calculate the shopping cart total? If not then your item is still there when your total is created and displayed.
Cyberen
Forum Newbie
Posts: 13
Joined: Wed Jan 19, 2011 4:32 pm

Re: New $_SESSION data causing a glitch in removing action?

Post by Cyberen »

Thank you Jade for replying to my post. As to your question, the total price of all the items is calculated after the for loop that displays each item. Here is the entire loop in PHP form. The calculation total is on the bottom.
There was no problem before I added a shipping calculation chunk of code and $_SESSION values in the loop.

Code: Select all

  <td> Your cart contains
        <?= $num_items ?>
        item(s)<br /><br />
        <a href="#" onClick="history.go(-1)"><img src="images/topbuttons/shopping.gif" alt="Continue Shopping" /></a>
        <?PHP
$total=0;
for($i=0;$i<sizeof($_SESSION['cart']);$i++){

 $val=$_SESSION['cart'][$i];
 $items= split("\|\|",$val);
 if($items[0]!=""){ 
  $query = "SELECT *,category from products,categories where categories.id=products.pid and products.id=" . $items[0];
  $result = mysql_query($query) or die("Query failed : " . mysql_error());
  $line = mysql_fetch_array($result, MYSQL_ASSOC);
  //if (isset($line)){
  mysql_free_result($result);   
?>
<!-- I am unable to add a thumbnail to the product because the $_POST id for the thumbnails of each individual item is the same as the category's thumbnail, which means I have to edit the database in order to change that and I (nor CrystalTech I suspect) know how to alter a MYSQL database to that extent. -->
      <br />
      <hr />
      <span class="style1">
	 <!-- <img src="<?PHP //echo $line['thumbnail']?>" alt="product" /><br /> -->
        <?= stripslashes($line['category']).":";?>
        </span>
      <?= stripslashes($line['name']) ?>
      <? if($line['dims']!=''){ ?>
	  <br />
      <span class="style1">Dimensions:</span>
      <?=
stripslashes (substr(($line['dims']), 0, 70))?>
     <?
	 
 //if shipping weight is present then extract details
$itemshippinginfo = array();
$dims = $line['dims'];
//if shipcost present extract it
  $shipcostKV = strstr($dims,'shipcost=');
 $shipcost = substr($shipcostKV,9); // returns all but the keyword=
  $n = strpos($shipcost,' ');
  

 //$shipcost = substr($shipcost,0,$n); // convert shipping cost to session
 if ($shipcost!=''){
	 $c++."<br />";
	 echo "<br /><span class='style1'>Shipping Cost:</span> &#36;" .$shipcost;
	 $_SESSION['shipcost'][$c]=$shipcost;
	 //echo "<br />".$_SESSION['shipcost'][$c];
	}
if (!strstr($dims,'shipdims='))
	{
	echo "<br /> Shipping dimensions not yet provided.";
	}
else
{
	//echo "<br /> g is".
	$g++."<br />";
	$j=NULL;
	$itemdims = substr($line['dims'],strpos($line['dims'],'shipdims'.$j)+strlen('shipdims'.$j)+1,strpos($line['dims'],'shipweight'.$j)-strpos($line['dims'],'shipdims'.$j)-strlen('shipweight'.$j));
	
	if(strpos($line['dims'],"shipdims".($j+2)))
		{
			$itemweight = substr($line['dims'],strpos($line['dims'],'shipweight'.$j)+strlen('shipweight'.$j)+1,strpos($line['dims'],' ',strpos($line['dims'],'shipweight'.$j))-(strpos($line['dims'],'shipweight'.$j)+strlen('shipweight'.$j))-1);
		}
		else
		{
		//extract shipping dimensions and weight
			$itemweight = substr($line['dims'],strpos($line['dims'],'shipweight'.$j)+strlen('shipweight'.$j)+1);
		}
	$itemshippinginfo[0]['length'] = substr($itemdims,0,strpos(strtolower($itemdims),'x')); 
	$itemshippinginfo[0]['width'] = substr($itemdims,strpos(strtolower($itemdims),'x')+1,strpos(strtolower($itemdims),'x',strpos(strtolower($itemdims),'x')+1)-strpos(strtolower($itemdims),'x')-1);
	$itemshippinginfo[0]['height'] = substr($itemdims,strpos(strtolower($itemdims),'x',strpos(strtolower($itemdims),'x')+1)+1);
	$itemshippinginfo[0]['weight'] = $itemweight;
	
	$j=2;
	while(strpos($line['dims'],"shipdims".$j))
		{
			$itemdims = substr($line['dims'],strpos($line['dims'],'shipdims'.$j)+strlen('shipdims'.$j)+1,strpos($line['dims'],'shipweight'.$j)-strpos($line['dims'],'shipdims'.$j)-strlen('shipweight'.$j));
				if(strpos($line['dims'],"shipdims".($j+1)))
				{
					$itemweight = substr($line['dims'],strpos($line['dims'],'shipweight'.$j)+strlen('shipweight'.$j)+1,strpos($line['dims'],' ',strpos($line['dims'],'shipweight'.$j))-(strpos($line['dims'],'shipweight'.$j)+strlen('shipweight'.$j))-1);
				}
				else
				{
					$itemweight = substr($line['dims'],strpos($line['dims'],'shipweight'.$j)+strlen('shipweight'.$j)+1);
				}
			$itemshippinginfo[$j]['length'] = substr($itemdims,0,strpos(strtolower($itemdims),'x')); 
			$itemshippinginfo[$j]['width'] = substr($itemdims,strpos(strtolower($itemdims),'x')+1,strpos(strtolower($itemdims),'x',strpos(strtolower($itemdims),'x')+1)-strpos(strtolower($itemdims),'x')-1);
			$itemshippinginfo[$j]['height'] = substr($itemdims,strpos(strtolower($itemdims),'x',strpos(strtolower($itemdims),'x')+1)+1);
			$itemshippinginfo[$j]['weight'] = $itemweight;
			$j++;
		}
		if(is_numeric($itemshippinginfo[0]['length'])) {
		echo "<br /><span class='style1'>Shipping Dimensions: </span>".$itemshippinginfo[0]['length']."" Long, ".$itemshippinginfo[0]['width']."" Wide, ".$itemshippinginfo[0]['height']."" High. Weight of package, ".$itemshippinginfo[0]['weight']." pounds.";
				if($itemshippinginfo[2]['length']!=''){
				echo "<br /><span class='style1'>Shipping Dimensions of Second Box: </span>".$itemshippinginfo[2]['length']."" Long, ".$itemshippinginfo[2]['width']."" Wide, ".$itemshippinginfo[2]['height']."" High. Weight of package, ".$itemshippinginfo[2]['weight']." pounds.";
				}
			;}
	//echo "<br /><br />";
		//$numbcheck = substr($itemdims, 0, 1);
		/*if(is_numeric($numbcheck)){
		echo $itemdims."<br /><br />";
	var_export($itemshippinginfo);}
	else{echo $shipcost;}
	 */
	 //echo $i;
	//echo $j."<br />";
	if (is_numeric(substr($itemshippinginfo[0]['length'],0,1)));{
		$_SESSION['shiplength'][$g]=$itemshippinginfo[0]['length'];
		$_SESSION['shipwidth'][$g]=$itemshippinginfo[0]['width'];
		$_SESSION['shipheight'][$g]=$itemshippinginfo[0]['height'];
		$_SESSION['shipweight'][$g]=$itemshippinginfo[0]['weight'];
	if (strlen($itemshippinginfo[2]['length'])>0) {
		$g++;
		$_SESSION['shiplength'][$g]=$itemshippinginfo[2]['length'];
		$_SESSION['shipwidth'][$g]=$itemshippinginfo[2]['width'];
		$_SESSION['shipheight'][$g]=$itemshippinginfo[2]['height'];
		$_SESSION['shipweight'][$g]=$itemshippinginfo[2]['weight'];
		}
		}
	}
}
	?>
	  <br />
      <span class="style1">Glass Color:</span>
      <?= stripslashes($items[1]) ?>
      <?}?>
      <? if($items[2]!=''){ ?>
      <br />
      <span class="style1">Finish:</span>
      <?= stripslashes($items[2]) ?>
      <?}?>
      <? if($items[3]!=''){ ?>
      <br />
      <span class="style1">Stocks:</span>
      <?= stripslashes($items[3]) ?>
      <?}?>
      <? if($items[4]!=''){ ?>
      <br />
      <span class="style1">Cord Color:</span>
      <?= stripslashes($items[4]) ?>
      <?}?>
      <? if($items[5]!=''){ 
 $ceiling= split("\|",stripslashes($items[5]));
?>
      <br />
      <span class="style1">Ceiling Height:</span>
      <?= $ceiling[0] ?>
      '
      <?}?>
      <? if($items[6]!=''){ 
?>
      <br />
      <span class="style1">Glass Shape:</span>
      <?= $items[6] ?>
      <?}?>
      <? if($items[7]!=''){ 
?>
      <br />
      <span class="style1">Leaf Option:</span>
      <?= $items[7] ?>
      <?}?>
      <? if($items[8]!=''){ ?>
      <br />
      <span class="style1">Ceiling Cap:</span>
      <?=  stripslashes($items[8]) ?>
      <?}?>
      <br />
      <span class="style1">Price:</span> $
      <? 
if($line['ceiling']=='Y' && $line['static']=='N'){
	
	$query = "SELECT * from ceiling where deleted=0 and pid=" . $items[0];
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$ceil_cost = mysql_fetch_array($result, MYSQL_ASSOC);
	$line['price']=$ceil_cost['h' . $ceiling[0]];
	mysql_free_result($result); 
}

echo number_format($line['price'],2);
?>
      Select one of the following options to modify this item<br />
      <br />
      <? echo '<a class="sub" href="viewcart5.php?back=' . urlencode($_GET['back']) . '&action=rem&id=' . $i . '"><img src="https://www.jezebelgallery.com/images/topbuttons/removefromcart.gif" alt="Remove" /></a> '; ?><br />
      <? 
  $total+=$line['price'];
 }
//}
?>    

    <td>      <a href="<?= $_GET['back']?>"><img src="images/topbuttons/shopping.gif" alt="Continue Shopping" /></a><br /></td>
  </tr>
  <tr>
    <td align="right" class="style1"><br />
      Cart subtotal:<br />
      $
      <?= number_format($total,2)?>
      USD</td>
Any insight would be appreciated!
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: New $_SESSION data causing a glitch in removing action?

Post by Jade »

If you do a print_r() on $_SESSION['cart'] do the items you removed still show up in the list?
Cyberen
Forum Newbie
Posts: 13
Joined: Wed Jan 19, 2011 4:32 pm

Re: New $_SESSION data causing a glitch in removing action?

Post by Cyberen »

I typed in a print_r($_SESSION); at the bottom of the page and this is the result. Even when I try to remove a product the array stays the same. I think it has something to do with how removing an item means going back in the browser and manipulating $_GET. Interesting how the shipping dimensions are in a section of their own.

[text]Array ( [cart] => Array ( [0] => 34||Azalea||Black||Round|||||||||| [1] => 36||Azalea||Black||Round|||||||||| )

[numberofitems] => 2

[itemdata] => Array (


[0] => Array (


[id] => 4
[pid] => 4
[name] => Bouquet Floor Lamp
[bulb] => 2 x (60 watt bulbs) included
[price] => 1090
[retail] => 1090
[redirect] => Description of the Product.
[deleted] => 0
[ceiling] => N
[dims] => Measurements: 62\"H x 16\"W x 16\"D shipdims=28X28X20 shipweight=15 shipdims2=60X20X20 shipweight2=40
[static] => N
[thumbnail] => /images/t_floorlamps.jpg
[addon] => 0
[category] => Floor Lamps
)


[1] => Array (

[id] => 4
[pid] => 4
[name] => Vaso Floor Lamp
[bulb] => 2 x (60 watt bulbs) included
[price] => 1090
[retail] => 1090
[redirect] => Description of the Vaso Floor Lamp.
[deleted] => 0
[ceiling] => N
[dims] => Measurements: 62\"H x 23\" round shipdims=28X28X20 shipweight=15 shipdims2=72X12X12 shipweight2=40
[static] => N
[thumbnail] => /images/t_floorlamps.jpg
[addon] => 0
[category] => Floor Lamps
)

)


[shiplength] => Array ( [1] => 28 [2] => 60 [3] => 28 [4] => 72 )
[shipwidth] => Array ( [1] => 28 [2] => 20 [3] => 28 [4] => 12 )
[shipheight] => Array ( [1] => 20 [2] => 20 [3] => 20 [4] => 12 )
[shipweight] => Array ( [1] => 15 [2] => 40 [3] => 15 [4] => 40 )

[productcostnummers] => 0 [packagenummers] => 4 ) [/text]


The "continue shopping" button also is glitchy; it sends the browser back a page for just a split-second and then forward to the current one again. Would altering this code into a javascript back function alter the functionality of the removal of items? Here's the code, existing right above the $total+=$line['price']; code:

Code: Select all

 <? echo '<a class="sub" href="viewcart5.php?back=' . urlencode($_GET['back']) . '&action=rem&id=' . $i . '"><img src="https://www.jezebelgallery.com/images/topbuttons/removefromcart.gif" alt="Remove" /></a> '; ?><br />
      <? 
Thanks again Jade for reading!
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: New $_SESSION data causing a glitch in removing action?

Post by Jade »

You're never removing the item:

Code: Select all

unset($_SESSION['shiplength'][$i]);
        unset($_SESSION['shipwidth'][$i]);
        unset($_SESSION['shipheight'][$i]);
        unset($_SESSION['shipweight'][$i]);
        unset($_SESSION['shiplength2'][$i]);
        unset($_SESSION['shipwidth2'][$i]);
        unset($_SESSION['shipheight2'][$i]);
        unset($_SESSION['shipweight2'][$i]);
        unset($_SESSION['itemdata'][$i]); //remove the item info not just the shipping info
Cyberen
Forum Newbie
Posts: 13
Joined: Wed Jan 19, 2011 4:32 pm

Re: New $_SESSION data causing a glitch in removing action?

Post by Cyberen »

Thanks for your suggestion, Jade, but it doesn't seem to work out.

The removal function for items is:

Code: Select all

<? echo '<a class="sub" href="viewcart5.php?back=' . urlencode($_GET['back']) . '&action=rem&id=' . $i . '"><img src="https://www.jezebelgallery.com/images/topbuttons/removefromcart.gif" alt="Remove" /></a> '; ?>
And the rem function it calls is in here I think:

Code: Select all

for($i=0;$i<sizeof($_SESSION['cart']);$i++){
 if(($_GET['action']=='rem'||$_GET['action']=='rev') && $_GET['id']==$i){

  if($_GET['action']=='rev'){
   $val=$_SESSION['cart'][$i];
   $items= split("\|\|",$val);
  }
  
  $_SESSION['cart'][$i]=""; 
  if($_GET['action']=='rev'){
   header("Location: pro.php?id=" . $items[0]);
   exit;  
  }
 }
 $val=$_SESSION['cart'][$i];
 $items= split("\|\|",$val);
 if($items[0]!=""){ 
  $num_items++;
  
  unset($_SESSION['shiplength'][$i]);
  	unset($_SESSION['shipwidth'][$i]);
	unset($_SESSION['shipheight'][$i]);
	unset($_SESSION['shipweight'][$i]);
	unset($_SESSION['shiplength2'][$i]);
	unset($_SESSION['shipwidth2'][$i]);
	unset($_SESSION['shipheight2'][$i]);
	unset($_SESSION['shipweight2'][$i]);
	unset($_SESSION['itemdata'][$i]);
 }


}
Did I add the removal functions wrong or something?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: New $_SESSION data causing a glitch in removing action?

Post by Jade »

It looks like php has a problem with unset and arrays. You'll need to re-create the array or do an array splice to remove the value. See the unset manual page: http://php.net/manual/en/function.unset.php
Cyberen
Forum Newbie
Posts: 13
Joined: Wed Jan 19, 2011 4:32 pm

Re: New $_SESSION data causing a glitch in removing action?

Post by Cyberen »

Thanks for the suggestions, Jade, but I already tried the unset function for all those values like you suggested and there was no improvement. Unless you think the problem is how the array was set up in the first place, all I really want is for the removal of items to work correctly. I will look into how to split arrays and unset values in the meanwhile. If you have any more suggestions I would be glad to read them.
Post Reply