Monday, May 14, 2012

Multiplication Worksheet Practice PHP

For some reason, this post isn't showing up right.  Click here for the Google Doc.

On Saturday, I figured out how to make a times table drill with PHP.  The main issue I had to figure out were:
  • How to get an array of text boxes as input for the user
for ($j=1; $j <=5; $j++)
{
 $k = $k + 1;
 echo "";
 $a[$k]= mt_rand(1,12);  //$a[] random factor in range [1..12]
 $ans[$k] = $a[$k]*$factor;  //$ans[] array stores the actual answers
 echo $a[$k] . "
×".$factor."
";  //ASCII 215 is the multiply character
echo""; //textbox to get input - stores user's answers in array useans[]
echo "

";
//echo $ans[$k].
echo "
"; 
     }
 

  • How to pass arrays from the worksheet (multiply.php) to the results page (multiply2.php).  The key here was to use hidden input types!  I had to get use to the syntax:
//This is in multiply.php - within the submit form
for ($k=1;$k<=25; $k++)
{
/*note use of hidden input to store values of array a[] so it can be   passed to multiply2.php */
   echo "";
   echo "";
}
echo "";
//This is in multiply2.php
$useans = $_POST['useans'];
//pass array from entered into hidden fields of the form in multiply.php
$rightans = $_POST['ans'];
$factor = $_POST['factor'];
$a = $_POST['a'];
$timetaken= $_POST['TimeDisplayBox'];
  • Timing the drill (ended up using Javascript from W3Schools, but had to zero the timer when stop is clicked)
  • Passing the result of the timing to the results page.  At first I thought you needed Ajax, but it was simpler.  I moved the textbox for the timing from a separate form into the same one I had to collect all the data (and which was submitted to multiply2.php).  
So, in multiply.php I had
echo "";

and in multiply2.php I had
$timetaken= $_POST['TimeDisplayBox'];

These links were helpful for passing arrays in PHP:
http://mrarrowhead.com/index.php?page=php_arrays.php

This page shows how to pass an array via the URL, it worked, but it didn't work the way I wanted
http://stackoverflow.com/questions/1548159/php-how-to-send-an-array-to-another-page


No comments:

Post a Comment