Solved

Creating dynamic items to add to cart

Posted on 2014-10-17
7
114 Views
Last Modified: 2014-12-29
I am creating a "shopping cart" for my site. It really isn't a cart since there is nothing being purchased. It is more of a function to gather order details.

The product needs to be assembled in the order form and then added to the cart. Most of the stock/pre-made carts I have looked at out there all involve pulling an item from a database and adding it to the cart. Since every order is custom, I need to be able to dynamically build a product before adding it to the cart.

I was thinking about building this into the database I am using, building the order and putting each dynamic item in the cart via the database. I want to eventually get there, but not yet.

Does anyone have something quick and dirty in mind that could help shortcut through this without putting the order into a db? An example would help.
0
Comment
Question by:RaneCloud
  • 4
7 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40387496
I think you would need to store the order details somewhere, and a database is exactly what we would use for this purpose.  Any reason why you wouldn't want to use one?
0
 
LVL 4

Accepted Solution

by:
Jernej Navotnik earned 500 total points
ID: 40410184
Dear RaneCloud,

as Ray Paseur already stated, databases are the "man" for the job.
But you could use native PHP sessions I suppose that store data in memory (keep in mind the session typically lasts only as long as the browser is not closed)...

Here is a really basic example (page1.php):
<?php
// Start the session
session_start();
?>
<html>
<body>
<?php
// Set session variables
$_SESSION["product"] = "car";
$_SESSION["color"] = "red";
echo "Session variables are set.";
?>
</body>
</html>

Open in new window


To get those stored variables (page2.php):
<?php
session_start();
?>
<html>
<body>
<?php
// Echo session variables that were set on previous page
echo "The product was: " . $_SESSION["product"] . ".<br>";
echo "The color was:  " . $_SESSION["color"] . ".";
//Or to display all session data:
print_r($_SESSION);
?>
</body>
</html>

Open in new window


Keep in mind, this is not the "best" way to go (to each his own I say), but it can be done...
You could also use normal files, but using these would be more complicated than a database.

Best regards!
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40411656
If you decide that the PHP session is the way you want to store this information, you will want to know exactly how PHP sessions work.  It's not always intuitive and it can result in loss of data.  This article can help a bit.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 58

Expert Comment

by:Gary
ID: 40520713
I've requested that this question be closed as follows:

Accepted answer: 500 points for Jernej Navotnik's comment #a40410184

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40520714
A shopping cart that uses the PHP session without a data base to back it up is a technically incompetent solution.  It can result in a loss of client data.  It would be better for E-E to delete this question than to keep this as an example of a "good idea."
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40522800
Gary:  Please don't mischaracterize my concern -- I don't think the question is "stupid," as you say, and I never said anything like that.  It's a good question!  It's a question that many of us have dealt with over the years.  I think the accepted solution does not represent something any professional would choose and it does not belong where it can be discovered and ascribed to Experts-Exchange.

But as I wrote, if you feel strongly that this is a good answer, then I'm more than willing to listen to your side.   Personally, I could not defend this approach.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now