Help developing cart

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mynotaur
Forum Newbie
Posts: 1
Joined: Thu Nov 06, 2008 7:06 am

Help developing cart

Post by Mynotaur »

Hi all,

Im new to PHP and sql and so I am using a premade simple php cart:

http://www.thewatchmakerproject.com/jou ... pping-cart

I am beggining to understand how it works but I need to improve on the existing cart, within the various part of code I insert the following into my site to select all the entries in my database:

Code: Select all

            <?php
            $sql = 'SELECT * FROM salads ORDER BY id';
            $result = $db->query($sql);
            $output[] = '<ul>';
            while ($row = $result->fetch()) {
            $output[] = '<li>'.$row['name'].': &pound;'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
            }
            $output[] = '</ul>';
            echo join('',$output);
            ?>
 
This then prodces a link which when clicked passes the id number of the particular item to the cart.php page:

Code: Select all

<a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a>
I have a couple of questions, firstly what is the above function called so I can read further into it to help my understanding.

Secondly would it be possible to send the id as well as the corresponding price information so that I could have a particular item with three prices, in this case three different sizes of pizza, so that the cart.php page would add the correct price for the correct size without having to list three seperate database entries for one type of pizza.

Please forgive me if this is a newbie question but I am still fairly new to Php and sql.


Any help is much appreciated. Thanks in advance.
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: Help developing cart

Post by sparrrow »

Not sure about your first question. It's a WHILE loop that steps through each row in your query result. Each run through it makes $result an array containing keys and values that match the columns and data for each row. It stops when there's no more rows. What it prints is a hyperlink with variables defined in the URL. These variables are passed in what is known as a GET method. The cart will reference $_GET['id'], which plucks the value right out of the web address and uses it in the code.

It is highly recommended that you pull your prices directly out of your password protected database each and every time. Headers and POST data can be spoofed, and you could have someone submit an order for 1000 pizzas at 1 penny each, then complain when you don't fulfill the order. That may be an extreme example, but nonetheless a real life possibility.

Depending on the nature of all the products in your database, there are a number of clever ways to build your database to make this work for you. One way would be a SIZE table. In here, define size_id=1, size_name='small', size_price='10.00'....etc etc. Then you can pass your product ID and your size ID and pull the correct price and other data securely out of your database.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Help developing cart

Post by Stryks »

The database approach described below is a good idea. You should avoid passing anything but an ID to identify the item. That way, all a malicious / curious user can do is add another (possibly non-existent) product.

The two table schema is probably not the way I'd go, though close. I'd have a table for items:
item_id
item_name
item_description
etc ...

A table for sizes
size_id
size_name
etc ...

And a table for prices
item_id
size_id
cost

The two table approach only works if all sizes are the same cost regardless of the product. This three table approach means that there is an unlimited and independant number of products and sizes, that are intersected with the price table.

So ... you could select a specific item and size cost with ...

Code: Select all

SELECT cost FROM tbl_item_price WHERE item_id = x AND size_id = y
It can take a bit of thought to set it up ... making sure that each product gets prices made for each size on creation ... each size gets a price made for each new product .... etc. Deletes are less problematic, you just need to remember to delete all instances of a size or item from both tables. Or you can use innoDB tables and set up a relationship to take care of this for you.

But, as with all things ... the solution that fits best will be dictated by what your specific requirements are.

Cheers
Post Reply