How to display two elements of array at same time using foreach loop in php?

@ [benanamen its my form through which inputs are coming
I am building a sequence generator

  function output_form($data) {
     extract($data);
      ?>  
      <div class="flex flex-center flex-column">
          <h2><i>GENERATE HERE !</i></h2>
                    <form action="" method="POST" id="generator">
                <input name="seed" value="<?php echo $seed ?>" type="hidden" />
            <div class="row">
                 <div class="col-25">
                <label for="num_questions">Select No. of questions:</label>
            </div>
            <div class="col-75">
                <input type="number" id="num_questions" name="num_questions" value="1" min="1" max="100">
            </div>
        </div>
        <br><br>

        <div class="row">
            <div class="col-25">
                <label for="int">How many numbers you want in a sequence? :</label>
            </div>
            <div class="col-75">
                <select name="num_operands">
                    <option value="2"> 2 </option>
                    <option value="3"> 3 </option>
                    <option value="4"> 4 </option>
                    <option value="5"> 5 </option>
                    <option value="6"> 6 </option>
                    <option value="7"> 7 </option>
                    <option value="8"> 8 </option>
                    <option value="9"> 9 </option>
                    <option value="10"> 10 </option>
                </select>
            </div>
        </div>
        <br>
        <div class="row">
            <div class="col-25">
                <label for="dig">Select no. of digits:</label>
            </div>
            <div class="col-75">
                <select name="num_digits">
                    <option value="1"> 1 Digit </option>
                    <option value="2"> 2 Digits </option>
                    <option value="3"> 3 Digits </option>
                    <option value="4"> 4  - Mix Digits </option>
                   
                </select>
            </div>
        </div>
        <br><br>
        <div class="row">
            <div class="col-25">
                <label>Select operations:</label>
            </div>
            <div class="col-75">
                <label>
                    <input type="checkbox" value="all" onChange="toggleCheckboxes.call(this, 'operation[]')">
                    All
                </label><br>
                <label>
                    <input type="checkbox" name="operation[]" value="Addition" checked>
                    Addition
                </label><br>
                <label>
                    <input type="checkbox" name="operation[]" value="Subtraction">
                    Subtraction
                </label><br>
                <label>
                    <input type="checkbox" name="operation[]" value="Multiplication">
                    Multiplication
                </label><br>
                <label>
                    <input type="checkbox" name="operation[]" value="Division">
                    Division
                </label><br>
            </div>
        </div>
         <br>

         <div class="row">
            <div class="col-25">
                <label>Duration:</label>
            </div>
            <div class="col-75">
                <input type="number" id="duration" name="duration" value="1">
            </div>
        </div>
        <br>
         <input type="button" name="submit" value="GENERATE" id="gen">
         <input type="submit" name="play" value="PLAY" id="play" />
         <input name="reset" id="re" type="reset" onclick="resetForm()" />  
        
    </form><br>
        </div>



           <?php 
       //onclicking generate button show reset button also 
           if(!isset($_POST['submit'])){
       ?>
     <script>
        $(document).ready(function(){
                      $("#re").hide();
                    });
     </script>
     <?php
   }
 }

function output() {
  $data = get_input_data();
   if (!isset($data['play'])) {
    output_form($data);
} else {
    output_slideshow($data);
     }
  }
?>

Thi is my code for generating output slideshow

function get_input_data() {
$seed = $_POST['seed'] ?? rand();
$play = $_POST['play'];
$num_digits = intval($_POST['num_digits']) ?? 1;
$num_questions = intval($_POST['num_questions']) ?? 1;
$num_operands = intval($_POST['num_operands']) ?? 1;
$duration = $_POST['duration'] ?? 1;

// Read operations from the POST params and map the array that looks like 
//    ['Addition', 'Multiplication'] as  ['+', '*']
 
$operations_map = array(
    'Addition'       => '+',
    'Subtraction'    => '-',
    'Multiplication' => '*',
    'Division'       => '/',
);
$operations = array_map(function($operation_name) use ($operations_map){
    return $operations_map[$operation_name];
}, $_POST['operation'] ?? []);  

// generate expressions using the specified random seed
srand($seed);
$expressions = array();
for ($i = 1; $i <= $num_questions; $i++) {
    $expression = generate_expression($num_operands, $operations, $num_digits);
    $expression_string = implode(" ", $expression);
    $new_string = str_replace("*","x", $expression_string);
    $expression_string1 = implode(" ", $expression);
    $result = eval("return ($expression_string1);");
    $expressions[] = compact("expression", "result");
}

return compact(
    'play',
    'seed',
    'num_digits',
    'num_questions',
    'num_operands',
    'operations',
    'expressions',
    'duration'
   )  ;
}        

function output_slideshow_header($data) {
extract($data);
if (!empty($operations)) {
    $num_operations = count($operations); //count selected operation
    ?>
 <div class="row">
    <div class="col-sm-4">
          <select class="choices">
            <option value="1">Your choices are here !</option>
            <option value="2"><div style= 'font-size: 25px; color:  #ffe6e6;'><i>You have selected <?php echo count($operations) ?> operation(s): <?php echo implode(', ', $operations); ?></i></div><br></option>
            <option value="3"><div style= 'font-size: 25px;color:  #ffe6e6;'><i>No. of questions: <?php echo $num_questions ?></i></div></option>
            <option value="4"><div style= 'font-size: 25px;color:  #ffe6e6;'><i>Numbers you want: <?php echo $num_operands ?></i></div></option>
            <option value="5"><div style= 'font-size: 25px;color:  #ffe6e6;'><i>No. of digits: <?php echo $num_digits ?></i></div></option>
         </select>
   </div>
      
    <div class="col-sm-4">  
          <div class="timer" style= 'font-size: 25px;color:  #ffe6e6;'>
            <?php  echo "Duration:" ?><time id="countdown"><script> var seconds = <?php echo  $duration * 60; ?>;</script>     
                          <?php echo $duration; ?>
             </time>
          </div>
    </div>

   <div class="col-sm-4"><!--exit button-->
           <button type="button" class="exit" 
    onclick="window.location.href='http://localhost/copied/final_generate/slideshow.php'">X</button>
              </div>
             </div>
      <?php
       }
       }

 function output_slide($slide_data, $index=1) {
$expression = $slide_data['expression'];
$result = $slide_data['result'];
?>
<div class="slide flex" id="out">
    <h3 class="ta-center">Q(<?php echo $index+1 ?>)</h3><br><br>
            <div class="expression flex flex-row" id="exp">
      <?php
        //logic 1
       // $i=0;
      $iCount = count($expression);
         foreach($expression as $key => $item) { ?>
            <span class="slide-item">      
                <?php 
                  if($key == 0 &&  $key < $iCount -1)
                { 
                  echo $item;     
                }
                else
                {
                  echo $item.expression[$key+1];
                  //$ke++;  011
                }           
    ?>

     </span><!--displaying each item at a time-->
<?php  }  ?>

        <span class="slide-item" id="equal"> = </span>
        <span class="slide-item">
            <span class="answer flex" id="place">
                <span class="answer-placeholder">
                   ****
                </span>

                <span class="answer-content"><!--displaying result-->
                   <?php echo number_format((float)$result, 2, '.', ''); ?>
                </span>
            
            </span>
        </span>
    </div><br><br><br><br><br><br><br><br>
     <center><div class="dotin"><?php
        for ($j=0; $j<count($expression); $j++) {
            ?><a class="dots-cont" onclick="goToItem(<?php echo $j ?>)"></a><?php
        }
    ?></div></center>

<a class="show-answer button" onclick="goToItem(<?php echo $j ?>)">SHOW ANSWER</a>
</div>
      <?php
      }

  function output_slideshow_controls($data) {
  ?>
<!--  go to next element of expression , for inner slider-->
<a class="goto-next-slide overlay-button overlay-right">Next</a>    
<a class="goto-prev-slide overlay-button overlay-left">Previous</a>
       
<div class="slideshow-controls center">
    <!--<a class="button goto-prev-slide">Previous</a>-->
     <!--<a class="button goto-next-slide">Next</a>-->
 
   <div class="flex flex-center"><?php
        for ($i=0; $i<count($data['expressions']); $i++) {
            ?><a class="dot" onclick="goToSlide(<?php echo $i ?>)"></a><?php
        }
    ?></div>
</div>

   <?php
  }

 function output_slideshow($data) {
   ?>
<div class="slideshow flex flex-column flex-center">
    <?php output_slideshow_header($data); ?>
    <div id="slides" class="slideshow-stage flex flex-center flex-column"><?php
        for ($i=0; $i<count($data['expressions']); $i++) {
            output_slide($data['expressions'][$i], $i);
        }
        ?>
    </div>

    <?php output_slideshow_controls($data); ?>
</div><?php
}

hey can you guide me how to aplly this logic with isset , I am not able to apply it correctly

@chorn suppose for example I hve an array which contain expression
$exp = array(β€˜2’, β€˜+’, β€˜3’, β€˜-’, β€˜1’);
and I want to use the modulo-operator to trigger only odd keys and show the prvious one, with a check on isset() so that the I will get the output in this manner
2
+3
-1

How can I do it?

<?php 
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'true');

