Fabis94 Posted March 28, 2009 Share Posted March 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151535-adding-variables-to-_session-variables-with-for/ Share on other sites More sharing options...
redarrow Posted March 28, 2009 Share Posted March 28, 2009 edit, was wrote wrong sorry. Quote Link to comment https://forums.phpfreaks.com/topic/151535-adding-variables-to-_session-variables-with-for/#findComment-795886 Share on other sites More sharing options...
Philip Posted March 28, 2009 Share Posted March 28, 2009 //... session start at top foreach($hparts as $key=>$value) { $_SESSION[$key] = $value; } print_r($_SESSION); // just to print what was placed in session Quote Link to comment https://forums.phpfreaks.com/topic/151535-adding-variables-to-_session-variables-with-for/#findComment-795887 Share on other sites More sharing options...
redarrow Posted March 28, 2009 Share Posted March 28, 2009 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]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151535-adding-variables-to-_session-variables-with-for/#findComment-795888 Share on other sites More sharing options...
Fabis94 Posted March 28, 2009 Author Share Posted March 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151535-adding-variables-to-_session-variables-with-for/#findComment-795891 Share on other sites More sharing options...
redarrow Posted March 28, 2009 Share Posted March 28, 2009 can you read the new example i posted please thank you. Quote Link to comment https://forums.phpfreaks.com/topic/151535-adding-variables-to-_session-variables-with-for/#findComment-795892 Share on other sites More sharing options...
Fabis94 Posted March 28, 2009 Author Share Posted March 28, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/151535-adding-variables-to-_session-variables-with-for/#findComment-795894 Share on other sites More sharing options...
Philip Posted March 28, 2009 Share Posted March 28, 2009 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151535-adding-variables-to-_session-variables-with-for/#findComment-795901 Share on other sites More sharing options...
Fabis94 Posted March 28, 2009 Author Share Posted March 28, 2009 Thanks, it works Quote Link to comment https://forums.phpfreaks.com/topic/151535-adding-variables-to-_session-variables-with-for/#findComment-795902 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.