Get one item 1 time for every 10 pages of reload

I have an array with 10 elements. $ arr = range (0, 9); I want to get one item once for every 10 pages of reload. Ho, can I do this?

+4
source share
4 answers

With session variables and a little modular arithmetic, you have to complete the task

<?php 

session_start();

$array = range(0, 9); // this could be any array
$repeat = 10;

if(!isset($_SESSION['counter'])) $_SESSION['counter'] = 0;
if(!isset($_SESSION['subcounter'])) $_SESSION['subcounter'] = 0;

echo $array[$_SESSION['subcounter'] % sizeof($array)];

if($_SESSION['counter'] % $repeat == 0)
  $_SESSION['subcounter']++;

$_SESSION['counter']++;

?>

Use cookies instead of sessions if you want to keep the counter after closing the browser.

tested and approved

+1
source

Try the following:

$arr = range(0,9);
$random = array_rand($arr, 1);

echo $random;

This will allow you to select one element within the range of your array $arrfor each reload.

0
source

, x , , , , . , , , , .

!

0

, , .

, , , .

session_start();

$arr = array('Item A','Item B','Item C','Item D','Item E','Item F','Item A','Item H','Item I','Item J');

if( isset($_SESSION['counter'],$_SESSION['showthisindex']) === false or $_SESSION['counter'] >= 10 )
{
  $_SESSION['counter'] = 1;
  $_SESSION['showthisindex'] = array_rand($arr);
}
else
{
  $_SESSION['counter']++;
}

echo $arr[ $_SESSION['showthisindex'] ];
0

Source: https://habr.com/ru/post/1607212/


All Articles