Home > Enterprise >  PHP for loop in $_SESSION names
PHP for loop in $_SESSION names

Time:10-12

Trying to convert $_GET to $_SESSION but alot of them using php for loop!

But i cant get it to work!

for ($x = 0; $x <= 2; $x  ) {
$_SESSION["u'.$x.'_first_name"] = $_GET["u'.$x.'_first_name"];
}

CodePudding user response:

I think this what you want

    for ($x = 1; $x <= 2; $x  ) {
    $_SESSION['u'.$x.'_first_name'] = $_GET['u'.$x.'_first_name'];
    }

Your single quotes were bracketing the $x.

And I have started the loop counter at 1 (from the comments).

CodePudding user response:

Everyone was right. It works already, maybe i have some intefering $x somewhere else that was making the code not to work. As when i run it by it self, its all good!

for ($u = 1; $u <= $_GET['amount']; $u  ) {
$_SESSION['u'.$u.'_first_name'] = $_GET['u'.$u.'_first_name'];
echo 'session : '.$_SESSION['u'.$u.'_first_name'].'<br>';
}

echo '
<form action="" id="formBook">
Amount : <input id="amount" name="amount" value="'.$_GET['amount'].'"><br>
';

for ($x = 1; $x <= $_GET['amount']; $x  ) {
echo '
Name '.$x.' : <input type="text"  id="u'.$x.'_first_name" name="u'.$x.'_first_name" value="'.$_SESSION['u'.$x.'_first_name'].'"><br>
';
}

echo '
<input  type="submit" name="submit" value="Next" ><br>
';

https://ideone.com/sodDBm (a fiddle for it)

Thanks everyone for your help!

  • Related