0

the project is a simple quiz for basic English grammar usage of A vs AN (grammar exercise for kids), I have an array with questions, instead of showing the questions in the same order, i want to shuffle the order every time the user refresh the page (for this I created an array of random numbers), but when they submit the answer with GET method, the user will see another page with the answers in the same dynamic previous order of random questions, so I can save them is a data base and know the exact random questions the user generated a previous

THIS IS WHAT THE USER SEE

<!DOCTYPE html>

<head> 

<title>A vs AN</title>

<link rel="stylesheet" href="den_Stil_des_Arbeitsblatts.css">

<?php 

//including array

include 'dieDatebank/dieTiere.php';

?> 

</head> 
 
<body>

<DIV id="optionsBox">

  <button onclick="dieFunktion_show_hide_answers()">Show / Hide Full Answers</button>

  <button id="btn-show" onclick="togglePlaceholder(this)">Delete Answers inside placeholder</button>

  <button onclick="dieFunktion_show_hide_spanish()">Show / Hide Spanish</button>

  <button onclick="dieFunktion_show_hide_ipa()">Show / Hide IPA</button>

  <button onclick="dieFunktion_print('printArea01')">Print</button>

  <button id="btn-reloadPage" onclick="dieFunktion_reloadPage()">New Worksheet</button>

</DIV>

<div id="centerDiv">
<div id="printArea01">
<h1>A VS AN</h1>
<p>
Name: ________________________
<br>
Total of words = <?php echo $NUM_TOTAL_PALABRAS; ?>
<br>
Total of questions = <?php echo $INT_TOTAL_QUESTIONS; ?>
</p>

<table>
<tr><th>N</th><th>EXERCISE</th><th>IPA</th><th>SPANISH</th><th>CHECK</th></tr>
<form action="gopage_checkanswers.php" method="GET">
   
        <?php
        for($i = 0; $i < 4; $i++)
        {
          //CALCULATE THE NUMBER OF THE EXERCISE
          $numberExercise = $i + 1;
          echo "<tr>
          <td>" .$numberExercise. "</td>
          <td><input type='text' class='classFrom' placeholder='".$ARRAY_RAND_QUESTIONS[$i][3]."' name='ENAME_".$numberExercise."'> ".$ARRAY_RAND_QUESTIONS[$i][0]."</td> 
          <td><span class='ipa'> ".$ARRAY_RAND_QUESTIONS[$i][1]."</span></td>
          <td><span class='spanisch'>  ".$ARRAY_RAND_QUESTIONS[$i][2]." </span></td>
          <td><span class='answer' => ".$ARRAY_RAND_QUESTIONS[$i][3]." ".$ARRAY_RAND_QUESTIONS[$i][0]."</span></td>
          </tr>";
          echo "<input type='hidden' name='NAME_KEY_".$numberExercise."' value='".$ARRAY_RAND_QUESTIONS[$i][3]."' />";
        
        }
        ?>
  
</table>
</div>
</div>
<br>
    <input type='hidden' name='NAME_TOTAL_NUMBER_QUESTIONS' value='<?php echo $INT_TOTAL_QUESTIONS; ?>' />
    <br>
    <input type="submit" value="CHECK ANSWERS" class="submitbtn" />
    <br>
    <input type="reset" value="RESET" class="submitbtn" />
</form>
<BR>
<button onclick="dieFunktion_show_hide_navbar()">OPTIONS</button>

<BR>

<script src="worksheet.js">
</script>
</body>
</html>

THIS IS THE PHP FILE WHICH GENERATES THE RANDOM QUIZ FROM THE ARRAY OF WORDS OF ANIMAL NAMES, this php file creates a new quiz every time the user restart the page

<?php
/* https://www.science.co.il/language/Codes.php
*/
$ARRAY_articles = array
(
  array("a","/ə/","un", "una"),
  array("an","/ən/ ","un", "una"),
  array("the","/ðə/ /ði/","el","la","los","las")
);

$an = $ARRAY_articles[1][0];
$a = $ARRAY_articles[0][0];

