Jump to content

Adding variables to $_SESSION variables with FOR


Fabis94

Recommended Posts

I have 83 variables in an array and the array is $hparts. I used this to add an array variable to a session variable:

for ($i = 0; $i <= 82; $i++) {
    $_SESSION["$1"] = $hparts["$1"];
}

 

On the start of the page i used start_session(); and after doing the FOR it redirects to the next page. In the next page i also have start_session(); at the beginning but the SESSION variables don't appear. I wrote:

 

echo $_SESSION[0];

echo $_SESSION['0'];

echo $_SESSION["0"];

 

But none of them appeared.

 

can do it this way as well

<?php session_start();

$array=array(1,2,3,4,5,6,7,8,9);

for ($i = 0; $i <count($array); $i++) {
    $_SESSION["new"][] = $array[$i];
}

print_r($_SESSION['new']);

//example

echo $_SESSION['new'][1];
?>

After it does the FOR it prints this on the screen:

 

Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => [25] => [26] => [27] => [28] => [29] => [30] => [31] => [32] => [33] => [34] => [35] => [36] => [37] => [38] => [39] => [40] => [41] => [42] => [43] => [44] => [45] => [46] => [47] => [48] => [49] => [50] => [51] => [52] => [53] => [54] => [55] => [56] => [57] => [58] => [59] => [60] => [61] => [62] => [63] => [64] => [65] => [66] => [67] => [68] => [69] => [70] => [71] => [72] => [73] => [74] => [75] => [76] => [77] => [78] => [79] => [80] => [81] => [82] => ) 

 

But it doesn't help much. It stores the whole array in $_SESSION['new'], but what do i need to type in the next page so it shows the string stored in the variable of the array.

This is the whole script for the page:

 

<?php
session_start();

$username = $_POST["username"];
$hiscores = file_get_contents('http://hiscore.runescape.com/index_lite.ws?player=' . $username);
$hparts = explode(',', $hiscores); // Max $hparts should be 82 (0 is first)



for ($i = 0; $i <= count($hparts); $i++) {
    $_SESSION["new"][] = $hparts[$i];
}


echo $_SESSION['new'][1];


echo '<script type="text/javascript">';
echo 'window.location="hiscores_out.php";';
echo '</script>'; 
?>

 

It doesn't print anything on the screen...

Did you try:

foreach($hparts as $key=>$value) {
    $_SESSION[$key] = $value;
}

 

<?php
session_start();

$username = $_POST["username"];
$hiscores = file_get_contents('http://hiscore.runescape.com/index_lite.ws?player=' . $username);
$hparts = explode(',', $hiscores); // Max $hparts should be 82 (0 is first)

foreach($hparts as $key=>$value) {
    $_SESSION[$key] = $value;
}


echo $_SESSION[1];


echo '<script type="text/javascript">';
echo 'window.location="hiscores_out.php";';
echo '</script>'; 
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.