ForEach question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vozzek
    New Member
    • Sep 2007
    • 40

    ForEach question

    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!
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    Originally posted by vozzek
    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

    Comment

    • mwasif
      Recognized Expert Contributor
      • Jul 2006
      • 802

      #3
      Moved to PHP Forum.

      Comment

      Working...