Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

php shopping cart, database or session?

session

  • Please log in to reply
5 replies to this topic

#1 Jacko

Jacko

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 78 posts
  • Learning:C, Java, C++, PHP

Posted 18 October 2009 - 07:10 AM

Hi,
I'm programming a shopping cart with php but i'm not sure if it's better to use session or database to save the items in the cart (cart is a class in my code). What do you advise me? Maybe the sessions are better because a guest can also put his items in his cart...

Thanks in advance... bye...^^
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 18 October 2009 - 10:21 AM

I would store data in the database. Sessions will still be important, however, for managing the database access.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Jacko

Jacko

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 78 posts
  • Learning:C, Java, C++, PHP

Posted 18 October 2009 - 11:15 AM

This is my actual Cart:
class Cart
{
private $items;

public function __construct()
{
if(!isset($_SESSION['items']))
$this->items = array();
else
$this->items = $_SESSION['items'];
}

public function __destruct()
{
$_SESSION['items'] = $this->items;
}

public function add_item($id, $num)
{
if($num > 0)
$this->items[$id] += $num;
else
$this->items[$id] = 0;
}

public function show_items()
{
return $this->items;
}
}

I think that the sessions are more convenient because a guest can fill the cart while he is unregistered.. What do you think?
  • 0

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 18 October 2009 - 01:04 PM

To check out, I would still want the person to be registered.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 gakattack

gakattack

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 18 October 2009 - 01:06 PM

You could always make it session only if the user isn't logged in, and database-based otherwise. I assume they need to register at some point though to purchase something, at that point you can throw it into a database from the session variables.

If you're selling stuff online; it's always good to have records!
Well, that's what I would do.
  • 0

#6 gakattack

gakattack

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 18 October 2009 - 01:10 PM

I could be wrong, but I think a properly designed database would be easier on the server than loads of session variables per user.
  • 0





Also tagged with one or more of these keywords: session

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download