The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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 a and 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:

Now, I'm assuming I'll have to define a variable at the beginning of the script ($correct="0") and somewhere in there have it add 1 for every if statement that shows correct and at the end of the page, use that variable total by having it displayed in the html. How do I have it add one to that total? and where do I put it in relation to each of these if/else statements?

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.

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.

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:

Code:

2
3
4

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.

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):

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)