Page 1 of 1

Some webshop building questions/problems

Posted: Thu Mar 20, 2003 7:35 pm
by Erik van Leeuwen
Dear PHP friend,

One month ago I started (without zero knowledge) to learn HTML, PHP and MySQL to develop my webshop. For now I am able to create the MySQL db and producttables and produce the product overview on the product PHP pages. But now, after several hundreds of hours, reading dozens of manuals and hundreds of sample scripts, I am stuck, and can’t manage it on my own.
Following my most important problems:

On every product row, I have a “Add to basket” link. How can I manage to add a product to the “Shopping basket” page? Automatic total amount counting would be the next challenge for me I think. But…..one step at the time…..
Or is the solution something like this: When a visitor clicks an “Add” link, the product will be inserted to a (separate) table? So it takes two extra db actions. (1)Database total product overview – (2)Product selection insert into different db table – (3)Visitors choice selection overview from database.

To stick order x to visitor x, I have to make use of sessions, I understood. It seems to me that I have to start the session on my HTML product links page, and have to make sure that they maintain on the several product PHP pages. As far as I found out I can do this by calling a session_start followed by a session_register command on the HTML product page, and just use session_start on the other (PHP) pages. Am I correct? In all those manuals and sample scripts I saw this used with user/password logins. But I don’t want to bother visitors with this. Only on the last (order) page. But that’s a bit late to start a session I think… ;-)
Do I have to save such sessions in a db table? Or wil just the “start and register” command on the first page, and the “register” command on the other pages, be enough to maintain such a session during a browser session?

Some facts:
The server makes use of PHP version 4.1.0
Product table: article
Id INT(11) NOT NULL auto increment primary key
Name TEXT NOT NULL
Descr TEXT NOT NULL
Price FLOAT(5,2) NOT NULL Standaardwaarde 0.00

To give you an impression of what I have so far:
http://members.lycos.nl/iao/produkten.htm

Ok…thank you so much for reading this long ( I wanted to explain my ideas and problems very clearly to you) article. I hope there is somebody willing to help me to concquer these bottlenecks with some specific script-code/tips/tricks. Because of the length of this message I didn’t post the script I use. Please let me know if this is desired.

Thanks in advance,

Erik van Leeuwen

Posted: Fri Mar 21, 2003 12:55 am
by Ebula
Your Idea about the session is correct. Depending on the Complexity of your shop I would either store the Shopping basket in a temp. Cookie (using setcookie() without setting an expire time) or creating a kind of temp. table in your db which is linked by an ID to the session of the User.
You would just insert the ID of the product bought in to the table/cookie. By using this uniqui ID you can the get all of the produkt info out of the db once you need it, e.g. adding all products prices together (i would use sql to add the products together, saves a lot of SELECTs and for/while ).
I have noticed that on your page, when you want to add a product to the shoping cart, what info do you give the add script?

Some webshop building questions/problems

Posted: Fri Mar 21, 2003 7:07 am
by Erik van Leeuwen
Hi Ebula,

Thanks for your reply.
I am less able to translate shoppingcart concepts into specific PHP/MySQL code than you think, I'm afraid. The scripts I have so far, have been made by cut and paste from several manuals and scripts examples, and with trial and error and a bit of my own knowledge trying to get it running.
Depending on the complexity.....
I think my idea is straightforward: Several product overview pages with "Add to basket" link - Shopping basket page with order overview and (total) price.
I don't want to make use of creditcard/shipping options etc. Customers can transfer the total amount after they recieved their order.
So very specific tips/script code would be very very helpfull. e.g.:
Something (incomplete) code like:
<?php
if (!isset($_SESSION)) {
session_start();
}
?>
And how to use it. Put in on every page on top?
And something about the "Add to basket" link. How to build such a link, what code/variables have to be in it? Something like:
<?php echo('<p><a href="' . $_SERVER['PHP_SELF'] .'?add=1">aan winkelmandje</a></p>'); ?> ?????
I know it's totally incorrect...but that's why I'm here.....
And do I have to put the product page info in a FORM.....method GET format, to get the order info on the "shopping basket" page? And do I need the same GET format on this "shopping basket" page to retrieve this order information? And how does such scriptcode look like (roughly)?
Ok, thanks again, and I like to here some solutions if possible.

Kind regards,

Erik

Posted: Tue Mar 25, 2003 6:01 am
by Ebula
I am not gonna start to write the shop for you, since then you are never gonna learn, but you should try following concept:

For the table in your DB where you store the product info, you should assign a unique id to every product. Using this id you can later on easiely access the info about the different products.
You should create a Table that temp. stores the shopping crate, with one field for the products and one which will hold the user ID.
When the user opens your page, you should start a session, and create a unique id for the user which you store in the $_SESSION variable. (Use uniqid(rand()); to create the id). Then create a new entry in your shopping crate table with the users unique id in it. You can now always refer to this entry using the session you started for the user.
Now, for the button "add to shopping basket" use the hyperlink to pass on the id of the product the user wants to buy to a script, where you write this id into the above mentioned new entry in your shopping crate table. Use the CONCAT() function in SQL to add more and more to the shopping crate entry, so that you get a string shomething like this (23,454,22,545,65) in that table. Using the explode function you can always turn this string into an array and sort the products, use this to query your products table for the price etc.
You should be able to find out what I mean by using the PHP and MySQL manual.
Otherwise just write back.

Posted: Wed Mar 26, 2003 7:21 am
by Erik van Leeuwen
Thanks. Of course it was not my intention to let you write it for me. Just some specific clue's to try. And so you did: (Use uniqid(rand()); and the CONCAT() function in SQL for example. I will try to figure this out and see how it works.

Thanks again and my regards.

Erik