$MULTIDIMENTIONAL_ASSOCIATIVE_ARRAY_ANIMALS = array
(
  array
  (
    "KEY_EN"=>"adder",
    "KEY_IPA"=>"/ˈædər/",
    "KEY_ES"=>"víbora",
    "KEY_ARTICLE"=>$an
  ),
  array
  (
    "KEY_EN"=>"ant",
    "KEY_IPA"=>"/ænt/",
    "KEY_ES"=>"hormiga",
    "KEY_ARTICLE"=>$an
  ),
  array
  (
    "KEY_EN"=>"anteater",
    "KEY_IPA"=>"/ˈænˌtitər/",
    "KEY_ES"=>"oso hormiguero",
    "KEY_ARTICLE"=>$an
  ),
  array
  (
    "KEY_EN"=>"antelope",
    "KEY_IPA"=>"/ˈæntəˌloʊp/",
    "KEY_ES"=>"antílope",
    "KEY_ARTICLE"=>$an,
  ),
  array
  (
    "KEY_EN"=>"badger",
    "KEY_IPA"=>"/ˈbæʤər/",
    "KEY_ES"=>"tejón",
    "KEY_ARTICLE"=>$a
  ),



  array
  (
    "KEY_EN"=>"boar",
    "KEY_IPA"=>"/bɔr/",
    "KEY_ES"=>"jabalí",
    "KEY_ARTICLE"=>$a
  ),

 

  array
  (
    "KEY_EN"=>"buffalo",
    "KEY_IPA"=>"/ˈbʌfəˌloʊ/",
    "KEY_ES"=>"búfalo",
    "KEY_ARTICLE"=>$a
  ),
   

  array
  (
    "KEY_EN"=>"bull",
    "KEY_IPA"=>"/bʊl/",
    "KEY_ES"=>"toro",
    "KEY_ARTICLE"=>$a
  ),


  array
  (
    "KEY_EN"=>"panther",
    "KEY_IPA"=>"/ˈpænθər/",
    "KEY_ES"=>"pantera",
    "KEY_ARTICLE"=>$a
  ),
 


  array
  (
    "KEY_EN"=>"parakeet",
    "KEY_IPA"=>"/ˈpɛrəˌkit/",
    "KEY_ES"=>"periquito",
    "KEY_ARTICLE"=>$a
  ),
     

  array
  (
    "KEY_EN"=>"pheasant",
    "KEY_IPA"=>"/ˈfɛzənt/",
    "KEY_ES"=>"faisán",
    "KEY_ARTICLE"=>$a
  ),


  array
  (
    "KEY_EN"=>"pigeon",
    "KEY_IPA"=>"/ˈpɪʤən/",
    "KEY_ES"=>"paloma",
    "KEY_ARTICLE"=>$a
  ),


  array
  (
    "KEY_EN"=>"platypus",
    "KEY_IPA"=>"/ˈplætəˌpʊs/",
    "KEY_ES"=>"ornitorrinco",
    "KEY_ARTICLE"=>$a
  ),

  array
  (
    "KEY_EN"=>"porcupine",
    "KEY_IPA"=>"/ˈpɔrkjəˌpaɪn/",
    "KEY_ES"=>"puercoespín",
    "KEY_ARTICLE"=>$a
  ),

  array
  (
    "KEY_EN"=>"raccoon",
    "KEY_IPA"=>"/ræˈkun/",
    "KEY_ES"=>"mapache",
    "KEY_ARTICLE"=>$a
  ),

 

  array
  (
    "KEY_EN"=>"salamander",
    "KEY_IPA"=>"/ˌsæləˈmændər/",
    "KEY_ES"=>"salamandra",
    "KEY_ARTICLE"=>$a
  ),

  array
  (
    "KEY_EN"=>"reindeer",
    "KEY_IPA"=>"/ˈreɪnˌdɪr/",
    "KEY_ES"=>"reno",
    "KEY_ARTICLE"=>$a
  ),

  array
  (
    "KEY_EN"=>"seal",
    "KEY_IPA"=>"/sil/",
    "KEY_ES"=>"foca",
    "KEY_ARTICLE"=>$a
  ),


  array
  (
    "KEY_EN"=>"sloth",
    "KEY_IPA"=>"/sloʊθ/",
    "KEY_ES"=>"perezoso",
    "KEY_ARTICLE"=>$a
  ),


  array
  (
    "KEY_EN"=>"slug",
    "KEY_IPA"=>"/slʌg/",
    "KEY_ES"=>"babosa",
    "KEY_ARTICLE"=>$a
  ),

  array
  (
    "KEY_EN"=>"squirrel",
    "KEY_IPA"=>"/ˈskwərəl/",
    "KEY_ES"=>"ardilla",
    "KEY_ARTICLE"=>$a
  ),

  array
  (
    "KEY_EN"=>"stork",
    "KEY_IPA"=>"/stɔrk/",
    "KEY_ES"=>"cigüeña",
    "KEY_ARTICLE"=>$a
  ),

  array
  (
    "KEY_EN"=>"stag",
    "KEY_IPA"=>"/stæg/",
    "KEY_ES"=>"ciervo macho",
    "KEY_ARTICLE"=>$a
  ),

  array
  (
    "KEY_EN"=>"swan",
    "KEY_IPA"=>"/swɑn/",
    "KEY_ES"=>"cisne",
    "KEY_ARTICLE"=>$a
  ),

  
  array
  (
    "KEY_EN"=>"toad",
    "KEY_IPA"=>"/toʊd/",
    "KEY_ES"=>"sapo",
    "KEY_ARTICLE"=>$a
  ),
  
  array
  (
    "KEY_EN"=>"turkey",
    "KEY_IPA"=>"/ˈtɜrki/",
    "KEY_ES"=>"pavo",
    "KEY_ARTICLE"=>$a
  ),

  array
  (
    "KEY_EN"=>"viper",
    "KEY_IPA"=>"/ˈvaɪpər/",
    "KEY_ES"=>"víbora",
    "KEY_ARTICLE"=>$a
  ),


  array
  (
    "KEY_EN"=>"vulture",
    "KEY_IPA"=>"/ˈvʌlʧər/",
    "KEY_ES"=>"buitre",
    "KEY_ARTICLE"=>$a
  ),

  array
  (
    "KEY_EN"=>"vixen",
    "KEY_IPA"=>"/ˈvɪksɪn/",
    "KEY_ES"=>"zorra",
    "KEY_ARTICLE"=>$a
  ),


  array
  (
    "KEY_EN"=>"walrus",
    "KEY_IPA"=>"/ˈwɔlrəs/",
    "KEY_ES"=>"morsa",
    "KEY_ARTICLE"=>$a
  ),


  array
  (
    "KEY_EN"=>"weasel",
    "KEY_IPA"=>"/ˈwizəl/",
    "KEY_ES"=>"comadreja",
    "KEY_ARTICLE"=>$a
  ),



  array
  (
    "KEY_EN"=>"beaver",
    "KEY_IPA"=>"/ˈbivər/",
    "KEY_ES"=>"castor",
    "KEY_ARTICLE"=>$a
  )
  );

