i would like to pass a variable through a for loop into a link such that the loaded page passes information about the correct link. right now my code seems to be storing the last variable in the for loop and passing that onto the link. it means that the generated table in the link always references the final piece.
the code is:
<?php foreach($result as $key => $result) :?>
<?php session_regenerate_id(); $_SESSION['id'] = $result['id']; ?>
<tr>
<td><?php echo $result['id'] ?></td>
<td><?php echo $result['date'] ?></td>
<td><?php echo $result['name'] ?></td>
<td><div data-toggle="buttons"><a href="cpt.php?id=$_SESSION['id']" target="_blank" rel="noopener">More details</a></div></td>
</tr>
<?php endforeach;?>
The point is the link should display more information about the id that the user clicks on. However it only ever shows data of the last id. I think this is because the $_session['id'] is just storing the final loop.
CodePudding user response:
You are overwriting the $result
variable in your loop:
<?php foreach($result as $key => $result) :?>
Do something like this instead:
<?php foreach($result as $key => $value) :?>
Then in your link you should be able to do:
<a href="cpt.php?id=<?php echo $value['id'] ?>"
I don't see any reason to be using session in this loop, I would advise removing that. You certainly don't want to be regenerating the session ID inside of a loop that displays chunks of html.