Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-11-2005, 06:59 PM   #1 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
Order Multiple Items

Hi all,

Last time I was here i recieved great advice, Ive searched the forums for similar topics but the results were mainly regarding multiple sorts.

Heres my delimma:

I have a php site which displays products for ordering. When a customer orders them it has nothing to do with credit card details etc its only a order request that gets recorded - eg. productid, quantity, productsize....

My products are in several different categories. A customer selects a category and all the related products a shown... select * from product where category = "Tshirts" for example.

Then I have my while loop display all the available tshirts and their details. Then comes the order part.

This is what im trying to achieve:

Each product with have a tick box next to it for placing a order... a customer could tick 3 of the 5 items then select the perfered size from the dropdown HTML box. Then for each item they would enter in the quantity. Then click the 'order selected items' option.

I then want my code to insert each item (productid, customerid) into the order table... so in this case it would insert each different productid (all 3) individually into the order table. Each record will have a order number that is the same to link them all orderid I guess.

How do I tackle this? I have been told to have a orderitem table (with the quantity field in it) as a junction between the product and orders table so I will need certain joins i guess.

How do I just pick out the items that have had a tick box ticked?

I will proberly need to explain this further but for now I will leave it at this incase someone can direct me to a thread that has dealt with multiple order items/quantity etc.

I have code showing the query but that doesn't really relate to what im trying to do as its just a normal query.

All suggestions appreciated.... I looked at oscommerce but its far to complex for what Im trying to do.... a far smaller scale!

Thanks
BUFFY is offline   Reply With Quote
Old 10-11-2005, 07:12 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
well this is a post with a very large scope to it. it is a lot easier to answer very specific questions 1 at a time.

Quote:
how do i tackle this ...
i think an orderitems table is a good idea.

Quote:
how do i just pick out the items that have had a tick box ticked
we need to see the html code you use to generate the tick box.

i'm a bit confused because up at the top you say you already have a site that takes product, quantity, and size... yet you're asking how to do it? are you adding more functionality to it? probably need to be more specific.
__________________
Mike
sde is offline   Reply With Quote
Old 10-11-2005, 07:48 PM   #3 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
Ok i'll load what ive got so far (not working) onto my tech server.

When I say I have a site, I mean i have a php page which displays products, that was simple enough.... and I have a idea of how to tackle multiple orders.

(my last site dealt with one product item per order so it was just inserting fields into the order table, whereas this one I need help on).

Temp site here: http://www.bcs.net.nz/~annah/ahidesigns/products.php

I have loaded a couple of pages to this address, click quality prints. Then there are multiple includes for the different categories in the productdetails.php page.

Since you clicked quality prints, that page will be included so the address should be products.php?qualityprints=yes.

Did you want my query code? Just ignore the broken images as I have to change my pages as the query isn't working for some reason there.

The design is "in process"


As you can see, the size, quantity and tickbox are just HTML code that I will insert into my order table. The price depends on the size and quantity so will do some if statements there.

My question is...

How do I make it so that only the products that have the tickbox selected are inserted into the order table, how do I use the orderitem juntion table with the quanity in it for each item ordered?

Hopefully it makes more sense to you know, Im not doing a good job of explaining it because Im not sure my idea on how to tackle it is right.
BUFFY is offline   Reply With Quote
Old 10-11-2005, 07:58 PM   #4 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
Tables

Product table
ProductID
ProductName
ProductCode
ProductCategory
......

OrderItem table
OrderItemID
OrderID_FK
ProductID_FK
ItemQuantity

Order table
OrderID
OrderCustomerID
OrderProductID
OrderDateOrdered
OrderAuthorisedBy
OrderCustomerComments
OrderEmployeeComments
OrderStatus
....


Then I have a discount table for certain categories, but thats another story
BUFFY is offline   Reply With Quote
Old 10-11-2005, 08:20 PM   #5 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
For your SQL,
I would have a customer_order table that had most of the fields from your Order table except productID. Then I would have an ordered_items table that had the details of a particular item that was ordered and an OrderID. So to figure out what items a particular order contained you would use an SQL statement like
Code:
SELECT * FROM Ordered_Items WHERE OrderID = 10
That way each item a user orders will be in a seperate row in Ordered_ITems but the entire Order can be referenced easily.