//find the total number of elements inside array $MULTIDIMENTIONAL_ASSOCIATIVE_ARRAY_ANIMALS
$NUM_TOTAL_PALABRAS = count($MULTIDIMENTIONAL_ASSOCIATIVE_ARRAY_ANIMALS);

//substract 1 to the total number of elements inside the array so I can create a rand number
$NUM_TOTAL_PALABRAS_MENOS_UNO = count($MULTIDIMENTIONAL_ASSOCIATIVE_ARRAY_ANIMALS) - 1;

//setting random number for each line inside the text
//creating random variables
for($i = 1; $i < 6; $i++)
{
  ${"RAND_NUM_".$i} = mt_rand(0,$NUM_TOTAL_PALABRAS_MENOS_UNO);
}
//--------------------------------------------------------------

//create array of the rand numbers variables, so they can be add dinamically later
$Arreglo_variables_num_azar = array($RAND_NUM_1,$RAND_NUM_2,$RAND_NUM_3,$RAND_NUM_4,$RAND_NUM_5);
//short array name
$num = $Arreglo_variables_num_azar;

$num_total_variables_num_azar_menos_uno = count($Arreglo_variables_num_azar) - 1;
//SHORT name
$MAA = $MULTIDIMENTIONAL_ASSOCIATIVE_ARRAY_ANIMALS;
//create an array of random elements taken from the CLASS_DICT_animals
$ARRAY_RAND_QUESTIONS = array();
for ($x = 0; $x <= $num_total_variables_num_azar_menos_uno ; $x++) {
  $ARRAY_RAND_QUESTIONS[] = array($MAA[$num[$x]]["KEY_EN"],$MAA[$num[$x]]["KEY_IPA"],$MAA[$num[$x]]["KEY_ES"],$MAA[$num[$x]]["KEY_ARTICLE"]);   
}

