Jump to content


In theory will this work?


15 replies to this topic

#1 tiki

    Young Padawan

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

Posted 18 January 2006 - 12:20 AM

Im not sure if it will work or not, but this is suppose to check every item in there cart with the user id, then check the items quantity and see if it matches to quantity of the product. If the product quantity is lower then the quantity in there cart, change it.

So in theory this should work right?

function updateCart($id) {
// Updates the cart
	$query = db_query("SELECT * FROM store_cart WHERE user = $id");
	while($check == db_fetch_object($query)) {
	
		// Checks the size in the cart
		if ($check->size == "Youth") {
			$quantity = "quantity_1"; 
		} elseif ($check->size == "Small") {
			$quantity = "quantity_2";
		} elseif ($check->size == "Medium") {
			$quantity = "quantity_3"; 
		} elseif ($check->size == "Bag") {
			$quantity = "quantity_0"; }
			
		// Finds the item with the cart and user
		$query = db_query("SELECT id, $quantity FROM store_product WHERE id = $check->id_pro");
		while($left == db_fetch_object($query)) {
		
			// If quantity in there cart is greater then quantity left
			if ($check->quantity > $left->$quantity) {
				
				// Drop the cart if no product quantity left
				if ($left->$quantity == 0) {
					$query = db_query("DELETE FROM store_cart WHERE id = $check->id");
				
				// If product quantity is 1, set there quantity to 1
				} elseif ($left->$quantity == 1) {
					$query = db_query("UPDATE store_cart SET quantity = 1 WHERE id = $check->id AND id_pro = $left->id AND user = $id");
				}
				
			}
		}
	}
}

Edited by tiki, 18 January 2006 - 12:22 AM.


#2 rc69

    PHP Master PD

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

Posted 18 January 2006 - 02:11 PM

The only way to know if a script will work is to test it. But, in THEORY, that could work.
One question i have is, what happens when there is more then 1 product left?
				if ($left->$quantity == 0) {
					$query = db_query("DELETE FROM store_cart WHERE id = $check->id");
				
				// If product quantity is 1, set there quantity to 1
				} elseif ($left->$quantity == 1) {
					$query = db_query("UPDATE store_cart SET quantity = 1 WHERE id = $check->id AND id_pro = $left->id AND user = $id");
				}


#3 tiki

    Young Padawan

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

Posted 18 January 2006 - 08:55 PM

If there is more then 1 product, the users cart shouldnt change unless the quanity of the product is lower then the quantity of the cart.

#4 rc69

    PHP Master PD

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

Posted 18 January 2006 - 10:38 PM

I've got no idea what you're selling, but imagine that there is 2 products in stock, and the user wants 5. This is just the scenerio that made me think to ask that question.

But other then that, have you tested the script and determined whether or not it works yet?

random comment: lol, just read the first line up until the comma... don't i sound like a nice person :D

#5 tiki

    Young Padawan

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

Posted 18 January 2006 - 11:09 PM

Haha =P ya, im gona try it later after I finish my finals hw o_O die school.

But say the user has 5 shirts in there cart and theres 2 left how would I check that, because all I have is usercart is greater then quantity.

Edited by tiki, 18 January 2006 - 11:12 PM.


#6 tiki

    Young Padawan

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

Posted 19 January 2006 - 01:06 AM

You think this will fix our little problem :D

// Drop the cart if no product quantity left
				if ($left->$quantity == 0) {
					$query = db_query("DELETE FROM store_cart WHERE id = $check->id");
				
				// If product quantity is 1, set there quantity to 1
				} elseif ($left->$quantity == 1) {
					$query = db_query("UPDATE store_cart SET quantity = 1 WHERE id = $check->id AND id_pro = $left->id AND user = $id");
				
				// Else the quantity is more then 1
				} else {
					$qty = $left->$quantity;
					$query = db_query("UPDATE store_cart SET quantity = $qty WHERE id = $check->id AND id_pro = $left->id AND user = $id");
				}


#7 rc69

    PHP Master PD

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

Posted 19 January 2006 - 02:19 PM

That's what i was thinking, but you're repeating your code. You could use less code by doing something like:
			// If quantity in there cart is greater then quantity left
			if ($check->quantity > $left->$quantity) {
				// Drop the cart if no product quantity left
				if ($left->$quantity == 0) {
					$query = db_query("DELETE FROM store_cart WHERE id = ".$check->id);

				// Else the quantity is more then 0
				} else {
					$query = db_query("UPDATE store_cart SET quantity = ".$left->$quantity." WHERE id = ".$check->id." AND id_pro = ".$left->id." AND user =". $id);
				}
			}
