Page 1 of 1

Creating a shopping basket

Posted: Sat Oct 07, 2006 9:06 am
by impulse()
Most of my shopping site is complete now. I'm struggling on the shopping basket a bit though. I want to create an array ($product[$i]) where $i is equal to the product ID which is in a MySQL DB. To do this I've used:

Code: Select all

$i = 1;
while ($results = mysql_fetch_array($query)) {
  $product[$i] = 1;

  // Other code to output the prodcuts in the DB //

$i++;
}
So now I have an array and each element in the array is equal to a product in the DB.

Then I have a text box with a submit button next to each item, each entry has a hidden field and the code looks like this:

Code: Select all

<form method="post" action="books.php">
        <input type="hidden" name="boxID" value="<?php echo $res['id']; ?>"><br><br>
        <input type="submit" name="add" value="Add to cart">
        Quantity <input type="text" name="quantity" value="0">
So I'm able to output what is entered in the text field and link it to what product the user wants to purchase. So now all I need is to create a session variable to carry these products across to each page the user views without loosing the data, so I have:

Code: Select all

if (isset($_REQUEST['add'])) { # Add to cart

  $quantity = $_REQUEST['quantity'];
  $boxID = $_REQUEST['boxID'];
  $_SESSION['brought'] = $product[$boxID] += 1;
  echo $_SESSION['brought'];
  }
But I'm stuck here. My brain just can't seem to understand how it's going to work from here. Can somebody point me in the right direction please?

Stephen,

Posted: Sat Oct 07, 2006 9:34 am
by impulse()
Hala-yewua!

Working code as follows:

Code: Select all

if (isset($_REQUEST['add'])) { # Add to cart
  session_start();

  $quantity = $_REQUEST['quantity'];
  $boxID = $_REQUEST['boxID'];

  $_SESSION['quantity'][$boxID] += $quantity;



  $q = mysql_query("SELECT * FROM products WHERE pType='books'");
  $numRows = mysql_numrows($q);



  for ($i = 1; $i < $numRows; $i++) {

    echo "Item <b>$i</b> has<b> ", $_SESSION['quantity'][$i], "</b>items<br>";
    }



  }
I seem to be able to work problems out easier when I post the problem on here, even if I don't get a reply :) I slowly have to walk through it all in my head on here to explain the problem and how I want to get around it and usually puts everything into perspective for me, allowing me to work out what I need to do :)

Posted: Sat Oct 07, 2006 9:36 am
by aaronhall
Sounds like you're all tangled up. The best way to approach this is to keep a table of all of the items in all of the users' carts. Here's a sample layout:

Code: Select all

create table userCartItems {
id int(11) auto_increment NOT NULL,
userID int(11) NOT NULL, ###this could also be the session ID if the user isn't signing up
itemID int(11) NOT NULL,
PRIMARY KEY (ID)
}
Everytime a user adds something into the cart, just add a row to the table using INSERT. Whenever you need to retrieve the list of items, just use something like:

Code: Select all

<?
$result = mysql_query("SELECT * FROM books INNER JOIN userCartItems ON books.id = userCartItems.itemID WHERE userCartItems.userID = '" . session_id() . "'");
?>
This would select all of the information from the books table corresponding with the books that have been added to the user's cart.

Posted: Sat Oct 07, 2006 9:44 am
by impulse()
I've never used sessions on MySQL DBs, I didn't know it could be done.
What happens? Does it leave temporary data on a database and then delete the rows once they're unused?

Does the session handle which DB row belong to which user?

Posted: Sat Oct 07, 2006 9:48 am
by aaronhall
Actually, all the session is doing is keeping track of the user between page request. PHP will automatically assign a unique ID to the user (available via the session_id() function). Then, instead of trying to track the user's shopping cart items in the $_SESSION array, you're just placing all of that information in the database. It works much nicer this way because, after all, you're trying to fetch other database-stored information based on his cart information. Does that make sense?

Posted: Sat Oct 07, 2006 10:02 am
by impulse()
I understand what you mean in terms of how the server's handling data.
I'm not sure what you mean by "nicer" though :)

Is using MySQL sessions more efficient than using PHP sessions?

Posted: Sat Oct 07, 2006 10:44 am
by nickvd
You're not really using the database for session data, you're using it for shopping cart data... the use of session_id() is purely for identifying the user whether they're logged in or not...