views:

256

answers:

4

I'm at a crossroads, not exactly sure what is better to use. Right now I'm using sessions arrays to store calculations on the fly, but I need to switch to objects because I would like have some functions. But I was also considering using ajax and insert the data on the fly back and forth to the database but I'm worried it might be to many calculations under heavy trafic.

What i'm trying to do is have a cart were items are added to, all of the items in the cart will need to be recalculated if a change to a quantity field is made so I was wondering which is the better solution, to use objects and sessions or update a large table in the db with multiple users manipulating the data.

I'm using mysql db if that helps in the decision..

A: 

I create a cart table which stores a SESSION ID and the corresponding item id.

The session id would be used to associate the browsing user. Calculating the price would be as easy as retrieving all rows with the users session id and summing the price from the item table (for example). Any changes to the cart involve a simple insert or delete for each item.

IllusivePro
what do you mean by a session table? can you give me a small example? I am using arrays atm.
payling
+1  A: 

The session should be much faster, but it will not persist if the user closes their browser. I use sessions for that sort of thing (shopping carts, last accessed searches, etc) most of the time. On a site with a medium-large number of users, you'll need to do maintenance on a table to clean out old session information. That's too much of a headache for me unless I really need the data to be there days later.

Scott Saunders
I was concered that having tons of different sessions would cause bandwidth problems, this is not true?
payling
The contents of the session aren't downloaded/uploaded, only the session id. The contents of the session stay on the server and are only accessible by server side code. Sessions are much, much lower impact than DB connections/accesses.
Scott Saunders
What do you use to hold data in sessions for something such as a cart? Do you think a class would be the best solution or is there something like a table like a db i could use?
payling
You have to use regular php variables: objects, strings, arrays, etc. I use whatever is appropriate for what I want to store. For shopping carts, I have a Shopping Cart class that has methods for finding totals and adding/removing Items, etc. Arrays for other data. Simple strings or integers for other data. The only thing 'like a table' would be an associative array.
Scott Saunders
A: 

For doing many different calculations, I'd go with session... This really is the most bandwidth friendly way. If you would like to persist their cart without using cookies, then you can store their cart in the database when they leave and reload it when they come back (store the cart based on userID obviously).

Polaris878
A: 

Well keep in mind that sessions themselves can be stored in the database. So regardless of what you choose, you shouldn't try to roll your own database-based sessions.

Keep in mind though that standard PHP sessions store the session data on disc. If you have many users on your site with many sessions over it's possible that you could bog down your box with too much disc i/o. This would be less of a problem if you had an SSD drive, however.

Another option I've used before is to simply have a MySQL database on the same box as the server, and store the data in a memory only table. I think this is the best of both worlds:

  • It's faster than a traditional myisam or innodb database because it doesn't ever get dumped to disk (this mostly only affects session writes)

  • Assuming your database is on a different box than your web server, it's faster because your session db is on the same box.

  • You can perform some sql wizardry on your session store if you need (can't do this with files)

ryeguy