Jump to content


increasing form value +1 for every item in cart?


9 replies to this topic

#1 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 13 January 2006 - 12:24 AM

Ok I am using paypal to purchase my items, and for them they need the number in there forms (ie: item_1 etc) to increment +1 for each item so it would be item_2 then item_3 etc. How would I increase the number, probably the for function but im not sure how to do that. Heres what I have (minus some html you dont need):

// Load the cart and items
$user  = loadProfile();
$empty = isCartEmpty();
displayError(); ?>

<? // Check to see if any items in cart
			if (!$empty) { ?>

			<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
			<input type="hidden" name="cmd" value="_cart" />
			<input type="hidden" name="upload" value="1" />
			<input type="hidden" name="business" value="paypal@illicitfashion.com" />
			<input type="hidden" name="item_name" value="Illicit Fashion" />
			<input type="hidden" name="currency_code" value="USD" />
						
			<fieldset>
			<legend><img src="/images/cart.gif" alt="" /> Cart Items</legend>
					
				<div id="cartLeft">
					<div id="cartTop">
					<div class="c1"><acronym title="Delete">X</acronym></div>
					<div class="c2">Product</div>
					<div class="c3">Size</div>
					<div class="c4">Price</div>
					<div class="c5"><acronym title="Quantity">Q</acronym></div>
					<br clear="all" />
					</div>
					
					<? 
					$total = 0;
					$qty = 0;
					$userid = $_SESSION["user"]["id"];
					$query  = db_query("SELECT p.name, p.price, p.color, c.* FROM store_cart c, store_product p WHERE c.user = '$userid' AND c.id_pro = p.id ORDER BY c.id");
					while($item = db_fetch_object($query)) { 
						$price = $item->price * $item->quantity;
						$total += $item->price * $item->quantity;
						$qty += $item->quantity;
					?>
							
					<div class="cartItem">
					<div class="c1"><a href="store.php?page=cart&do=delete&id=<? pv($item->id) ?>" title="Remove <? pv($item->name) ?>"><img src="/images/cart-delete.gif" alt="X" /></a></div>
					<div class="c2"><a href="store.php?page=product&id=<? pv($item->id_pro) ?>" title="<? pv($item->name) ?>"><? pv($item->name) ?></a></div>
					<div class="c3"><? pv($item->size) ?></div>
					<div class="c4">$<? amount($price) ?></div>
					<div class="c5"><? pv($item->quantity) ?></div>
					<br clear="all" />
					</div>
					
					<input type="hidden" name="item_name_1" value="<? pv($item->name) ?>" />
					<input type="hidden" name="item_number_1" value="<? pv($item->id_pro) ?>" />
					<input type="hidden" name="amount_1" value="<? amount($price) ?>" />
					<input type="hidden" name="quantity_1" value="<? pv($item->quantity) ?>" />
					<input type="hidden" name="on0_1" value="Ordered" />
					<input type="hidden" name="os0_1" value="<? pv($item->color) ?> <? pv($item->size) ?>" />
					<input type="hidden" name="shipping_1" value="2.50" />
					<input type="hidden" name="shipping2_1" value=".50" />
					
					<? } ?>
					
					<div id="cartFooter">
					<p id="cartTotal">Grand Total: <b>$<? amount($total) ?></b></p>
					<input type="hidden" name="amount" value="<? amount($total) ?>" />
					
					<input type="hidden" value="TRUE" name="submit" id="submit" />
					<input type="image" src="/images/button-checkout.gif" alt="Checkout with Paypal" id="cartCheckout" />
					<a href="store.php?page=cart<? echo "&" ?>do=update" title="Update Cart" id="cartUpdate"><img src="/images/button-update.gif" alt="Update" /></a>
					</div>
					
				</div>

So for every item I need this:

<input type="hidden" name="item_name_1" value="<? pv($item->name) ?>" />
<input type="hidden" name="item_number_1" value="<? pv($item->id_pro) ?>" />
<input type="hidden" name="amount_1" value="<? amount($price) ?>" />
<input type="hidden" name="quantity_1" value="<? pv($item->quantity) ?>" />
<input type="hidden" name="on0_1" value="Ordered" />
<input type="hidden" name="os0_1" value="<? pv($item->color) ?> <? pv($item->size) ?>" />
<input type="hidden" name="shipping_1" value="2.50" />
<input type="hidden" name="shipping2_1" value=".50" />

To increment to

<input type="hidden" name="item_name2" value="<? pv($item->name) ?>" />
<input type="hidden" name="item_number_2" value="<? pv($item->id_pro) ?>" />
<input type="hidden" name="amount_2" value="<? amount($price) ?>" />
<input type="hidden" name="quantity_2" value="<? pv($item->quantity) ?>" />
<input type="hidden" name="on0_2" value="Ordered" />
<input type="hidden" name="os0_2" value="<? pv($item->color) ?> <? pv($item->size) ?>" />
<input type="hidden" name="shipping_2" value="2.50" />
<input type="hidden" name="shipping2_2" value=".50" />

for each item.

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 January 2006 - 04:11 PM

					$id = 0;
					while($item = db_fetch_object($query)){
						$id++;
						$total += $price = $item->price * $item->quantity;
						$qty += $item->quantity;
					?>
							
					<div class="cartItem">
					<div class="c1"><a href="store.php?page=cart&do=delete&id=<? pv($item->id) ?>" title="Remove <? pv($item->name) ?>"><img src="/images/cart-delete.gif" alt="X" /></a></div>
					<div class="c2"><a href="store.php?page=product&id=<? pv($item->id_pro) ?>" title="<? pv($item->name) ?>"><? pv($item->name) ?></a></div>
					<div class="c3"><? pv($item->size) ?></div>
					<div class="c4">$<? amount($price) ?></div>
					<div class="c5"><? pv($item->quantity) ?></div>
					<br clear="all" />
					</div>
					
					<input type="hidden" name="item_name_<?= $id; ?>" value="<? pv($item->name) ?>" />
					<input type="hidden" name="item_number_<?= $id; ?>" value="<? pv($item->id_pro) ?>" />
					<input type="hidden" name="amount_<?= $id; ?>" value="<? amount($price) ?>" />
					<input type="hidden" name="quantity_<?= $id; ?>" value="<? pv($item->quantity) ?>" />
					<input type="hidden" name="on0_<?= $id; ?>" value="Ordered" />
					<input type="hidden" name="os0_<?= $id; ?>" value="<? pv($item->color) ?> <? pv($item->size) ?>" />
					<input type="hidden" name="shipping_<?= $id; ?>" value="2.50" />
					<input type="hidden" name="shipping2_<?= $id; ?>" value=".50" />
					
					<? } ?>
The changes should be easy to spot, but it's basically adding $id up near the while loop, and in the <input>'s

#3 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 13 January 2006 - 06:44 PM

Its that simple, never would of guessed, also what does the = do in <?= $payid; ?>?

Ok quick question now, how would I show how many items they have in there cart? I can get the full price just cant figure the quantity.

Each item has its own row in the table with its own size and quantity, I need every quantity in each row matching the users id to display the total quantity of items.

Heres my total code so far:
function loadTotal() {
// Loads the total price of your cart
	global $_SESSION;
	
	$stotal = 0;
	$userid = $_SESSION["user"]["id"];
	$query  = db_query("SELECT p.*, c.* FROM store_cart c, store_product p WHERE c.user = '$userid' AND c.id_pro = p.id");
	while($result = db_fetch_object($query)) {
		$stotal += $result->price * $result->quantity; }
	$total = amount($stotal);
	
	return $total;
}

Edited by tiki, 13 January 2006 - 06:59 PM.


#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 January 2006 - 09:15 PM

2 things.
1. I'm confused, does getting the quantity from the previous script you posted even work? If so, why don't you just use that?
while($item = db_fetch_object($query)){
						$id++;
						$total += $price = $item->price * $item->quantity;
						$qty += $item->quantity;
					?>
2. This is just something for you to consider, as it could improve how fast or slow your script loads.
global $_SESSION;
You only need that if you're running php < 4.1
http://php.net/manual/en/language.variable...es.superglobals

As for what the "=" sign is... look here:
http://php.net/manua...tax.php#AEN2679

Edited by rc69, 13 January 2006 - 09:17 PM.


#5 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 14 January 2006 - 01:16 AM

Ive tried this:

function loadTotal() {
// Loads the total price of your cart
	global $_SESSION;
	
	$stotal = 0;
	$items  = 0;
	$userid = $_SESSION["user"]["id"];
	$query  = db_query("SELECT p.*, c.* FROM store_cart c, store_product p WHERE c.user = '$userid' AND c.id_pro = p.id");
	while($result = db_fetch_object($query)) {
		$stotal += $result->price * $result->quantity;
		$items += $result->quantity; }
	$total = amount($stotal);
	
	$echo = $items .' items at $'. $total;
	return $echo;
}

And all it echos is the $total.

#6 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 14 January 2006 - 02:12 AM

Ok...
So lets see what all is being returned from your query.
while($result = db_fetch_object($query)){
echo highlight_string(print_r($result,1),1).'<br>';
	$stotal += $result->price * $result->quantity;
	$items += $result->quantity;
//echo $items.'<br>';
}
First echo will print everything in the $result object to your screen on each run through the loop. The second (currently commented out) echo will print whatever is in $items on each run (once un-commented).

#7 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 14 January 2006 - 02:31 AM

Heres what it is, with uncommented echo:

stdClass Object
(
	[price] => 10.00
	[id] => 1
	[id_pro] => 1
	[quantity] => 2
	[user] => 3
)

2
stdClass Object
(
	[price] => 10.00
	[id] => 2
	[id_pro] => 2
	[quantity] => 1
	[user] => 3
)

3

and ive also reworked the code to this, and it echos 10.003 items at $5. Which is wrong since it should be, 3 items at $30.00

function loadTotal() {
// Loads the total price of your cart
	global $_SESSION;
	
	$stotal = 0;
	$items  = 0;
	$userid = $_SESSION["user"]["id"];
	$query  = db_query("SELECT p.price, p.id, c.id_pro, c.quantity, c.user FROM store_cart c, store_product p WHERE c.user = '$userid' AND c.id_pro = p.id");
	while($result = db_fetch_object($query)) {
		$items += $result->quantity;
		$stotal = $result->price * $result->quantity;
		 }
	$total = amount($stotal);
	
	echo $items .' items at $'. $total;
}

Edited by tiki, 14 January 2006 - 02:43 AM.


#8 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 14 January 2006 - 06:53 PM

I've got one last ditch effort to figure this out, if it doesn't work, then i'm out of ideas.
Change the echo at the bottom of that function to this:
echo '<span style="color:red;">'.$items.' items at $'.$total.'</span>';
This is simply to test a theory. You said the script as-is echos "10.003 items at $5", which means there may be something else outputting something. If the entire string is red, then you know this is a real problem. If "3 items at $5" is the only red part, then the problem is else where.

#9 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 15 January 2006 - 12:47 AM

Ya weird, you were right:

10.003 items at $53 items at $5

I tried both echos btw. No idea were the 10.00 is coming from.

#10 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 15 January 2006 - 12:59 AM

Screw it, I made it two functions:

function loadItems() {
// Loads the total items in your cart

	$items  = 0;
	$userid = $_SESSION["user"]["id"];
	$query  = db_query("SELECT p.id, c.id_pro, c.quantity, c.user FROM store_cart c, store_product p WHERE c.user = '$userid' AND c.id_pro = p.id");
	while($result = db_fetch_object($query)) {
		$items += $result->quantity; }
	
	echo $items;
}

function loadTotal() {
// Loads the total price of your cart
	
	$total = 0;
	$userid = $_SESSION["user"]["id"];
	$query = db_query("SELECT p.name, p.price, p.color, c.* FROM store_cart c, store_product p WHERE c.user = '$userid' AND c.id_pro = p.id ORDER BY c.id");
	while($result = db_fetch_object($query)) {
		$total += $result->price * $result->quantity; }
	
	amount($total);
}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users