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 />';
}
?>
Related
I had a problem with my array_push, that i noticed.
So what i'm doing.:
I have a site, where there are some buttons with a specific value.
Each value is getting fetched from a database.
I have a session called test, that get's converted to an array(to store multiple in the same array)
Everytime one of the buttons are clicked, the value for that specific button, is getting pushed to the array.
But, i can ONLY see that it has been pushed at the second try.
[test] => Array( [0] => 21304 )
This is what i see, after second try. But my array count, says that there are 2 elements, in that array.
Here is my code:
if(isset($_POST['process'])) {
if(!isset($_SESSION['test'])) {
$_SESSION['test'] = array();
$array_merge = array_push($_SESSION['test'], $_POST['process']);
}
}
The $_POST['process'] is the button with the unique value.
Can somebody maybe see what I'm doing wrong here?
Kind regards
You are only adding to the $_SESSION['test'] array if $_SESSION['test'] was not previously set.
So you need to always add an occurance to the session array and only initialise the session array if it was not previously set
session_start();
// ...
if(isset($_POST['process'])) {
if(!isset($_SESSION['test'])) {
$_SESSION['test'] = array();
}
$_SESSION['test'][] = $_POST['process'];
}
NOTE from the manual
If you use array_push() to add one element to the array, it's better to use $array[] = because in that way there is no overhead of calling a function.
I have this array in php code. I want to have that whenever page is being called it should print value of first array index and when next time second value of array index and so on... what modification I could do? for now it´s printing everything when being called single time.
<html>
<?php
$addresses = array('ifcbxespra', 'ifcheqjbmea', 'ifcqiknsa', 'ifcqirtjla', 'ifcwqsrlmn', 'ifclmkmzhz','ifcwdujhgc','ifcihddngh','icffhzudcd','ifchnsqzgs','ifcgssqrhg');
foreach ($addresses as &$value) {
echo $value ;
}
?>
</html>
I'm not sure if I understood what you want. But if you want to print the first array's value when the page loads one time, the second array's value when the page loads another time and so on, you can do this:
<?php
if(!isset($addresses) || empty($addresses)){ //checks if the array is not initialized or if it's empty
$addresses = array('ifcbxespra', 'ifcheqjbmea', 'ifcqiknsa', 'ifcqirtjla', 'ifcwqsrlmn', 'ifclmkmzhz','ifcwdujhgc','ifcihddngh','icffhzudcd','ifchnsqzgs','ifcgssqrhg');
echo $addresses[0]; //print the first value
array_splice($addresses, 0, 1); //removes the first element of the array and reindexes it
}else{
echo $addresses[0]; //print the first value
array_splice($addresses, 0, 1); //removes the first element of the array and reindexes it
}
The logic behinds it is: if the array already exists and is not empty (it has values), print the first value and then remove it, so next time the first value will be the second actual value. When the array is empty, redefine it as to start again.
You can search for more information on array_splice() here.
P.S.: you have to use PHP's $_SESSION to save the array between the pages.
You can use something like $_SESSION and store there the last index.
For example:
$array = array('one', 'two', 'three');
if (!$_SESSION['nextIndex'] || $_SESSION['nextIndex'] >= count($array)) {
$_SESSION['nextIndex'] = 0
}
// print the value
echo $array[$_SESSION['nextIndex']];
// increment the nextIndex
$_SESSION['nextIndex']++;
NOTE: This will only work for the same user. Each page reload will increment the array index. But if you need some cross-user counting, then you have to store the information somewhere on the server, like a DB or even a simple txt file.
Check out this example: http://hibbard.eu/how-to-make-a-simple-visitor-counter-using-php/
Finally I solved this problem with the MySQL. created a column with all code. and then call the script every-time when user press button. In the script first I fetch first raw and print that value, and then, delete that raw. so every-time user will get unique value from the list of code And it is working fine.
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 have 3 objects with same variable but rendered with diferent values.
001 - Title one
002 - Title two
003 - Title three
I need to check on another block of page if this first three are the same. The only thing that comes to mind is store this variables in a $_SESSION. So far so good. The problem is that it only stores last value (obviously).
CODE
/* -- block one --*/
session_start();
$_SESSION['lasttitle'] = $item->getTitle;
echo $item->getTitle(); //rendering 1st object
echo $item->getTitle(); //rendering 2nd object
echo $item->getTitle(); //rendering 3rd object
/* -- block two --*/
session_start();
$last3 = $_SESSION['lasttitle'];
if($item->getTitle() != $last3) {
//don't render last 3
}
$_SESSION['lastTitles'][] = $item->getTitle();
$_SESSION['lastTitles'] = array_slice($_SESSION['lastTitles'], -3);
..
if (in_array($item->getTitle(), $_SESSION['lastTitles'])) ..
This stores an array of the last three titles, and checks whether a title is in this array.
Create multidimensional session:
$_SESSION['lasttitle'][$item->id] = $item->getTitle();
$_SESSION['lasttitle'][$item2->id] = $item2->getTitle();
$_SESSION['lasttitle'][$item3->id] = $item3->getTitle();
And check if that element is in array:
$total = 0;
foreach ($items as $item) {
if (isset($_SESSION['lasttitle'][$item->id])) {
$total++;
}
}
if ($total == 3) {
// Do something, it's all 3 titles in session!
}
I know it may be better solution, but I don't understand full algorithm. Also, to compare some arrays, you can use array_diff_assoc()
I think storing data as array in session might help you. Simply gather all titles you need in an array and put it into $_SESSION (instead of one variable). One the next page you will be able to analyze data you have and choose variables you need.
<?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']);