0

I've been trying for 2 hours now and I cannot seem to get it right.

How do I put the following in a loop and create unique variables for each output:

$valueEmail = mysqli_real_escape_string($sql, $_POST['Email']);
$valuePassword = mysqli_real_escape_string($sql, $_POST['Password']);
$valueConfirmPassword = mysqli_real_escape_string($sql, $_POST['ConfirmPassword']);
Niels
  • 1,005
  • 1
  • 8
  • 18

1 Answers1

2

I don't understand, what you really need, but if I understood correctly you can use something like this:

$array = //array with all your inputs
[
    'Email',
    'Password'
];
for($i=0; $i<count($array);$i++) {
    ${'value'.$array[$i]}=mysqli_real_escape_string($sql, $_POST[$array[$i]]);
}
echo $valueEmail." ".$valuePassword; // Works!

You can read more here Appending a value of a variable to a variable name?

Good luck!

Community
  • 1
  • 1
Danielius
  • 852
  • 5
  • 23
  • Hey thanks that works! Also, what if my array looks like this: `$formOptions = array('Email', 'Password');` – Niels Jan 10 '17 at 18:30
  • By the way: Sorry for my vague question but i'm glad you got it haha – Niels Jan 10 '17 at 18:32
  • 1
    @NielsvanOsch well, your array can look both like `array()` or like `[...]` there is no difference ;) If this answer helped you, you can mark it as best :) – Danielius Jan 10 '17 at 18:35
  • what array do you recommend? – Niels Jan 10 '17 at 18:37
  • @NielsvanOsch don't think there is any difference in new versions of PHP but I always use `[]`. Here: http://stackoverflow.com/a/26652262/4402190 :) – Danielius Jan 10 '17 at 18:38