Solved

Creating dynamic items to add to cart

Posted on 2014-10-17
7
119 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 109

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 109

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 109

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 109

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Insert won't work due to space in column name 5 21
php extract($_REQUEST) 5 49
Presenting past 6 months bug 7 29
PHP AJAX JSON 2 61
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

773 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