I have 2 arrays. I want a static array. I want 2 of the elements to be shown each time the user logs-in to the browser. Next time the user logs-in, it should show another 2 elements. I have 2 static arrays. When elements from 1 array is finished, it should switch to another array. This is a part of bigger requirement.I want to create an intermediate static array. There will be 2 inner foreach loops which will iterate through elements of the array and echo elements to each log-in.I have commented away all expressions that gave me error in the following code. Currently, once the url "http://swulj.atwebpages.com/loop.php" is entered in the browser, I get 2 elements. Next time the url is entered, it should persist the values of I and j. So accordingly the next element from the array should be printed. But it is printing the same values.I am using storing variables in session.
OUTPUT:
before loop; i = 0, j = 1, loop = 1
j = 1
i = 0, j = 1
i = Sachin, j = Virat
j = 2
break == true, session start
CODE:
<?php
$a1 = array("Tendulkar","Kohli","Dhoni");
$a2 = array("Sachin","Virat","Mahendra Singh");
$i = 0;
$j = 1;
$loop = 0;
//echo $loop;
session_start();
if(!isset($_SESSION["loop"]))
{
$_SESSION["loop"] = $loop;
}
else
{
$loop = $_SESSION["loop"];
}
if(!isset($_SESSION["i"]))
{
$_SESSION["i"] = $i;
}
else
{
$i = $_SESSION["i"];
}
if(!isset($_SESSION["j"]))
{
$_SESSION["j"] = $j;
}
else
{
$j = $_SESSION["j"];
}
echo "before loop; i = ". $i. ", j = ".$j.", loop = ".$loop."<br>";
$break = false;
if($loop == 0)
{
foreach($a1 as $result) {
$array[] = $result;
//echo $result;
}
$len = count($array);
//echo $len;
for(; $i < $len-1 ; $i++)
{
echo "inside";
if($break == true)
{
session_start();
$_SESSION["i"] = $i;
if($i==1 && $j==2)
{
$i = 0;
$j = 1;
session_start();
$_SESSION["i"] = $i;
$_SESSION["j"] = $j;
}
break;
}
for(; $j < $len; $j++)
{
echo "inside";
if($i == $j) continue;
if($break == true)
{
session_start();
$_SESSION["j"] = $j;
break;
}
echo"i = ". $i . ", j = ". $j."<br>";
echo"i = ". $array[$i] . ", j = ". $array[$j]."<br>";
if(($i == $len-2) && ($j == $len-1))
{
$loop=1;
session_start();
$_SESSION["loop"] = $loop;
$_SESSION["i"] = 0;
$_SESSION["j"] = 1;
unset($array);
}
$break = true;
}
}
}
if($loop == 1)
{
$break = false;
foreach($a2 as $result) {
$array[] = $result;
}
$len = count($array);
for(; $i < $len-1 ; $i++)
{
if($break == true)
{
session_start();
$_SESSION["i"] = $i;
if($i==1 && $j==2)
{
$i = 0;
$j = 1;
session_start();
$_SESSION["i"] = $i;
$_SESSION["j"] = $j;
}
break;
}
for(; $j < $len; $j++)
{
echo "j = ". $j."<br>";
if($break == true)
{
echo "break == true, session start". "<br>";
session_start();
$_SESSION["j"] = $j;
$_SESSION["j"] = $j;
break;
}
if($i == $j) continue;
echo"i = ". $i . ", j = ". $j."<br>";
echo"i = ". $array[$i] . ", j = ". $array[$j]."<br>";
if(($i == $len-2) && ($j == $len-1))
{
$loop=1;
session_start();
$_SESSION["loop"] = $loop;
$_SESSION["i"] = 0;
$_SESSION["j"] = 1;
unset($array);
}
$break = true;
}
}
}
?>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…