That keeps things short and non-repetitive, and hopefully working :)

p.s. Yes, i took your variables out of the quotes, it's personal preference (espcially with objects and arrays). All i simply did was remove the if-statement from the else, and changed the 1 in the db_query to $left->$quantity.

Edited by rc69, 19 January 2006 - 02:20 PM.


#8 tiki

    Young Padawan

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

Posted 19 January 2006 - 06:43 PM

Yes I was thinking that also...

Question but its probably true,

your allowed to use variables from a parent while loop in a child loop right, makes sense.


Edit: Ive tried it and it doesnt seem to work. I have more quantity in my cart then in the store, but my cart stays the same.

Edited by tiki, 19 January 2006 - 07:01 PM.


#9 rc69

    PHP Master PD

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

Posted 20 January 2006 - 11:35 PM

Well, you're loop question is true, you can use variables in the child loops.

As for the cart not updating... my only obvious question is, are you making the call to updateCart, and if so, are you passing the right id?
Remember what i told you about highlight_string and making sure your variables contain what you want them to contain? Try adding a few of those/some echos to that function and making sure everything is acting the way it should.

#10 tiki

    Young Padawan

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

Posted 21 January 2006 - 01:09 AM

Ya its the right id, I even tried $_GEt["id'];

Ill test the highlight and change a few variables.
---
Btw your allowed to do this right?

if ($size == "Bag") { $quantity = "quantity_0"; }

$row = db_fetch_object($result);

echo "$row->$quantity";

Use the variable on the db object?


And it seems my add to cart still adds items when there is no quantity.

function addItem($id, $size) {
// Add product to cart if it exists
	global $_SESSION;
	
	if ($size == "Bag") {
		$quantity = "quantity_0"; 
	} elseif ($size == "Youth") {
		$quantity = "quantity_1"; 
	} elseif ($size == "Small") {
		$quantity = "quantity_2";
	} elseif ($size == "Medium") {
		$quantity = "quantity_3"; 
	}
		
	$query  = "SELECT id, $quantity FROM store_product WHERE id = '$id'";
	$result = db_query($query);
	$row = db_fetch_object($result);
	
	if (db_num_rows($result) != 1) {
		header("Location: store.php?page=cart");
		
	} elseif($row->$quantity == 0) {
		header('Location: store.php?page=cart');
	
	} else {	
	
		// Check if its in the cart already
		$userid = $_SESSION["user"]["id"];	
		$query  = "SELECT id_pro FROM store_cart WHERE id_pro = '$id' AND user = '$userid' AND size = '$size'";
		$cart = db_query($query);
		
		if (db_num_rows($cart) == 0) {
			// Put the product in cart table
			$query = db_query("INSERT INTO store_cart (id_pro, quantity, size, user, date) VALUES ('$id', '1', '$size', '$userid', NOW())");
		} else {
			// Update product quantity in cart table
			$query = db_query("UPDATE store_cart SET quantity = quantity + 1 WHERE user = '$userid' AND id_pro = '$id' AND size = '$size'");				
		}	
	
	}				
}
---
I figured out my update function, but It only does the first item in the cart, not every tiem... and I get this error after it corrects the cart:

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/illicitf/public_html/library/database.php on line 97

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/illicitf/public_html/library/database.php on line 97

#11 rc69

    PHP Master PD

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

Posted 24 January 2006 - 04:34 PM

About the variable object thingy... yes, you are allowed to, if you weren't, php would yell at you. I believe the variable object thing would be the same concept as variable variables.

As for the mysql_fetch_object error, it would be the same common error you get with mysql_fetch_array.
http://www.pixel2life.com/forums/index.php...sql_fetch_array

And, since i don't know enough about the table set up, and everything else, i can't really guess as to why it only updates one item.
But i do know of one thing you can change that has nothing to do with the problem. Where you redirect people after setting $row, you can remove an if-statement and change it to:
if(db_num_rows($result) != 1 || $row->$quantity == 0){
		header('Location: store.php?page=cart');
	} else {
		// Check if its in the cart already

p.s. Please don't triple post, use the edit button.

#12 tiki

    Young Padawan

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

Posted 24 January 2006 - 07:58 PM

Well I still cant get the errors to go away and it to update more then 1 item, and heres my table setup.

store_cart
 id   	int(10)  auto_increment   	  
 id_pro  int(10)
 quantity  int(10) 	  
 size	  varchar(100) 	
 user	  varchar(32) 
 date	  datetime


store_product
	   id   	int(10)
	 id_cat	  int(10) 
	 name	  varchar(100)
	 description	  varchar(250)
	 color	  varchar(15)
	 price	  decimal(5,2)
	 type	  enum('bag', 'shirt')
	 quantity_0	  varchar(5) // Bags
	 quantity_1	  varchar(5) // Youth Size
	 quantity_2	  varchar(5) // Small
	 quantity_3	  varchar(5) // Medium
	 image_small	  varchar(100) 
	 image_large	  varchar(100)
	 on_special	  enum('yes', 'no')

And the code:

function updateCart($id) {
// Updates the cart
	echo '<div class="updated"><b>Updated</b><br />';
	$query = mysql_query("SELECT * FROM store_cart WHERE user = '$id'") or die(mysql_error());
	while($check = mysql_fetch_array($query)) {
	
		// Checks the size in the cart
		if ($check["size"] == "Bag") {
			$quantity = "quantity_0"; 
		} elseif ($check["size"] == "Youth") {
			$quantity = "quantity_1"; 
		} elseif ($check["size"] == "Small") {
			$quantity = "quantity_2";
		} elseif ($check["size"] == "Medium") {
			$quantity = "quantity_3"; 
		}
			
		// Finds the item with the cart and user
		$query = mysql_query("SELECT id, $quantity FROM store_product WHERE id = '$check[id_pro]'") or die(mysql_error());
		while($left = mysql_fetch_array($query)) {
			$stock = $left["$quantity"];
			
			// If quantity in there cart is greater then quantity left
			if ($check["quantity"] > $stock) {
				// Drop the cart if no product quantity left
				if ($stock == 0) {
					$query = db_query("DELETE FROM store_cart WHERE id = $check[id]");
					echo "An item has been removed there was none left instock!";
				// Else the quantity is more then 0
				} else {
					$qty = $stock;
					$query = db_query("UPDATE store_cart SET quantity = '$qty' WHERE id = '$check[id]' AND id_pro = '$left[id]' AND user = '$id'");
					echo "An items quantity has been lowered because you have more then there is instock!";
				}
			} else {
				echo "Your cart has been checked and updated!";
			}
		}
	}
	echo "\n</div>";
}

Edited by tiki, 24 January 2006 - 08:00 PM.


#13 rc69

    PHP Master PD

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

Posted 24 January 2006 - 10:26 PM

ok... well, just a little bit ago, we were having problems with addItem(), now we're working with updateCart(). So which one is it that i should be looking at, and what is the function in /home/illicitf/public_html/library/database.php around line 97 that is causing the errors (also, which line exactly is line 97)?

Also, something's bugging me about your store_cart table. I'm assuming that each user will get their own row, which is natural for most systems i know of. But from looking at your table structure, if a user were to buy either 1) multiple products or 2) multiple sizes of a single product, they would need another row in the database. So a slight explaination of which way you go about it, could help with our "updateCart" problems.

