-2

I was wondering if it's possible to do something like:

for ($d=0; $d<20; $d++) {
  $productname.$d = $_POST['productname'.$d];
  $link.$d = $_POST['link'.$d];
  $color.$d = $_POST['color'.$d];
  $size.$d = $_POST['size'.$d];
  $otherinfo.$d = $_POST['otherinfo'.$d];
  $no.$d = $_POST['no'.$d];
  $other.$d = $_POST['other'.$d];
}

since the code above doesn't work.

What am I doing wrong? Any help is appreciated.

zavg
  • 10,351
  • 4
  • 44
  • 67
Claudio Delgado
  • 2,239
  • 7
  • 20
  • 27
  • 7
    You want to use ***arrays!*** Really, use arrays. Use them directly in your form ` – deceze Oct 26 '14 at 13:49
  • @deceze Could you elaborate? If I had any idea I would have done it myself, hence the question. Please guide me on how to populate an array from 0 to 20 for these if possible. It would be greatly appreciated. – Claudio Delgado Oct 26 '14 at 13:51
  • What are you even trying to do? Show more code. How are you planning on accessing those variables? – I wrestled a bear once. Oct 26 '14 at 13:55
  • @Adelphia I have all the code ready, it IS working, it can be accessed. it was all manually added for 5 of each of these, so now that I plan to make the code for 20 inputs, I have decided to use more efficient coding. – Claudio Delgado Oct 26 '14 at 13:57
  • 2
    @ClaudioDelgado If you name your inputs with brackets `productname[]` instead of `productname0`, `productname1`, `productname2` etc., then they'll be accessible as a regular array through `$_POST['productname']` (eg. `$_POST['productname'][0]`. You can also loop through this array as any other array) – h2ooooooo Oct 26 '14 at 13:59
  • 1
    Try to name your inputs `name="product[0][color]"`, `product[0][name]`, `product[1][color]` etc. Then `var_dump($_POST)`. Voila, a sane data structure without any hassle. – deceze Oct 26 '14 at 13:59
  • @ClaudioDelgado You might have it, but we don't, so post it. – I wrestled a bear once. Oct 26 '14 at 14:04
  • For the question part of yours *"What am I doing wrong?"* and as of *"Any help is appreciated."* : I've closed your question against existing Q&A material which shows how you can obtain error messages from PHP. If you enable those to the highest level you will get notices and warnings about your code which do show what and where you're doing something wrong. If you have problems to understand those notices, we also have a reference on site for quick first help: [Reference - What does this error mean in PHP?](http://stackoverflow.com/q/12769982/367456). http://stackoverflow.com/help/how-to-ask – hakre Oct 26 '14 at 14:09

1 Answers1

1

Your $productname.$d statement has no sense.

Use $productname[$d] instead. It is array approach and it is much more preferable.

P.S.: If you really want so many different variables you can use variable variables (pseudocode is below):

$varName = 'productname'.$d;
$$varName =  $_POST['productname'.$d];;
zavg
  • 10,351
  • 4
  • 44
  • 67