Following on from thebobert suggestions, when you need to perform the same process on a set of data, this is where arrays and loops come in handy. An array is a set of data. And in the example below you will see how it is handy to hold your data in an array and loop through the array when you need to apply the same processing to each element:
Clumsy
PHP Code:
$value1 = 1;
$value2 = 2;
$value3 = 3;
$value1++;
$value2++;
$value3++;
echo "$value1 <br> $value2 <br> $value3 <br>";
output:
Less clumsy (traditional programing structure - the for loop):
PHP Code:
// create an array which has three elements
// which are the values 1, 2, 3
$values = array(1, 2, 3);
// loop through (traverse) the array and for each
// array element increment its value by one
// and print it out on a new line
for( $i = 0; $i < count($values); $i++ )
{
$values[$i]++;
echo $values[$i], '<br>';
}
results should be the same as above - you can see the amount of code this replaces if say you had 80 different values you wanted to process.
Some points to be made about the above.
1) You can initialise an array using the array() function.
eg : $values = array(1, 2, 3);
or
alternatively, you can append onto an the end of an array like so:
$values[] = 4;
$values[] = 5;
now $values will hold the elements 1, 2, 3, 4 ,5 (a total of five elements).
2) You access a specific element in an array by its index number. Note that index numbers start at zero, so the index number of the first element in the array will be zero. That is, according to my example above
$values[0] == 1
$values[1] == 2
...etc...
3) for ( $i = 0; $i < count($values); $i++ )
Breaking this down it is saying:
i) initialize a counter $i to be equal to zero
ii) while the value of the counter is still less that the total of the elements in the array... do what is inside the for loop
iii) once the statements inside the for loop have been executed, increment the counter by one and then re-evaluate the condition in ii) to decide whether to enter the for loop again or to finish looping and move on.
You can create an array in your HTML form as well. Here is an example. Lets say you will create a form with three questions. This time, because its easier in this case, I am going to use the foreach control structure, which you can see is just a simplified version of the for structure (it traverses the array and for each iteration copies the value of the next element in the array into the variable $q):
PHP Code:
$questions = ('1 + 2', '3 * 3', '5 - 1');
$i = 1;
echo '<form name="frmMathQuestions" action="answers.php" method="POST">';
foreach($questions as $q)
{
echo "<br>Q$i++: What is $q ? ",
'<input type="text" name="answers[]" value="" />';
}
echo '<input type="submit" name="submit" value="submit"></form>';
This (hopefully) will output some HTML that will look a little like this:
Code:
<form name="frmMathQuestions" action="answers.php" method="POST">
<br>Q1: What is 1 + 2 ? <input type="text" name="answers[]" value="" />
<br>Q2: What is 3 * 3 ? <input type="text" name="answers[]" value="" />
<br>Q3: What is 5 - 1 ? <input type="text" name="answers[]" value="" />
<input type="submit" name="submit" value="submit"></form>
To process this form, the script answers.php will look like:
PHP Code:
// note: the array $answers will hold the values POSTed from the form
$correctAnswers = array(3, 9, 4);
$totalCorrect = 0;
for ( $i = 0; $i < count($correctAnswers); $i++ )
{
if ($answers[$i] == $correctAnswers[$i])
{
echo 'happy.gif';
$totalCorrect++;
}
else
{
echo 'sad.gif';
}
}
echo "Your total score is : $totalCorrect";
Hope that helps. NOTE: none of this code is tested - its just to illustrate the concepts - best read the manual for each of the control strucures I've mentioned.
Bookmarks