0

I have a long list of variables with a number at the end. For example address1 and I have all the way up top address14. I want to post these from a form but rather than type out $address1 = $_POST[address1] I would like to create a loop that loops round 14 times and changes the number at the end of address in both the variable name and in the $_POST section...

I am struggling to do this. I have a loop that creates the varaibles, but I keep getting errors as it isn't doing the $_POST bit.

Can someone please assist? Thank you.

This is what I currently have:

        $x = 0;
        while($x < 14) {
            $address = "address" . $x;
            $address = $$address;

            $string = "<p>Address$x:" . $address[0] . "</p>";
            echo $string;
            $x = $x + 1;

}

Ade
  • 3
  • 1
  • 3
  • what do you mean by the $_POST - bit? You have no form? Are you trying to create a form with names and adress that user has to type in? And then you want to get these values with $_POST. Is that what you want? – bestprogrammerintheworld Apr 20 '13 at 14:12
  • I have a form in another page. It posts the fields across as it should, but I have 14 of the same field, but the names change just by one digit, so address1, address2, address3, all the way up to address14. I need to create a loop to receive these from the form that has posted them. So where at the top of the page you create a new variable with the $_POST function I need that but 14 times. For example: $address1 = $_POST [address1], $address2 = $_POST [address2] and so on up until address 14. – Ade Apr 20 '13 at 14:16
  • $address[0] to $address[$x] ? – bestprogrammerintheworld Apr 20 '13 at 14:18
  • I understand I could use an array but I am trying to use variable variables. – Ade Apr 20 '13 at 14:18
  • why do you need to use variable instead of an array? Can you show the code of how you retrieve the $_POST-variables then? – bestprogrammerintheworld Apr 20 '13 at 14:19
  • Because on the form they are individual entities, they aren't set up as an array and I have dynamically created them on my form too using a counter, which is easier as it is just the name given to the input fields, but when trying to append the end of 'address' with a number for a variable it becomes more complex. – Ade Apr 20 '13 at 14:22
  • You could easily create dynamically created elements in a form (add brackets after name). Like this or you could solve it like user2301283 answered. – bestprogrammerintheworld Apr 20 '13 at 14:23

3 Answers3

2

why you don't do:

for ($i=0; $i < 14; $i++) {
    $address[$i] = $_POST['address'.$i];
}
Fosfor
  • 331
  • 2
  • 3
  • 15
0

Try this:

$addresses = array();
for( $x = 0; $x <= 14; $x++ )
{
    $address_field = "address" . $x;
    if( array_key_exists( $address_field, $_POST ) ) {
        $addresses[$x] = $_POST[$address_field];
        echo '<p>Address', $x, ': ', $addresses[$x], "</p>\n";
    }
}
J.D. Pace
  • 586
  • 1
  • 3
  • 17
0

You could also use the array syntax in your form :

<input type="text" name="address[1]" />
<input type="text" name="address[2]" />
<input type="text" name="address[3]" />
... up to 14

In PHP :

$address = $_POST['address'];

$address is now an array containing all the addresses