Niels
Niels

Reputation: 1006

PHP how to put the following in a loop

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']);

Upvotes: 0

Views: 54

Answers (1)

Danielius
Danielius

Reputation: 837

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!

Upvotes: 2

Related Questions