$expression = array('2', '+', '3', '-', '1');

echo '<pre>';
	print_r($expression);
echo '</pre>';	
echo '<hr>';

echo '<span class="slide-item">';      
	$sTotal = '';
	foreach($expression as $key => $item) 
	{ 
	$numeric = is_numeric($item);	
	# echo '<br>' .$item .'==> ';	var_dump($numeric);
	if( $numeric ):
 		echo $item;
	  $sTotal .= $numeric;
	else:
	  echo '<br>' .$item;
  	$sTotal .= '&nbsp;' .$item .'&nbsp;';
	endif;  

	} 
echo '<span>';
echo '<br><br>sTotal ==> ' .$sTotal;

Output:

1 Like

hey thanks a lot but i did that just a few minutes ago and its working now .

1 Like

@ John_Betong can yo u tell me that how can I display β€œx” sign as multiplication sign instead of β€œ*” , I tried to just replace it at a time of echo and also by using string replace but its not showing eval function is showing error

...
...
	if( $numeric ):
 		echo $item;
	  $sTotal .= $numeric;
	else:
	  if('*'===$item):
	  	$item='X';
	  endif;	
	  echo '<br>' .$item;

  	$sTotal .= '&nbsp;' .$item .'&nbsp;';
	endif;  


Edit:

val function is showing error

Please check the PHP Online Manual concerning eval(…)* because I don’t think it is going to do what you want it to do.

https://secure.php.net/manual/en/function.eval.php

1 Like

Ok i will check it , Thanks again for the help.

Wouldn’t that be better as

else:
          $dispitem = $item;
	  if('*'===$item):
	  	$dispitem='X';
	  endif;	
	  echo '<br>' .$dispitem;

  	$sTotal .= '&nbsp;' .$item .'&nbsp;';
	endif;  

I haven’t read the completed code, just thinking that if $sTotal is then being passed to eval() sometime later, you’ve still changed * to x which will cause an issue there, as opposed to leaving it as the normal sign but just displaying it differently.

2 Likes

Hey the issue has been solved I just add an if condition at the time of echoing the output and its start working Thanks anyways for your suggestions

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.