Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Used to be able to remove items, now I can't. What's the $_SESSION doing wrong?

extract session

  • Please log in to reply
No replies to this topic

#1 Cyberen

Cyberen

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 04 February 2011 - 03:09 PM

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 Way things used to work before I meddled with it:

When a person used the remove item from cart button, it worked and even if you printed the array for the items in the page, it showed the right amount of items.

What Changed:

New calculations, values of variables and $_SESSION values in this part of the item information. All this is new, and notice the $_SESSION at the bottom:

<?

//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> $" .$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'];
}
}
}
}
?>


The Problem Now:

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. This is the part that is still shown as visible, when I do a print function the item is still in the array:

<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 removal of item buttons has this code:

<? 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 function outlined below:

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]);
}


}


I added the unset values above in a vain attempt to fix the problem. $i is the number representing the item in the list, $g is a variable that initially equals 0 that represents the amount of packages on the page which I added.

If you're curious what the entire loop looks like, it's like this. I added my chunk of code below the stripslashes line for dims or dimensions.

<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> $" .$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>


If I add two items and take one away using the removal item button, it still shows up in the array when I print it. Notice how the dimensions for the packages are on the bottom instead of with the item. Is this the source of the problem?

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 )


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?
  • 0





Also tagged with one or more of these keywords: extract, session

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download