PHP - Display a different string on refresh, but not random
Let's say I have this array:
array('A', 'B', 'C', 'D');
I want to display one item at a time, on page reload. For example, on first visit, it shows 'A'; on refresh, it shows 'B'; then 'C'; then 'D'; then 'A'. The point is to show these item in order on each page reload开发者_JS百科. I think I should use cookies, but I don't have the knowledge.
I don't want to do this with random because it will show something like 'C' then 'A' then 'D'...
Can you help me with some tips, hints or keywords to search? I don't know how to start.
Any help is appreciated!
You have to store what the last request showed and return the element base on this. You can use a file or a database for this. If you just want to go through on each request, you could store a file and increment a counter between 0 and 3, read the element from the array and safe the counter.
If you want to make it for every user, you could set a cookie and increment the counter there. Or you try to identify the user (again, cookie or session) and increment the counter in a database, which might be some overhead.
You could use sessions as in
<?
session_start();
$data = array('A', 'B', 'C', 'D');
if (isset($_SESSION['last_index'])) {
$_SESSION['last_index'] = ($_SESSION['last_index'] + 1) % count($data);
} else {
$_SESSION['last_index'] = 0;
}
echo $data[$_SESSION['last_index']];
Read about sessions
You can do this with a PHP session.
<?php
$default = Array('A', 'B', 'C', 'D') ;
session_start() ;
$_SESSION['myarray'] = isset($_SESSION['myarray']) ? $_SESSION['myarray'] : $default ;
$_SESSION['myarray'] = (count($_SESSION['myarray']) > 0) ? $_SESSION['myarray'] : $default ;
echo array_pop($_SESSION['myarray']) ;
?>
Use array_rand
(http://php.net/array_rand) and array_pop
. Store the array in the session:
if ( !isset( $_SESSION['strings'] ) or !count( $_SESSION['strings'] ) ) {
// initialize the array (in random order)
$_SESSION['strings'] = array_rand( array('A', 'B', 'C', 'D') );
}
$item = array_pop( $_SESSION['strings'] ); // take one element off the array
The above regenerates the array everytime all the items have been returned - if you want to maintain the same order for the entire session, do:
if ( !isset( $_SESSION['strings'] ) ) {
// initialize the array (in random order)
$_SESSION['strings'] = $_SESSION['strings_def'] = array_rand( array('A', 'B', 'C', 'D') );
}
if ( !count( $_SESSION['strings'] ) ) {
$_SESSION['strings'] = $_SESSION['strings_def'];
}
$item = array_pop( $_SESSION['strings'] ); // take one element off the array
$strings = array('A','B','C','D');
$cur_index = (empty($_SESSION['page_idx']))?$_SESSION['page_idx']:-1;
++$cur_index;
if ($cur_index>=count($strings))
$cur_index = 0;
echo $strings[$cur_index];
//And finally, the trick itself
$_SESSION['page_idx'] = $cur_index;
<?php
session_start();
$arr = array('A','B','C','D');
if (!isset($_SESSION['idx'])) $_SESSION['idx'] = 0;
else $_SESSION['idx'] = (($_SESSION['idx'] > (count($arr)-2)) ? 0 : ($_SESSION['idx']+1) );
echo $arr[$_SESSION['idx']];
?>
精彩评论