For the PHP,
I could be wrong but I think each tickbox in the select form returns a true/false value as to whether it is ticked, that you can use with a loop to add items.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 10-11-2005, 09:10 PM   #6 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
Thanks for the replies...

So what your saying is... For SQL

Display products from the products table....

When a customer clicks the order button, insert productid, size and quantity into ordered_items table.
Retrieve orderitemID from the previously entered record, enter this (orderitemID) and the date, comments etc into the customer_order table.

Is that what you meant?


*Ive just realised my layout doesn't allow a customer to order 1 large and 2 meduims, just 3 of the same size. I think javascript will be able to change the amount of size boxes on the fly once quantity has been entered.

Last edited by BUFFY; 10-11-2005 at 10:11 PM.
BUFFY is offline   Reply With Quote
Old 10-11-2005, 09:22 PM   #7 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Almost - when a user submits an order you store the name, address, etc in a new line in the customer_order table and then store the individual items and quantities in the ordered_items table with a reference to the newly generated entry in the customer_order table. Basically this avoids having to store any sort of list of products in the customer_order table.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 10-19-2005, 06:44 PM   #8 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
Ok after playing around with this suggestion and numerous others I have a solution that should (in theory) work.

So I have a page listing all the products, next to each item I now have one quantity box, one size box and a add to cart button.

When a customer clicks the add to cart button the form has the ProductID in its name, so when the page refreshes if ProductID > 0 then it carries out the following:

1)
First it checks if they have already clicked add to cart before by checking if a OrderID has been assigned, if its zero I create a new record with 0 as a CustomerID for now, then get the newly assigned OrderID.

2) Then I insert the order information (quantity, size) into the OrderItem table, just ignore the total, subtotal as that isn't done yet.

Here is my code for the insert part, if i comment it out the page loads, and if I comment it out and echo ProductID above it, its always nothing even when the 'Add to Cart' button has been clicked.

PHP Code:
<?PHP
session_start
();
//connect to DB
require  'inc/connect.inc.php';

