php - Increment a variable name in for loop -


I have a variable that I want to set based on some submitted form data. On the form I have a set of fields which can increase the number of user numbers (i.e. they can add more to the same type of fields), and therefore I need to set this script which handles submitted data is.

So far I have it:

  while ($ i & lt; = $ totalproducts) {$ product. $ I = $ _POST ['Product' $ I]; $ I ++; } $ Totalproducts holds the total number of areas As an example, let me assume that I have 3 items, I want to get it from the code:  < Pre>  $ product1 = $ _POST ['product1']; $ Product2 = $ _POST ['product2']; $ Product3 = $ _POST ['product3'];  

I'm sure that I am near, just can not do it together slots ...

You can use:

  while ($ i & lt; = $ totalproducts) {$ variable = 'Product' $ I; $$ variable = $ _POST ['product' $ I]; $ I ++; }  

But better use will be ARAR:

  $ products = array (); While ($ i & lt; = $ totalproducts) {$ product [$ i] = $ _POST ['product' $ I]; $ I ++; }  

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -