By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

ForEach question

P: 40
Hi everyone,

I have a shopping cart array consisting of the following:

$pd_id[] // Product ID
$qty[] // Quantity
$opta[] // Option A
$optb[] // Option B
$optc[] // Option C

When the final checkout button is pressed, I am going to insert these fields into my order table (tbl_order) (along with a bunch of other name/address stuff...)

The problem I'm having is that the above fields are built as an array. There can be anywhere from 1 to 10 items in the shopping cart, and I want to insert them into the table as follows (there are 5 fields multiplied by 10 possible items, so my table has 50 potential fields for this info):

pd_id1
qty1
opta1
optb1
optc2
pd_id2
qty2
opta2
optb2
optc2
...etc...etc... all the way to pd_id10.

Assuming I already have the above array completely populated, how exactly do you write the ForEach loop that would generate the $sql insert? Or should I just loop from 1 through 10 and do five inserts for each of them? (In this case, won't I have to initialize null values for numbers NOT in the shopping cart?)

Just wanted some advice on the best way to approach this. I always seem to do things the hardest possible way... and then one of you guys comes along and asks me "why didn't you just do THIS?" and provides me with the *correct* way that I should've tried in the first place. :)

Thanks in advance!
Dec 17 '07 #1
Share this Question
Share on Google+
2 Replies


mwasif
Expert 100+
P: 801
There can be anywhere from 1 to 10 items in the shopping cart, and I want to insert them into the table as follows (there are 5 fields multiplied by 10 possible items, so my table has 50 potential fields for this info):
pd_id1
qty1
opta1
optb1
optb2
pd_id2
qty2
opta2
optb2
optc2
...etc...etc... all the way to pd_id10.
What about having only these fields rather than creating each set of columns of each product.

pd_id, qty, opta, optb
having the values like

1, 10, some value, some value
45,3, some value, some value
Dec 17 '07 #2

mwasif
Expert 100+
P: 801
Moved to PHP Forum.
Dec 17 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.