This may be a silly question and sorry for any confusing sentences.. I don’t know if I can explain this issue well enough to make you understand, but $_SESSION seems unable to be repeated more than once on a page.
session_start();
while (list($a, $b) = each($_SESSION['temp']))
echo "<li>$a - $b</li>";
The above code is ok, but if I have $_SESSION['temp'] on the same page as below, then it doesn’t show anything…
session_start();
while (list($a, $b) = each($_SESSION['temp']))
echo "<li>$a - $b</li>";
while (list($c, $d) = each($_SESSION['temp']))
echo "<li>$c - $d</li>"; /* <=== nothing shown :( */
To get the value from the $_SESSION['temp'], I need to give it a new name:
session_start();
$temp = $_SESSION['temp']; /* <== new name */
while (list($a, $b) = each($_SESSION['temp']))
echo "<li>$a - $b</li>";
while (list($c, $d) = each($temp))
echo "<li>$c - $d</li>"; /* <=== now shown :) */
Can you tell me how come $_SESSION['temp'] can’t be used twice or more on the same page?
Is there any better way to get a value from $_SESSION['temp']?
Thank you.
http://php.net/manual/en/function.each.php
Return the current key and value pair from an array and advance the array cursor.
After each() has executed, the array cursor will be left on the next element of the array, or past the last element if it hits the end of the array. You have to use
reset()if you want to traverse the array again using each.