help in shopping cart! :(

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
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

help in shopping cart! :(

Post by lmh85 »

i know shopping cart shd sounds early for experts but to a noob like me!! it do sounds easy at first but when i started 'coding' i'm struck!! anybody there can lend a helping hand?

The below coding is a part thats called when you click on the update button of the shopping cart.. i'm using cookie for this time.. any other better way in doing please do let me know! and oh ya.. there's error on the part where the condition checks on the 0 that the user entered in for the qty. If the user entered in 0 or checked on the remove checkboxes, it will then take the cookie out and move the cookie below up(its in array).

i simply can't get it right!! its up at my neck now!! anyone help?!?! :(:(:(

Code: Select all

if(isset($_POST['btnUpdate'])){
$intCount=1;
$intQty=0;
$intNum="0";
$intCountZero=0;
$intCountItem=0;
//check if there's 0;
	foreach ($_COOKIE['id'] as $IdName => $IdValue){
		foreach ($_COOKIE['qty'] as $qtyName => $qtyValue){
			if($IdName==$qtyName){
				foreach($_POST['txtQty'] as $qtyId => $qtyV){
					if(($qtyId + 1)==$IdName && ($qtyId + 1)==$qtyName){
						if($qtyV==0){
							$intCountZero++;						
						}
						$intCountItem++;
					}
				}
			
			}//if
		}//foreach
	}//foreach

if($intCountZero>0 && $intCountZero!=1){
	for($i = 1; $i <=$intCountZero; $i++) { 
	$intCount=1;
	$intQty=0;
	$intNum="0";
	$intCountOnce=0;
	$countV=0;
	foreach ($_COOKIE['id'] as $IdName => $IdValue){
			foreach ($_COOKIE['qty'] as $qtyName => $qtyValue){
				if($IdName==$qtyName){
					if(is_numeric($txtQty[$intQty])==false){
						$intNum="1";
						setcookie("id[$intCount]");
						setcookie("qty[$intCount]");
						setcookie("id[$intCount]","$IdValue",time()+86400);
						setcookie("qty[$intCount]","$qtyValue",time()+86400);
					}else{
					foreach($_POST['txtQty'] as $qtyId => $qtyV){
						if(($qtyId + 1)==$IdName && ($qtyId + 1)==$qtyName){
							if($qtyV==0 && $intCountOnce==0){
								setcookie("id[$intCount]");
								setcookie("qty[$intCount]");
								$countV=$intCount;
								$intCountOnce=1;
							}elseif($intCountOnce==1){
								setcookie("id[$countV]");
								setcookie("qty[$countV]");
								setcookie("id[$countV]","$IdValue",time()+86400);
								setcookie("qty[$countV]","$txtQty[$intQty]",time()+86400);
								setcookie("id[$intCount]");
								setcookie("qty[$intCount]");
							$countV++;
							}else{
								setcookie("id[$intCount]");
								setcookie("qty[$intCount]");
								setcookie("id[$intCount]","$IdValue",time()+86400);
								setcookie("qty[$intCount]","$txtQty[$intQty]",time()+86400);
							}
								$intCount++;
								$intQty++;
								
						}//if
						if($intQty==$intCountItem){
						break;
						}
					}//foreach		
					}//if
				}//if
				if($intQty==$intCountItem){
				break;
				}
			}//foreach
			if($intQty==$intCountItem){
			break;
			}
		}//foreach
	}//for
	}elseif($intCountZero==1){
	foreach ($_COOKIE['id'] as $IdName => $IdValue){
		$intCount=1;
		foreach ($_COOKIE['qty'] as $qtyName => $qtyValue){
		if($IdName==$qtyName){
			setcookie("id[$intCount]");
			setcookie("qty[$intCount]");
			}
		}
	}
	}else{//if no "0" found in qty value and checkbox remove is not checked
	$intCount=1;
	$intQty=0;
	$intNum="0";
	foreach ($_COOKIE['id'] as $IdName => $IdValue){
		foreach ($_COOKIE['qty'] as $qtyName => $qtyValue){
			if($IdName==$qtyName){
				if(is_numeric($txtQty[$intQty])==false){
					$intNum="1";
					setcookie("id[$intCount]");
					setcookie("qty[$intCount]");
					setcookie("id[$intCount]","$IdValue",time()+86400);
					setcookie("qty[$intCount]","$qtyValue",time()+86400);
				}else{
					setcookie("id[$intCount]");
					setcookie("qty[$intCount]");
					setcookie("id[$intCount]","$IdValue",time()+86400);
					setcookie("qty[$intCount]","$txtQty[$intQty]",time()+86400);
				}
				$intCount++;
				$intQty++;		
			}//if
		}//foreach
	}//foreach
}
	
if($intNum=="1"){
header("Location:".$PHP_SELF."?strAlert=1&$intCountZero");
}else{
header("Location:".$PHP_SELF."?strAlert=2&$intCountZero");
}
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

I'm not good enough to help you with most of this, even though I'll survey it and try. However, I can warn you of one thing.. Cookies might not be the way to go, hopefully someone can prove me wrong, or right, so you know. I say this because, if cookies are disabled then you're waste deep in trouble.

I will attempt to help but like i said, i'm not the go to guy at this point.
wyrmmage
Forum Commoner
Posts: 56
Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID

Post by wyrmmage »

ya, I recommend using Session variables, which you start using session_start() to accomplish, also, maybe you could modify your original post and state more clearly what exactly you are having trouble with? :?
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Hi,

Have ago with this cart code .. uses sessions .. pretty easy to follow ..

add_cart.php

Code: Select all

// Adds product to the cart.

if (is_numeric($_POST['itemid'])) { // Check for a product id.

	$itemid = $_POST['itemid'];
	
	// Check if the cart already contains one of the products.
	if (isset ($_SESSION['cart'][$itemid])) {
		$qty = $_SESSION['cart'][$itemid] + 1;
	} else {
		$qty = $_POST['qty'];
	}
	
	// Add product to the cart session variable.
	$_SESSION['cart'][$itemid] = $qty;

	// Show Cart
	require 'view_cart.php';

}

view_cart.php

Code: Select all

<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" style="margin: 2px; border: 1px solid #000000;">
 <tr>
  <th>Shopping Cart</th>
 </tr>
 <tr>
  <td><?php 

if (isset ($_POST['submit'])) {
	foreach ($_POST['qty'] as $key => $value) {
		if ( ($value == 0) AND (is_numeric ($value)) ) {
			unset ($_SESSION['cart'][$key]);
		} elseif ( is_numeric ($value) AND ($value > 0) ) {
			$_SESSION['cart'][$key] = $value;
		}
	}
}

$empty = TRUE;
if (isset ($_SESSION['cart'])) {
	foreach ($_SESSION['cart'] as $key => $value) {
		if (isset($value)) {
			$empty = FALSE;	
		}
	} 
}	

if (!$empty) {

	$query = 'SELECT * FROM items WHERE itemid IN (';
	foreach ($_SESSION['cart'] as $key => $value) {
		$query .= $key . ',';
	}
	$query = substr ($query, 0, -1) . ') ORDER BY itemid ASC';
	$result = mysql_query ($query);
		
?>

<form action="index.php?pages=viewcart" method="post">
  <table width="99%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr> 
     <td align="left"><b>Part No</b></td>
      <td width="380" align="left"><b>Product</b></td>
      <td width="162" align="right"><b>Price</b></td>
      <td width="143" align="center"><b>Qty</b></td>
      <td colspan="2" align="right"><b>Sub Total</b></td>
    </tr>
    <td width="207"> 

<?php

	$total = 0;
	while ($row = mysql_fetch_array ($result)) {
		
	$subtotal = $_SESSION['cart'][$row['itemid']] * $row['price'];
	$total += $subtotal;
		
	$qty = $_SESSION['cart'][$row['itemid']];
	$totalqty += $qty;
	
	session_register('total');
		$_SESSION['total'] = number_format($total, 2);
		
	session_register('qty');
		$_SESSION['qty'] = $qty;
		
	session_register('totalqty');
		$_SESSION['totalqty'] = $totalqty;
			
?>    </td>
    <tr> 
      <td align="left"><?php echo stripslashes($row['code']); ?></td>
      <td align="left"><a href="index.php?pages=sitem&itemid=<?php echo $row['itemid']; ?>"><?php echo stripslashes($row['name']); ?></a></td>
      <td align="right">$<?php echo $row['price'] ?></td>
      <td align="center"><center>
       <input type="text" size="3" name="qty[<?php echo $row['itemid'] ?>]" value="<?php echo $_SESSION['cart'][$row['itemid']] ?>" />
      </center></td>
      <td colspan="2" align="right">$<?php echo number_format ($subtotal, 2) ?></td>
    </tr>

<?php
}
?>
    <tr> 
      <td colspan="3" rowspan="2" align="right"><b></b></td>
      <td align="right">&nbsp;</td>
      <td width="106" align="right"><b>Total:</b></td>
      <td width="75" align="right"><b><font color="red">$<?php echo number_format ($total, 2) ?></font></b></td>
    </tr>
    <tr> 
      <td colspan="3" align="right"><br />
      <input type="submit" name="submit" value="Update Cart" class="submit-button" />
      </div></td>
    </tr>
 </table>
</form>

<br><center><a href="../index.php?pages=checkout">Checkout</a></center><br />

<?php

} else {

	echo '<br><center>Your cart is currently empty.</center><br>';
}

?></td>
 </tr>
</table>

Should be easy to work through ..

Good luck


Bob
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Your code is using session_register() and $_SESSION, why? Just scrap the session_register() calls alltogether. Why? The manual explains
http://php.net/session_register wrote: Caution
If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.

Caution
This registers a global variable. If you want to register a session variable from within a function, you need to make sure to make it global using the global keyword or the $GLOBALS[] array, or use the special session arrays as noted below.

Caution
If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered(), and session_unregister().
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Post by lmh85 »

hi there, there's error when the user entered 0 into the qty field. It can be detected if the user only enter once. But it enters 2 0 in a different field, it will not delete the cookies but instead, delete one and leave the other qty as 0 . After which , the user clicked once more on update button, it will delete ALL the cookies! :(
session? i don't know how to use. :(:( anyone enlighten me please!! :(
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Never use cookies when you can use sessions. Looking at the amount of data you are sending you might want to use the database too.
Also I have a strong feeling that you have massively overcomplicated your code.
started 'coding' i'm struck!!
struck means hit: bad
stuck: good :)
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Post by lmh85 »

I had edited the shopping cart that bob gave me on the session shopping cart. no items was found? is there anything i need to do before i get the session? i'm just using back the codes for view cart.

Code: Select all

if(isset($_POST['btnAdd'])){
// Adds product to the cart.
	if(is_numeric($txtQty)==true && $txtQty!=0){// Check for a product id.
		$intMoreQty=0;
		// Check if the cart already contains one of the products. 
		if (isset($_SESSION['cart'][$pdtId])){
			$qty =$_SESSION['cart'][$pdtId] + $txtQty;
			$intMoreQty=1;
	   }else{
			$qty = $_POST['txtQty'];
	   }
	   // Add product to the cart session variable.
		$_SESSION['cart'][$itemid] = $qty;
		if($intMoreQty==1){
			$comments = "We had added additional quantity to your shopping cart for this item.";
		}else{
			$comments = "Your item had added into the cart.";
		}
		print("<div align=\"center\" class=\"bodyText\">$comments<p /><input type=\"button\" name=\"btnClose\" class=\"btnCloseConfirm\" value=\"Close This Window\"onClick=\"window.close();\"></div>");
	}elseif($txtQty<=0){
		$lblWarning="<font color=\"#990000\"><b>Please enter number more than 0.</b></font>";
	}else{
		$lblWarning="<font color=\"#990000\"><b>Please enter numeric values.</b></font>";
	}
}
oops.. i'm stuck and soon be struck. :(
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Post by lmh85 »

ole wrote:Never use cookies when you can use sessions. Looking at the amount of data you are sending you might want to use the database too.
Also I have a strong feeling that you have massively overcomplicated your code.
started 'coding' i'm struck!!
struck means hit: bad
stuck: good :)
hey guru!! i also think that i'm having over complicated my codes. i'm never good in loopings. and this is my 1st time using php!! i'm so going to be struck. Why is being stuck good?
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Post by lmh85 »

i have the tot of placing it into the database too. But, what if the person get out of my site without checking out? does it mean that i need to manually clear it? anyone? help help..
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Post by lmh85 »

i had managed to do away with cookies and changed it to session! thanks everyone for the help! and i realised one thing. i'm writing too much redundant codes up there! :cry: :oops: :oops:
Post Reply