Edited by rc69, 24 January 2006 - 10:36 PM.


#14 tiki

    Young Padawan

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

Posted 24 January 2006 - 11:10 PM

The database line is just

return mysql_fetch_array($query); or
return mysql_fetch_object($query);

Aswell with the cart for every item of product+size adds a new row into the database.

So basically cart database would be like this, id is the id, q is the quantity and u is the user id, etc.

Id1 - Medium Gray Shirt - Q2 - U1
Id2 - Large Gray Shirt - Q1 - U1
Id3 - Youth Green Shirt - Q3 - U1

Also im trying to work on both functions, but id rahter get updateCart() working correctly first.

#15 tiki

    Young Padawan

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

Posted 27 January 2006 - 02:26 AM

I got the update cart working and the add to cart.

I just need the add to cart to check the current quantity, hmm btw heres my working update code.

function updateCart($id) {
// Updates the cart
	echo '<div class="updated"><b>Updated</b><br />';
	$cart = db_query("SELECT * FROM store_cart WHERE user = '$id'") or die(mysql_error());
	while($check = db_fetch_array($cart)) {
	
		// Checks the size in the cart
		if ($check["size"] == "Bag") {
			$quantity = "quantity_0"; 
		} elseif ($check["size"] == "Youth") {
			$quantity = "quantity_1"; 
		} elseif ($check["size"] == "Small") {
			$quantity = "quantity_2";
		} elseif ($check["size"] == "Medium") {
			$quantity = "quantity_3"; 
		}
			
		// Finds the item with the cart and user
		$items = db_query("SELECT id, $quantity FROM store_product WHERE id = '$check[id_pro]'") or die(mysql_error());
		while($left = db_fetch_array($items)) {
			$stock = $left["$quantity"];
			
			// If quantity in there cart is greater then quantity left
			if ($check["quantity"] > $stock) {
			
				// Drop the cart if no product quantity left
				if ($stock == 0) {
					$item = db_query("SELECT * FROM store_product WHERE id = '$left[id]'");
					$echo = db_fetch_object($item);
					echo "The item '$echo->name' has been removed because its out of stock! <br />\n";
					
					$query = db_query("DELETE FROM store_cart WHERE id = $check[id]") or die(mysql_error());
				// Else the quantity is more then 0
				} else {
					$item = db_query("SELECT * FROM store_product WHERE id = '$left[id]'");
					$echo = db_fetch_object($item);
					echo "The quantity of item '$echo->name' has been lowered due to its stock! <br />\n";
					
					$qty = $stock;
					$query = db_query("UPDATE store_cart SET quantity = '$qty' WHERE id = '$check[id]' AND id_pro = '$left[id]' AND user = '$id'") or die(mysql_error());
				}
			}
		}
	}
	echo "Your cart has been updated! <br />\n</div>";
}


