Asked 7 years ago
24 Jan 2017
Views 2208
jaydeep

jaydeep posted

PHP :: array next index in every call

how can i print next index of array by each webpage request .
let me show you what i have


<?php 
$array=array("first call","second call","third call","forth call")
foreach($array as $value){
echo $value;
}
?>


now it print all but i need to print only one value of array which is next index of array by each page call so
suppose if i load web page first time it should print first call , than in second time it should print second call so on... hope you get the my question
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

in every webpage request you can set increment of query string which indicate which index we need to print on this web call..
like
arrayoverflow.com/webcall.php?request=1
arrayoverflow.com/webcall.php?request=2
arrayoverflow.com/webcall.php?request=3


if(isset($_REQUEST['request']) && $_REQUEST['request']!=''){
$request=array('first request',"second request","third request");
if(isset($request[$_REQUEST['request']])){
echo $request[$_REQUEST['request']];   
}
}


so you can pass index at query parameters
but it should be auto remember . i dont want to pass the index as query string - jaydeep  
Jan 24 '17 13:41
Rasi

Rasi
answered Nov 30 '-1 00:00

why you dont use the session to remember which one index is need to print


$ARRAY=array("my","name","is","rasila");
session_start();
if(!isset($_SESSION['index']) )
$_SESSION['index']=0;
if($_SESSION['index']>count($array) )
$_SESSION['index']=0;

echo $ARRAY[$_SESSION['index']];
$_SESSION['index']=$_SESSION['index']+1;



above code print each time next indexed array value . if it reach at end of array than it reset automatically and start from first index again to print

and if you dont have issue with COOKIE than you can use PHP COOKIE also
same code will be look like this in PHP

$ARRAY=array("my","name","is","rasila");
 if(!isset($_COOKIE['index']) )
set_cookie('index',0,time()+3600);
if($_COOKIE['index']>count($array) )
$_COOKIE['index']=0;

echo $ARRAY[$_COOKIE['index']];
$_COOKIE['index']=$_COOKIE['index']+1;



Post Answer