I have tried many solutions but they are not working for my purpose here is my php code which is working fine for generating a random expression.
<?php
if(isset($_POST['play']))
{
//validation for no of operation selected
$operation = $_POST['operation'];
if(empty($operation))
{
echo "Please Select at least one operation <br/>";
}
else
{
$N = count($operation); //count selected operation
?>
<div class="slideshow-container" id="slide">
<center>
<?php
echo "<br />";
echo "This is your generated sequence !<br><br>";
echo "You have selected $N operation(s):";
for($i=0; $i < $N; $i++) //for loop for for operator array.
{
echo($operation[$i] . ", "); //display selected operation
}
echo "<br />";
?>
</center>
</div>
<?php
//checkbox operations
if($N==1) //if only one operation is selected
{
if($operation[0]=='Addition')
{
$rOperators = array('+');
}
else if($operation[0]=='Subtraction')
{
$rOperators = array('-');
}
else if($operation[0]=='Multiplication')
{
$rOperators = array('*');
}
else
{
$rOperators = array('/');
}
}
else if ($N==2) //if only two operations are selected by the user
{
if($operation[0]=='Addition')
{
if($operation[1]=='Subtraction')
{
$rOperators = array('+','-');
}
else if($operation[1]=='Multiplication')
{
$rOperators = array('+','*');
}
else
{
$rOperators = array('+','/');
}
}
else if($operation[0]=='Subtraction')
{
if($operation[1]=='Multiplication')
{
$rOperators = array('-','*');
}
else
{
$rOperators = array('-','/');
}
}
else
{
$rOperators = array('*','/');
}
}
else if ($N==3) //if 3 o
{
if($operation[0]=='Addition')
{
if($operation[1]=='Subtraction')
{
if($operation[2]=='Multiplication')
{
$rOperators = array('+','-','*');
}
else
{
$rOperators = array('+','-','/');
}
}
else
{
$rOperators = array('+','*','/');
}
}
else
{
$rOperators = array('-','*','/');
}
}
else
{
$rOperators = array('+','-','*','/');
}
}
//display sequence having only single digit numbers
if($_POST['digits'] == 1)
{
$q = $_POST['que'];
$previousInt = null; //Track the previous operand
$s = $_POST['select'];
//display inputs given by user through form
?>
<div class="slideshow-container" id="slide">
<center>
<?php
echo "No.of questions:".$q. "<br />";
echo "No. of integers:" .$s. "<br />";
echo "No of digits: 1 <br />";
?>
</center>
<?php
for ($x = 1; $x<=$q; $x++) //for loop for no. of questions
{
$randomOperands = array();
$randomOperators = array();
// loop over $i n times
for ($i = 1; $i <=$s; $i++) //for loop for no. of integers user entered
{
$nextInt = rand(1, 9); // assign random operand to array slot
$randomOperands[] = $nextInt;
if($i < $s)
{
if($previousInt === 0) //No operator after last opearnd
{
//avoid division-by-zero
$randomOperators[] = $rOperators[rand(0, 2)];
}
else
{
$randomOperators[] = $rOperators[rand(0, $N-1)];
}
}
$previousInt = $nextInt;
}
// print array values:
$exp = '';
$output_string = " ";
//Generating the expression
foreach($randomOperands as $key=>$value1)
{
$exp .= $value1 . "" ;
$output_string .= $value1."<br>";
if(isset($randomOperators[$key]))
{
$exp .= $randomOperators[$key] ."";
$output_string .= $randomOperators[$key]."<br>";
}
}
//print expression
$res = eval("return ($exp);");//evaluate the expression
$generate = array($output_string , $res);
?>
<div class="mySlider">
<!-- back button to go back to previous element of expression-->
<a class="back" id= "pre">❮</a>
<!--display of the expression-->
<?php
echo 'Q('.$x.')<br><br>'.$generate[0].<br>';
?>
<!-- generate result with show button-->
<div class="ansdiv"><?php echo "<br>____<br>'.$generate[1]."<br>"; ?></div><br>
<!-- next buttonto go to next element of expression >
<a class="next" id = "fro">❯</a>
</div>
here is screen shot of output I am getting
so as you can see in screenshot the expression is generating with its result, Here $output_string
is the expression and $res
is result of expression they both are stored in array named $generate
Now I want to show the all the elements of array one by one on clicking next button i.e the first element should be 4 according to screenshot and after clicking next button there should be - operator and so on untill the result displayed at last.
NOTE - Here everytime the value of $output_string
variable will be different because The expressions are generating randomly and accordingly the result will also changed.this is working fine for my purpose but I tried many solution I am unable somehow to display elements one by one. How can I fix it? Please help me ?
Please help me , I am not able to fix it .