1, 'Range1End'=>10, 'Range2Start'=>1, 'Range2End'=>10, 'Iterations'=>1, 'RowClassRepeat'=>3, 'NumColumns'=>5) as $VarName => $Default) ${$VarName}=intval(GetParam($VarName, $Default)); foreach(Array('NumColumns', 'RowClassRepeat') as $Name) //Remove possible division by 0s on integers if(${$Name}==0) ${$Name}=1; //Process other parameters $IsGenerated=isset($_REQUEST['Generate']); foreach(Array('MakeRandom', 'IncludeAnswers', 'IncludeTitle') as $VarName) //Booleans (Default true) ${$VarName}=(!$IsGenerated || isset($_REQUEST[$VarName])); $FullTypeNames=Array('Add'=>'Addition', 'Sub'=>'Subtraction', 'Mul'=>'Multiplication', 'Div'=>'Division'); $MathType=GetParam('MathType', 'Add'); $MathType=(!isset($FullTypeNames[$MathType]) ? 'Add' : $MathType); //If MathType is invalid, set it to Add $Title=$FullTypeNames[$MathType]." (${Range1Start}-$Range1End)x(${Range2Start}-$Range2End)"; //Title //Output the form to generate tables function EHTML($S) { return htmlspecialchars($S, ENT_QUOTES, 'UTF-8'); } //Escape for HTML function TextBox($Name) { global ${$Name}; print ""; } //Output a text input ?> Math Facts<?=($IsGenerated ? ": $Title" : '')?>
Range 1 Start
Range 1 End
Range 2 Start
Range 2 End
Iterations
Math Type $Label) print "\t\t\t$Label
\n"; ?>
Make Random>
Include Answers>
Include Title>
Number of Columns
How often row class repeats
Make sure to confirm your page setup (portrait/landscape, prints backgrounds [including colors], margins, etc). Confirm before first print with a print preview.
$Title"; //Functions to solve the math types function Add($x, $y) { return $x+$y; } function Sub($x, $y) { return $x-$y; } function Mul($x, $y) { return $x*$y; } function Div($x, $y) { return $x/$y; } //Function to check if a math combination is valid function CheckAdd($x, $y) { return true; } function CheckSub($x, $y) { return $x>=$y; } function CheckMul($x, $y) { return true; } function CheckDiv($x, $y) { return $x%$y==0; } //Static parameters $MathSymbols=Array('Add'=>'+', 'Sub'=>'-', 'Mul'=>'x', 'Div'=>'รท'); $ShowMathSymbol=' '.$MathSymbols[$MathType].' '; $CheckFunc="Check$MathType"; //Create the valid facts $Nums=Array(); //[]=Array(DisplayFact, Result) for($x=0;$x<$Iterations;$x++) for($i=$Range1Start;$i<=$Range1End;$i++) for($n=$Range2Start;$n<=$Range2End;$n++) if($CheckFunc($i, $n)) $Nums[]=Array(FixNum($i).$ShowMathSymbol.FixNum($n), $MathType($i, $n)); function FixNum($S) { return strtr(str_pad($S, 2, ' ', STR_PAD_LEFT), Array(' '=>' ')); } //Pad a number to 2 spaces //Randomize the facts $NewNums=Array(); if($MakeRandom) while(count($Nums)) $NewNums=array_merge($NewNums, array_splice($Nums, rand(0, count($Nums)-1), 1)); else //If not randomizing $NewNums=$Nums; //Output a table - No need to duplicate code since both the facts and answer table are generated the same function OutputTable($TableNum) { global $NewNums, $NumColumns, $RowClassRepeat; $RowNum=-1; foreach($NewNums as $i => $N) { if($i%$NumColumns==0) { $RowNum++; print ($i!=0 ? '' : '').''.chr(ord('A')+($i/$NumColumns)).')'; } print ''.$N[$TableNum].($TableNum==0 ? ' = ___' : '').''; } $ColsToFillIn=$NumColumns-(count($NewNums)%$NumColumns); if($ColsToFillIn!=$NumColumns) for($i=0;$i<$ColsToFillIn;$i++) print ' '; print ''; } //Output the facts print ''; OutputTable(0); //Output the answers if($IncludeAnswers) { print '
'; OutputTable(1); } ?>