And here my add to cart function, how would I make it check there current cart quantity against the product quantity... and if there trying to add more then there is, display an error.

function addItem($id, $size) {
// Add product to cart if it exists
	global $_SESSION;
	
	if ($size == "Bag") {
		$quantity = "quantity_0"; 
	} elseif ($size == "Youth") {
		$quantity = "quantity_1"; 
	} elseif ($size == "Small") {
		$quantity = "quantity_2";
	} elseif ($size == "Medium") {
		$quantity = "quantity_3"; 
	}
		
	$query  = "SELECT id, $quantity FROM store_product WHERE id = '$id'";
	$result = db_query($query);
	$row = db_fetch_object($result);
	
	if (db_num_rows($result) != 1) {
		echo '<div class="error"><b>Error</b><br />';
		echo "That item and or size was not found!";
		echo "\n</div>";
	} elseif($row->$quantity == 0) {
		echo '<div class="error"><b>Error</b><br />';
		echo "That item is out of stock, sorry!";
		echo "\n</div>";
	} else {	
	
		// Check if its in the cart already
		$userid = $_SESSION["user"]["id"];	
		$query  = "SELECT id_pro FROM store_cart WHERE id_pro = '$id' AND user = '$userid' AND size = '$size'";
		$cart = db_query($query);
		
		if (db_num_rows($cart) == 0) {
			// Put the product in cart table
			$item = db_query("SELECT * FROM store_product WHERE id = '$id'");
			$echo = db_fetch_object($item);
			echo '<div class="updated"><b>Added</b><br />';
			echo "The item '$echo->name' has been added to your cart!";
			echo "\n</div>";
			
			$query = db_query("INSERT INTO store_cart (id_pro, quantity, size, user, date) VALUES ('$id', '1', '$size', '$userid', NOW())");
		} else {
			// Update product quantity in cart table
			$item = db_query("SELECT * FROM store_product WHERE id = '$id'");
			$echo = db_fetch_object($item);
			echo '<div class="updated"><b>Increased</b><br />';
			echo "The quantity of item '$echo->name' has been increased!";
			echo "\n</div>";
			
			$query = db_query("UPDATE store_cart SET quantity = quantity + 1 WHERE user = '$userid' AND id_pro = '$id' AND size = '$size'");				
		}	
	
	}				
}

Edited by tiki, 27 January 2006 - 02:29 AM.


#16 rc69

    PHP Master PD

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

Posted 27 January 2006 - 05:42 PM

You'd do it the same way you did in the updateCart function, except you would change the action taken in the if-statements.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users