473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

ForEach question

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
2 1062
mwasif
802 Expert 512MB
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
802 Expert 512MB
Moved to PHP Forum.
Dec 17 '07 #3

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

Similar topics

32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
13
by: cody | last post by:
foreach does implicitly cast every object in the collection to the specified taget type without warning. Without generics this behaviour had the advantage of less typing for us since casting was...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
8
by: Brad Wood | last post by:
When I do this: foreach( Button btn in myForm.Controls ) An exception is raised when no buttons exist. I would simply expect execution to continue after the foreach loop (just as would be the...
13
by: Michael Brown | last post by:
Hi there. Can anybody comment on the efficiency of the following technique: foreach (SomeItem Item in SomeNativeDotNet.Collection) { // Whatever } My concern is that the "Collection"...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
9
by: news.microsoft.com | last post by:
I am looping through an iteration and I would like to test the next item but if its not the one that I want how do I put it back so that when my foreach continues it is in the next iteration? ...
9
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return...
7
by: Tony Johansson | last post by:
Hello! Why does not this cause a compile error because a ulong has never been implicitly convertible to byte? ulong vektor = {100000,200000,300000}; foreach(byte b in vektor) {...
1
by: greyseal96 | last post by:
Hi, I am a pretty new programmer, so I apologize in andvance if this is a dumb question... In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.