//total number of questions
$INT_TOTAL_QUESTIONS = count($ARRAY_RAND_QUESTIONS); 
//end php-------------------------------------------------------------------------------
?>

THIS IS THE PHP FILE WHERE I CAN SEE THE NUMBER OF RIGHT ANSWERS gopage_checkanswers . php

<?php
    $user_answer1 = $_GET['ENAME_1'];
    $user_answer2 = $_GET['ENAME_2'];
    $user_answer3 = $_GET['ENAME_3'];
    $user_answer4 = $_GET['ENAME_4'];

    $value1 = $_GET['NAME_KEY_1']; 
    $value2 = $_GET['NAME_KEY_2']; 
    $value3 = $_GET['NAME_KEY_3']; 
    $value4 = $_GET['NAME_KEY_4']; 

    $totalNumberOfQuestions = $_GET['NAME_TOTAL_NUMBER_QUESTIONS'] - 1;

$totalCorrect = 0;
    
if ($user_answer1 == $value1) { $totalCorrect++; }
if ($user_answer2 == $value2) { $totalCorrect++; }
if ($user_answer3 == $value3) { $totalCorrect++; }
if ($user_answer4 == $value4) { $totalCorrect++; }
   

 ?>

<?php echo "TOTAL: $totalCorrect / $totalNumberOfQuestions"; ?>

MY QUESTION IS THIS CODE, i want to dynamically populate this array with 100 $RAND_NUM_n variables, i dont want to hardcore those 100 custom variables names, so when I add a new animal word in my array of animals names, this $Arreglo_variables_num_azar array is updated automatically

$Arreglo_variables_num_azar = array($RAND_NUM_1,$RAND_NUM_2,$RAND_NUM_3,$RAND_NUM_4,$RAND_NUM_5);
  • Allow me to stop you before you make a giant, regrettable mess of your application. Do not apply numeric suffixes to field names. This will have a butterfly effect on your project and you will not be able to cleanly process related data with array functions. Whenever you think you need "variable variables", this almost always indicates that there is a flawed programming design. Instead, you need to rebuild your process starting from the HTML form. Use array syntax like `name="fieldName[$i]"` (or similar) so that your receiving PHP code can work sensibly. – mickmackusa Jun 27 '22 at 00:26

1 Answers1

0

Here is a simplified version on how to use variable names dynamically.

$RAND_NUM_1 = 'Hello';
$RAND_NUM_2 = 'World';
$RAND_NUM_3 = 'Foo';
$RAND_NUM_4 = 'Bar';
$RAND_NUM_5 = 'Baz';

$array = [];
for($i = 1; $i <= 5; $i++) {
    $array[] = ${"RAND_NUM_$i"};
}

print_r($array);

and will print

Array
(
    [0] => Hello
    [1] => World
    [2] => Foo
    [3] => Bar
    [4] => Baz
)
Markus Zeller
  • 8,516
  • 2
  • 29
  • 35
  • @Markus this question was asked over the weekend (and was closed). This is an XY Problem. The way to resolve this problem is not to use variable variables, but to better design the HTML form so that sensible array functions can be used on array data. – mickmackusa Jun 27 '22 at 00:28