Reputation: 3
I don't have much knowledge, but I'm turning around, but I came across the following question, which seems to be simple in my view, but I found no answers or examples. If you can help me, please.
I get a list of 5 items on the paeg access.php
<?php
session_start ();
foreach($followers as $value){
$_SESSION['username_recente'] = $value['username'];
$_SESSION['full_name_recente'] = $value['full_name'];
$_SESSION['profile_pic_url_recente'] = $value['profile_pic_url'];
$_SESSION['followed_by_viewer_recente'] = $value['followed_by_viewer'];
}
Return this to me:
Username: johndoe1
Fullname: john doe 1
profile_pic: http://....
followed: true
Username: johndoe2
Fullname: john doe 2
profile_pic: http://....
followed: true
I have 5 results of these.
With a simple echo, on that same page, I get the 5 items I want.
However, on the next page, index.php, there is only the first item on the list.
index.php
<?php
foreach ($_SESSION as $key => $value) {
print $key . '<br>';
print $value;
}
returns me only 1 result in index.php
Username: johndoe1
Fullname: john doe 1
profile_pic: http://....
followed: true
Am I using the functions correctly? I do not think so. Please help me.
Upvotes: 0
Views: 562
Reputation: 3
Thanks to the help comments, they were very helpful, but I managed to solve the problem as follows:
Save session data with:
$_SESSION['array'] = $followers;
the next page, just:
foreach ($_SESSION['array'] as $value) {
// values...
}
tks a lot
Upvotes: 0
Reputation: 78994
You're only creating a single dimension with those keys, so for example $_SESSION['username_recente']
is overwritten each time through the loop and there will only be one. You'll need a multi-dimensional array of some sort. Here's an example similar to how database rows are returned:
foreach($followers as $key => $value) {
$_SESSION[$key]['username_recente'] = $value['username'];
$_SESSION[$key]['full_name_recente'] = $value['full_name'];
$_SESSION[$key]['profile_pic_url_recente'] = $value['profile_pic_url'];
$_SESSION[$key]['followed_by_viewer_recente'] = $value['followed_by_viewer'];
}
Then:
//required
session_start();
foreach ($_SESSION as $array) {
foreach($array as $key => $value) {
print "$key : $value<br>";
}
}
If you can use the same keys from $followers
in $_SESSION
then for the first page, just:
$_SESSION = $followers;
//or better
$_SESSION['followers'] = $followers;
Then on the next page loop $_SESSION['followers']
.
Upvotes: 1