SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Aug 25, 2001, 10:45 #1
- Join Date
- Apr 2001
- Posts
- 36
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Anybody willing to help a newbie to PHP?
I'm gonna jump in here with both feet.
I've made little "mad libs" type pages using PHP and now I want to take it a baby step further. What I'm trying to do is this:
I want to set up a page that has a form where the user will enter answers to math problems (I have this page all set up and ready to go) and on submitting the page, it will take you to a page that is set up just like the form, only when their answer is correct, it shows aand if it's wrong it shows a
I'd also like the script to keep a tally of how many are correct or how many are incorrect. Now, I'm figuring that to do the first part, my php will have to look like this:
PHP Code:<?php
if ("42" == $answer1) { ?>
<!--<img src="grin.gif">-->
<?php } else { ?>
<!--<img src="frown.gif">-->
<?php } ?>
Moving forward, question 2, am I being at all efficient by putting this into 81 seperate if/else statements? Is there an easier way? Each problem with it's answer will be in a seperate cell of a table, so I don't think it could get easier than this, but I'm not sure.
I may eventually adapt this to use a database for storing student scores, but that's a whole other kettle of fish.
I'd appreciate any help y'all can give me.
Thanks,
SusanLast edited by Susan; Aug 25, 2001 at 10:49.
-
Aug 25, 2001, 12:58 #2
- Join Date
- Nov 1999
- Location
- Brisbane, Australia
- Posts
- 682
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
hey Susan
i spent all day trying to grasp the php basics hehe and made my first little link redirection script
just learnt this so to increment by 1 you can
PHP Code:$correct = "0"
$correct++
-
Aug 25, 2001, 15:21 #3
- Join Date
- Aug 2001
- Posts
- 22
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well I would personally not use 80 if statements. It is hard to tell exactly what you need to do because i didn't see the table etc... but I would work at making a foor loop or a while loop to create your test function. If you have a link to you form possibly I could give more ideas.
P.S. I would also dynamically generate the problems so that you do not have to write a form with 80 or 90 questions and answers. If you want any ideas on this feel free to ask.Microsoft’s Motto: Resistance is futile, you will be assimilated…
-
Aug 25, 2001, 23:53 #4
- Join Date
- Jun 2000
- Location
- Sydney, Australia
- Posts
- 3,798
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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>";
Code:2 3 4
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>';
}
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>';
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>
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";
Last edited by freakysid; Aug 26, 2001 at 16:47.
-
Aug 26, 2001, 08:25 #5
- Join Date
- Apr 2001
- Posts
- 36
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for your help, guys. Freakysid, I'll look at this in depth today. (last night it made my eyes glaze over.
)
-
Aug 26, 2001, 10:01 #6
- Join Date
- Aug 2001
- Posts
- 22
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I, frankly, did not want to say everything freakysid did. Freakysid you did a good job!! Another way you could make it more dynamic would be use some of the cool things freakysid mentioned and randomly generate numbers for the problems then have the pc process it for the answers.
For example:
Say if you have a page that is all addition and subtraction then two (or more) arrays could be fed random numbers ranging from -10 to 10 (or what ever) then the computer could add them together to get the answer and you could display them by echo("$arra[1] + $arrb[1]); or something similar. then another answer array could be made (a for loop would be good here)
PHP Code:$numofquestions = 30; // Set number of questions
srand (microtime() * 1000000); //seed the random generator
for ($i = 0; $i < $numofquestions; $i++){
arra[$i] = rand(0, 20) - 10; // return values from -10 to 10
arrb[$i] = rand(0, 20) - 10; // return values from -10 to 10
arrc[$i] = arra[$i] + arrb[$i]; // Add the two together
echo("Add the following: arra[$i] + arrb[$i]");
}
GOOD LUCK!!Microsoft’s Motto: Resistance is futile, you will be assimilated…
Bookmarks