Is there a way to store values that I append to an array for further cases?
For example, I create an array in one page of php and from another page I append value to the array on the first page. The problem appears when the second page tries to append another value to first page and the value gets replaced. I shall explain with code.
FirstPage.php
<?php
global $file;
print_r ($file);
?>
SecondPage.php
<?php
$file = array();
$id=$_GET['id'];
array_push($file, "$id"); //ID = 1 here
include('FirstPage.php');
?>
When I execute the SecondPage.php, the output is
Array ( [0] => 1)
Then I change the value of ID = 2, the output then is
Array ( [0] => 2)
So the values never get stored in FirstPage.php, they always get replaced.
You can use session variable for this by assigning the value to the session variable on the first page as
SESSION
First Page
<?php
session_start();
$_SESSION['variable'] = $file; //variable assigned the value of array()
print_r ($file);
?>
And on the second page assign the value session variable to the simple variable.
Second Page
<?php
session_start();
$file = $_SESSION['variable'];
$id=$_GET['id'];
array_push($file, $id); //ID = 1 here
?>
And after this you don't need to include first page in again.
As you have not mentioned your array() in the question take this as an example
First.php
<?php
session_start();
$file = $_SESSION['varname'];
print_r($file);
?>
Second.php
<?php
session_start();
$file = array(1,2,3,4);
array_push($file,'5');
$_SESSION['varname'] = $file;
header("Location: first.php");
?>
You should use database, save your array to file or use another method of storing data. These operations you are using are stateless. Content of arrays will not be saved between next executions of PHP scripts.
Sessions work until browser closing so it's not also the solution of your problem.
Related
I've two page , first page there's an array data and second page I want call array data
Like this
First page index.php
$array_data[]=$array_tmp;
print_r($array_data); // array can display in this page
$_SESSION['one'] = $array_data;
Second page next.php
I want to call array from first page
session_start();
$array = $_SESSION['one'];
foreach( $array as $key => $value ) {
echo $value;
}
print_r($_SESSION['one'])
May I know what's wrong? As array can't display in second page.
I think you should change this code $array = $_SESSION['one']; to $array[] = $_SESSION['one'];. I`m not sure & not tested as well but I think so. Hope this will help.
You need to start the session if you already have not. Without starting the session, you can't assign value to the session variable. So the first code snippet would be like this:
session_start();
$array_data[]=$array_tmp;
print_r($array_data); // array can display in this page
$_SESSION['one'] = $array_data;
Second snippet looks fine but the last line is missing a semicolon. It might sound silly, but that can prevent the whole script from running. Here's the code fixed.
session_start();
$array = $_SESSION['one'];
foreach( $array as $key => $value ) {
echo $value;
}
print_r($_SESSION['one']);
Post more code if this does not work.
I'm sending data over to a PHP file and storing it using $_SESSION. I'd like to post multiple instances of data to the session at different times and view these on the front end. Currently I can store one item in my session and this is displayed on the front end, however I'm struggling to add further instances of artist and title as it simply gets overwritten every time I $_POST data across.
How do I store and display an array of $_POST data in the session variable? I've tried adding a further set of [] after $_SESSION['artist'] and $_SESSION['title'] but this doesn't work.
<?php
session_start();
if(isset($_POST['artist']))
{
$_SESSION['artist'] = $_POST['artist'];
}
if(isset($_POST['title']))
{
$_SESSION['title'] = $_POST['title'];
}
print_r($_SESSION['artist']);
echo "<br>";
print_r($_SESSION['title']);
?>
You'll need to use an array to hold these values, so $array[] = $value will append $value to the $array.
Example:
if(isset($_POST['title'])) {
// Append POST data to SESSION
$_SESSION['titles'][] = $_POST['title'];
}
print_r($_SESSION['titles']);
You don't need to make sure $array is actually an array, since [] will make the variable an array if needed: Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.
<?php
session_start();
if(isset($_POST['artist']))
{
$_SESSION['artist'][] = $_POST['artist'];
}
if(isset($_POST['title']))
{
$_SESSION['title'][] = $_POST['title'];
}
print_r($_SESSION['artist']);
echo "<br>";
print_r($_SESSION['title']);
?>
user array to store it.
<?php
session_start();
$_SESSION['del']=array("a1","a2","a3","a4","a5");
unset($_SESSION['del'][0]);
echo implode(" ",$_SESSION['del'])
?>
How do I remove each array element one by one every time I refresh the page?
here is the code
<?php
session_start();
if(isset($_SESSION['del'])) { // to make sure array is not set again as in question
unset($_SESSION['del'][0]); // remove the first element
$_SESSION['del'] = array_values($_SESSION['del']); // to shift rest of the elements one location left for making indexes starts from 0
} else { // set session array only once
$_SESSION['del']=array("a1","a2","a3","a4","a5");
}
echo implode(" ",$_SESSION['del']); // print results
?>
if(isset($_SESSION['del']))
{
if(is_array($_SESSION['del']))
{
array_shift($_SESSION['del']);
}
}
Code Explanation
if the session del is currently set.
and that the session['del'] is an array
remove the first value of the array del.
I am not sure if it works with $_SESSION, but if it is an array then array_shift() would be what you are looking for:
array_shift($_SESSION['del']);
I have an array of variables and I need to show its values.
So at first I send "me" variable to copy the array into session then I send "mypage" variable to show the first variable of the array but when I send "mypage" variable it seems the session is empty as show function does not show anything.
Please advice if you think I should use any other method as I am doing this to implement pagination.
<?php session_start();
if(isset($_GET["me"]) //if me variable is received copy the array in session
{
$myarray = array("foo", "bar", "hallo", "world");
$_SESSION["mysession"] = $myarray;
}
if(isset($_GET["mypage"]) // if mypage variable is received go to show function
show()
show() // show value 1 of the session
{
$values = array();
$values= $_SESSION['mysession'];
echo $values[1];
}
?>
Your code is very vague though for your question it seems that you are accessing SESSION with wrong key.
As you are setting session with mysession key above. Try to change below in your show function,
$values= $_SESSION['mysession'];
Everytime the user visit a page, the page url will be stored into an array session. I want to have 10 elements in the array only. So that 10 elements will save 10 latest visited page urls. Here is my codes :
<?php
$currentpageurl = $_GET['username'];
$urlarray=array();
$urlarray[] = $currentpageurl;
$_SESSION['pageurl']=$urlarray;
foreach($_SESSION['pageurl'] as $key=>$value)
{
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
?>
I tested the code, it always overwrite the element in the array with new visited page, thus it has only 1 element in the array. How to make it not to overwrite the element?
You are always overwriting the array with a new one here:
$urlarray=array(); // new empty array
$urlarray[] = $currentpageurl;
$_SESSION['pageurl']=$urlarray;
Instead use:
session_start();
// like #Kwpolska said, you probably miss that, so $_SESSION didnt work
is_array($_SESSION["pageurl"]) or $_SESSION["pageurl"] = array();
// fix for your current problem
$_SESSION['pageurl'][] = $currentpageurl;
// This appends it right onto an array.
$_SESSION["pageurl"] = array_slice($_SESSION["pageurl"], -10);
// to cut it down to the last 10 elements
Simplest way to do this and keep just the last 10 entries will be to create your initial array with the correct size (via array_fill()). Then we can push new items onto the beginning of the array and pop old items off the other end using array_unshift() and array_pop().
session_start();
// Initialise URL array with 10 entries.
if (empty($_SESSION['pageurls'])) {
$_SESSION['pageurls'] = array_fill(0,10,'');
}
function trackPage($url) {
array_unshift($_SESSION['pageurls'],$url);
array_pop($_SESSION['pageurls']);
}
Make sure the above code always runs first. Then you can pass new URLs to the array when you want. So, perhaps something like:
trackPage($_SERVER['REQUEST_URI']);
You've ommitted session_start();. Working code (without trimming):
<?php
session_start();
$currentpageurl = $_GET['username'];
$_SESSION['pageurl'][] = $currentpageurl;
foreach($_SESSION['pageurl'] as $key=>$value) {
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
?>