//check if add to cart has been pressed
if ($_REQUEST['ProductID'] == 0
{
// check if order exists
if ($_SESSION['OrderID'] >0) {
    
// check to make sure there is a DB record
    
$sqlcount="SELECT count(*) thecount FROM Orders WHERE OrderID = $_SESSION['OrderID']";
    
$resultcount=mysql_query($sqlcount);
    
$row=mysql_fetch_array();
    if (
$row[0]==0) {
        
// if not create it - insert into Orders  (OrderID,  etc) values ();
        
$sqlcreate="INSERT INTO Orders(CustomerID_FK) values (0)";
        
$resultcreate=mysql_query($sqlcreate);
        
// get new OrderID and save in $_SESSION['OrderID']
        
$_SESSION['OrderID']=mysql_insert_id();
     }
} else {
    
$sqlcreate2="Insert INTO Orders(CustomerID_FK) values (0)";
    
$resultcreate2=mysql_query($sqlcreate2);
    
// get new OrderID and save in $_SESSION['OrderID']
    
$_SESSION['OrderID']=mysql_insert_id();
}

//For new PHP, used to make data safe before sending a query to MySQL,
//prevents SQL Injection into the input fields
//Had to change from PHP5 expressions to suit PHP4 server
$quantity=mysql_escape_string($_POST['quantity']);
$size=mysql_escape_string($_POST['size']);
$price=mysql_escape_string($_POST['price']);
$subtotal=mysql_escape_string($_POST['subtotal']);
$total=mysql_escape_string($_POST['total']);

// add order item
$sqlinsert="Insert INTO OrderItem SET,
OrderID_FK = " 
$_SESSION['OrderID'] . ",
ProductID_FK = " 
$_REQUEST['ProductID'] . ",
OrderItemQuantity = '$Quantity',
OrderItemSize = '$Size',
OrderItemUnitprice = '$Price',
OrderItemSubTotal = '$Subtotal',
OrderItemTotal = '$Total'"
;

$resultinsert=mysql_query($sqlinsert);

//end request productid
?>
Heres an extract from part of my form:
PHP Code:
<form name="<?php echo 'order'.$ProductID?>" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type=hidden name=ProductID value=<?PHP echo $ProductID;?>>
<table width="100%"  border="0" cellspacing="0" cellpadding="5">
    <tr>
        <td class="text2">Quantity</td>
        <td><select name="Quantity">
          <option value=1 selected>1</option>
          <option value=2>2</option>
          <option value=3>3</option>
        <option value=4>4</option>
          <option value=5>5</option>
        </select ></td>
        </tr>
        <tr>
        <td class="text2">Size</td>
        <td><select name="Size">
          <option value="A1" selected>A1</option>
          <option value="A2">A2</option>
          <option value="A3">A3</option>
        </select ></td>
        </tr>
        <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        </tr>
        <tr>
        <td colspan="2"><div align="center">
        <input name="submit" type="submit" value="Add to Cart">
        </div></td>
        </tr>
        </table>
        </form>
At the moment, the ProductID isn't working.... being assigned, whatever.

I also want to display a message beside each product saying that have already ordered one/two sizes of the product, with the option to add another like normal - size,quantity boxes.

I will then have a Show Cart page that displays the OrderItem table records for the OrderID used, and delete them if they choose not to login and save the order request.

Im not sure if Ive made this clear or posted enough code, but if anyone understands what im trying to achieve and can point out what I need in my code, why that would just be splendid

Thankyou

Last edited by BUFFY; 10-20-2005 at 03:02 AM.
BUFFY is offline   Reply With Quote
Old 10-19-2005, 09:58 PM   #9 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Should be <?php echo 'order'.$ProductID;
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 10-20-2005, 02:23 AM   #10 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
Thanks for pointing that out, though the main reason why its not loading I think is in the first section of the php code because when I comment that out the page loads.

Im not even sure if the logic is right either, or if Ive missed something...
BUFFY is offline   Reply With Quote
Old 10-20-2005, 02:44 AM   #11 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Sorry, I read quickly and missed the part about the page not loading. My correction had to do with your statement that ProductID was not being set. It wasn't being set because 'order'+$ProductId will always yield zero.

I don't see anything that should make the page fail, but if ProductID is not zero, I don't see anything that would be output either. Does there need to be paranthesis around that if statement as well?

EDIT: Wait a minute. Do you have error reporting turned off? I think calling mysql_fetch_array without the argument $resultcount should throw an error like "NULL is not a valid mysql result set" or something like that.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 10-20-2005, 03:13 AM   #12 (permalink)
BUFFY
Registered User
 
Join Date: May 2005
Posts: 29
BUFFY is on a distinguished road
Ive got in my php.ini file:
error_reporting = E_ALL & ~E_NOTICE
display_errors = Off

At the moment it is just throwing up a blank page, no errors.

I added brackets to the REQUEST statement, and changed one of the CustomerID to CustomerID_FK, what it should have been. But still nothing....

So when i comment out the php code at the start and go echo $_REQUEST['ProductID']; and press the 'Add to Cart' button, the page refreshes showing the products as usual, but it displays nothing as its ProductID value.

echo "Request " . $_REQUEST['ProductID'];
echo "<br/>ProductID " . $ProductID;

Both show nothing, when I thought I was assigning them on the form.
BUFFY is offline   Reply With Quote
Old 10-20-2005, 08:14 AM   #13 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
why is display_errors off? if you are having trouble with code, you should definately have that on.

there is probably an error and if you turn it on then there will be text indicating which line your error is on.
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beware the multiple option shopping cart . . . metazai PHP 9 08-31-2005 08:05 AM
Order by greater of two date columns Wysocki Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 4 02-07-2005 05:10 PM
someone please help kickerman97 Java 3 10-19-2004 03:19 PM
Using ORDER BY in a select statement Epsilon PHP 3 08-22-2004 02:28 PM
Search -That Can Match Out Of Order Items JBurke All Other Coding Languages 11 09-25-2003 02:09 PM


All times are GMT -8. The time now is 11:36 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting