Shopping Cart for Shop -- MySQL [PLEASE HELP]

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

User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Shopping Cart for Shop -- MySQL [PLEASE HELP]

Post by tecktalkcm0391 »

Hello everyone,

I wanted to get some suggestions/help on how to do this, along with what codes/functions to use <-- I can probably figure out the codes/functions.

Right now I have a MySQL database named items with a list of item numbers matched with their name, description, image location, and their owner. Some of the items have the owner set as (Shop [#]) and I wanted to make a page named shop. and then have a it GET the id of the page and then get the items for that shop and display them in a 5 by X table (X being how many rows it needs).

I know how to do most of that part. But this is what I don't know what/how to do [it]:

I want to make it display each item ONLY once, and then show how many of each one there is under its name and picture, in the table. And then have a button to add to a "shopping cart", where you can add as many items want of any amount/type of items, execpt if you don't have enough [fake] money [on the site] in your hand to buy all of the items in your cart plus the item you click on. When an user adds the item to there cart, it takes away the item from the list so that other users don't see it as avaliable. Finally when the user is done, they can click purchase, and then it shows them everything that is is there "shopping cart", with the quanity and the price, and then they click pay, and... ... then the total for the purchase is deduced out of there "money" on the site.


Any ideas, any help, anything to read/look out, any code?

Anything that could point me in the right direction would help....

:D :D THANKS FOR ALL OF THE HELP! :D :D
Last edited by tecktalkcm0391 on Fri Jun 23, 2006 3:07 pm, edited 3 times in total.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Can someone just point me in the right direction. Why doesn't anyone respond?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Research:
http://www.google.com/search?hl=en&q=shopping+cart+php
http://www.google.com/search?hl=en&q=sh ... t+php+code
http://www.google.com/search?hl=en&q=sh ... +code+free
http://www.google.com/search?hl=en&lr=& ... g+cart+php
http://www.codebeach.com/tutorials/buil ... ss-php.asp
http://www.thewatchmakerproject.com/jou ... pping-cart <--- More towards what I want... the only thing I can't figure out is that my "shops" only have X amount of items in it at one time, and if 100 users want to get item1 and there are only 50 of item1 and they all add them to there cart then only the first 50 get it when they pay. I want it to take it and "put it" in there cart as if you were at a store, and then put it back if they remove it from there cart or leave the store (page) for more than X minutes


I still can't find anythingn on what I am trying to acomplish...
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Ok, I got my "shop" with a shopping cart up and running.

Now what I want to do, but I still can't figure out [the logic for] is that my "shops" only have X amount of items in it at one time.
Now say 250 users want to get item 1, but there are only 50 of item 1. So they all put it in their shopping cart.
Now say 200 of the users add something eles to their cart while only 50 go straight to their cart, and pay, and recieve the item.
The other 200 users will go to pay, and won't be able to get all of the items in there cart because the other 50 users areadly "bought" the 50 item 1's that were for sale.

How could I solve this? I was thinking about making it where when the users click to put it in the cart it acutally would be like a store (how at the store you take what you want and put it in your cart and then put it back if you don't want it, or pay for it.). So everytime a item was put in a cart the number of items advaliable would go down. And then if the user remove it from there cart or left the store (page) for more than X minutes, then the item would go back into the shop.

The reason I need to find a way to fix this is because right now I only have 100 members, so it should be find, but if there were 10,000 members in the same shop at the same time getting the same item. I would have a lot of problems.

Thanks for the help. This is really fustrating me !


Note: Each item on my site, has its own id, so if there are 10 item 1's then there would be 10 unique numbers for each of them.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Right now I have:

Shop Front:

Code: Select all

<?php
ob_start();
// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>Shop <?php print($_GET['shopid']; ?></title>
	<link rel="stylesheet" href="css/styles.css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><style type="text/css">
<!--
body {
	margin-left: 5px;
	margin-top: 5px;
	margin-right: 5px;
	margin-bottom: 5px;
}
-->
</style></head>

<body>

<h1 align="center">Your Shopping Cart</h1>

<div align="center">
  <?php
echo writeShoppingCart();
?>
  
  
</div>
<h1 align="center">Items In Our Store</h1>

<div align="center">
  <?php
$sql = "SELECT * FROM books WHERE shopid = '{$_GET['shopid']}' ORDER BY id";
$result = $db->query($sql);
$output[] = '<ul>';
while ($row = $result->fetch()) {
	$output[] = '<li>"'.$row['title'].'" by '.$row['author'].': &pound;'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
}
$output[] = '</ul>';
echo join('',$output);
?>
  
  
</div>
</body>
</html>
Shopping Cart:

Code: Select all

<?php
ob_start();
// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
	case 'add':
		if ($cart) {
			$cart .= ','.$_GET['id'];
		} else {
			$cart = $_GET['id'];
		}
		break;
	case 'delete':
		if ($cart) {
			$items = explode(',',$cart);
			$newcart = '';
			foreach ($items as $item) {
				if ($_GET['id'] != $item) {
					if ($newcart != '') {
						$newcart .= ','.$item;
					} else {
						$newcart = $item;
					}
				}
			}
			$cart = $newcart;
		}
		break;
	case 'update':
	if ($cart) {
		$newcart = '';
		foreach ($_POST as $key=>$value) {
			if (stristr($key,'qty')) {
				$id = str_replace('qty','',$key);
				$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
				$newcart = '';
				foreach ($items as $item) {
					if ($id != $item) {
						if ($newcart != '') {
							$newcart .= ','.$item;
						} else {
							$newcart = $item;
						}
					}
				}
				for ($i=1;$i<=$value;$i++) {
					if ($newcart != '') {
						$newcart .= ','.$id;
					} else {
						$newcart = $id;
					}
				}
			}
		}
	}
	$cart = $newcart;
	break;
}
$_SESSION['cart'] = $cart;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>Shopping Cart</title>
	<link rel="stylesheet" href="css/styles.css" />
</head>

<body>


<h1 align="center">Your Shopping Cart</h1>

<div align="center">
  <?php
echo writeShoppingCart();
?>
  
  
</div>
<h1 align="center">Please check quantities...</h1>

<div align="center">
  <?php
echo showCart();
?>
  
</div>
<p align="center"><a href="index.php">Back to shop...</a></p>



</body>
</html>
Now since I have my database like this:
Item ID...........Name...........Picture URL.................Price..............Owner
111111...........Toast.................toast.jpg....................13.00.............User1
111112...........Eggs.................eggs.jpg......................34.54.............User1
------------------------------
111120...........Milk...................milk.jpg........................35.49.............User2
------------------------------
111124...........Milk...................milk.jpg........................44.99.............User1
------------------------------
------------------------------
111151...........Toast.................toast.jpg....................13.00.............Shop 10
111152...........Eggs.................eggs.jpg......................34.54.............Shop 10
How can I make it compatable? I know I need to get the shop ID and then make it into 'Shop'. $id then query for all of the item with that shop name next to it. But I don't know how to only list an item on the page one time, and then just add to its quanity, so it displays X Toast only once and X Eggs only once. (READ MY PREVIOUS POSTS FOR MORE INFO, OF MY GOALS)

I've been just using a simple database like this:
id...... title......... author...... price....... shopid
1....... Book1 ..... Author1..... 24.99 ......4
2....... Book2 ..... Author2..... 21.99 ......2
3....... Book3 ..... Author3..... 8.99 ........3
4....... Book4 ..... Author2..... 14.99 ......2
To get the basic code, but thats not the way the database I need to read from is setup. So what can I do?
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

tecktalkcm0391 wrote:[...]
$sql = "SELECT * FROM books WHERE shopid = '{$_GET['id']}' ORDER BY item_code";

exept selecting only it once (so if there are 10 of the items it only gets all the information for the one time) <-- Read the post for more about this.
As you've pointed out, your statement might return x entries. Are there any criteria as to which entry is more important?
Otherwise, you'd might want to use LIMIT.

cy,
Marc

btw: I wouldn't place anything like $_GET /$_POST etc. (user input) straight in the mysql statement. Do yourself a favour and use an intermediary variable so that you can use a validating function at the start of the script.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

OK, i've been working on this for about a week now, and still no where, can anybody help?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Ok, this is basically what I need help with:

I have a list of items

1 > Eggs
2 > Eggs
3 > Eggs
4 > Milk
5 > Milk
6 > Eggs

and I want to show eggs and milk only one time, but show the quanity. Then have the user be able to add X to there cart an say they add 3 eggs it would now only show 1 eggs left, and if they delete it from there cart it would go back to 4.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Are you showing the Shopping Cart or the Product Listing there?
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

tecktalkcm0391 wrote:Ok, this is basically what I need help with:

I have a list of items

1 > Eggs
2 > Eggs
3 > Eggs
4 > Milk
5 > Milk
6 > Eggs

and I want to show eggs and milk only one time, but show the quanity. Then have the user be able to add X to there cart an say they add 3 eggs it would now only show 1 eggs left, and if they delete it from there cart it would go back to 4.
Think inventory. Logically, when you go to the grocery store, there is a certain amount of inventory. There is some in storage, some on the shelf and some in the channel. What is sold is not counted as inventory because the store doesn't own it anymore. So you have to know what is in storage (maybe, your system might not need this), what is on the shelf (something you have asked for in this thread) and how many are in the channel (awaiting purchase, but not committed).

Essentially your total inventory is the sum of all three pieces. It just happens to be in different areas. So if I take an item from the shelf, the inventory stays the same, but the shelf amount decreases by one unit and the channel increases by a unit. If I by the item, the inventory decreases by a unit. If I change mymind before buying, I put the item back on the shelf, the channel decreases by a unit and the shelf increases by a unit. Are you seeing the logic behind this?

What I would do is develop my system so that I can know my total inventory (also good for knowing when to reorder supplies [kanban system, you know?]) and keep track of what is in the channel. This will always let you know what is on the shelf and what is in the cars based on the number of items in the cart. So your code would essentially 'take one away' from the shelf everytime a user 'put one into' their cart.

Does this make sense?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

OK, my minds rolling now. Thanks!
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Ok, now I have another question... Would I just do something like select * from items where location='shop'
and then see how many rows come up to show the quanity.

Also.

Since my database is like this:
id...... title......... author...... price....... shopid ...... location.....owner
1....... Book1 ..... Author1..... 24.99 ......4 ............ Shop........
2....... Book2 ..... Author2..... 21.99 ......2 ............ Cart.........
3....... Book3 ..... Author3..... 8.99 ........3 ............ User........ User1
4....... Book4 ..... Author2..... 14.99 ......2 ............ Shop.......
5....... Book2 ..... Author2..... 21.99 ......2 ............ Cart........
6....... Book3 ..... Author3..... 8.99 ........3 ............ Shop.......
7....... Book4 ..... Author2..... 14.99 ......2 ............ Shop.......
How would I have the user "buy" the item. and then make the owner the username of an item in the cart without having two user "get" the same item, but only the first one gets it.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

tecktalkcm0391 wrote:Ok, now I have another question... Would I just do something like select * from items where location='shop'
and then see how many rows come up to show the quanity.
Are you asking "how do I get the number if items that have the same shopid?"
tecktalkcm0391 wrote:How would I have the user "buy" the item. and then make the owner the username of an item in the cart without having two user "get" the same item, but only the first one gets it.
By "buy" do you mean add to a cart or purchase at checkout?

What does "make the owner the username of an item" mean?

When you ask "without having two user "get" the same item, but only the first one gets it.", does "get" mean the same as "buy" above?

And when you say "the same item," we would need to know if each item/SKU is a unique thing for sale, or do they have a inventory quantity?
(#10850)
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

arborint wrote: And when you say "the same item," we would need to know if each item/SKU is a unique thing for sale, or do they have a inventory quantity?
Looks like the former, as each book has a unique id.

Personally, I wouldn't use the unique id approach, as it only comes down to using more database space than actually necessary.

aerodromoi
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

Just to give you an idea...

Code: Select all

<?php
class shopping{

   // simulate db
   var $product = array(
                       12 => array("mobiles",5,150.49),
                       13 => array("apples",200,0.30),
                       100 => array("pcs",3,599.30),
                       15 => array("pens",50,1.00)
                     );
   var $bill;
   
   function add_article_to_cart($product_no,$product_quantity){
   
      print("<h2>Buy</h2>");
      print($product_quantity." ".$this->product[$product_no][0]." added to cart.<br />\n");
      if($this->product[$product_no][1] < $product_quantity) {
         print ("Sorry, you can only buy ".$this->product[$product_no][1]." ".$this->product[$product_no][0]."<br />\n");
      }
      else{
         $this->product[$product_no][1] -= $product_quantity;      
         $this->bill += $product_quantity*$this->product[$product_no][2];
         print ($product_quantity." ".$this->product[$product_no][0]." bought.<br />\n");
         print ("Your bill: ".$this->bill." Euros<br />\n");
         
      }
      print ("<br />\n");
   }
   
   function remove_article_from_cart($product_no,$product_quantity){
      
      $this->product[$product_no][1] += $product_quantity;
      $this->bill -= $product_quantity*$this->product[$product_no][2];
      
      print("<h2>Return</h2>");
      print ($product_quantity." ".$this->product[$product_no][0]." removed from cart.<br />\n");      
      print ("Your bill: ".$this->bill." Euros<br />\n");
   }
   
   function show_stock(){
      
      echo "<h2>Show All Products</h2>";
      foreach ($this->product as $key){
         echo $key[1]." ".$key[0]. "<br />";
      }
      echo "<br />\n";
   }
   
}

$myshopping = new shopping;

// show all products
$myshopping->show_stock();

//buy six mobiles and two apples
$myshopping->add_article_to_cart(12,6);
$myshopping->add_article_to_cart(13,2);

// show stock
$myshopping->show_stock();

// remove one apple from cart
$myshopping->remove_article_from_cart(13,1);

// show stock
$myshopping->show_stock();
?>
Good luck,
aerodromoi
Post Reply