Page 1 of 1

if then else Syntax

Posted: 2016-08-29 04:04
by buck1942
I think that I have a problem with syntax in my if then else statement:

1 function questions_after_insert($data, $memberInfo, &$args){
2
3 // Compute Correct Answer
4 $id = makeSafe($data['selectedID']);
5 sql("UPDATE questions set correct_answer = number_1 + number_2 ", $eo);
6 if ($data['your_answer'] == $data['correct_answer']){
7 //if (8 == 8){
8 // answer is correct
9 sql("UPDATE questions SET number_correct = '1' WHERE id ='$id' ", $eo);
10 }else{
11 // answer is incorrect
12 sql("UPDATE questions SET number_correct = '0' WHERE id ='$id' ", $eo);
13 }
14 return TRUE;
15 }

I think that my syntax in line 6 is wrong. I store "your-answer" and "correct_answer" in my file, thus, I can see that the values are in fact equal, but the routine always falls thru to line 12.

If I disable line 6 and enable line 7, all works well.

I am hoping someone can help... it seems to me that sometimes plain ole PHP syntax is fine and sometimes it is not (with Appgini)... I have not yet sorted things out.

Thank you,

Buck

Re: if then else Syntax

Posted: 2016-08-29 04:49
by peebee
If it's working when you remove line 6 and replace it with line 7, then sounds like line 6 is the problem?

if ($data['your_answer'] == $data['correct_answer']){

Line 6 looks OK but I see you make comment......

I store "your-answer" and "correct_answer" in my file......

Should it be "your-answer" or "your_answer" in line 6? Might explain why it's not working?

Re: if then else Syntax

Posted: 2016-08-29 12:29
by grimblefritz
The syntax is ok. You can run the file through PHP and check it:

Code: Select all

php -l tablename.php
If there are errors, you'll see them. They may also be written to the file 'error_log'.

As peebee says, the problem is in line six. Either the array element names are incorrect, or perhaps they are of different data types. Try adding this before line 6 and examine the $data array:

Code: Select all

echo '<pre>'; var_dump.print_r($data,1); echo '</pre>';
Are 'your_answer' and 'correct_answer' what you expected?

The print_r() function does not output the data type, but is somewhat easier for a human to digest. If needed, however, you can see the data types with this:

Code: Select all

echo '<pre>'; var_dump.print_r($data,1); echo '</pre>';
Either way, you're check that the $data array elements are named as you have typed them, and that they contain the values and types you expect.

Re: if then else Syntax

Posted: 2016-08-31 06:21
by buck1942
ALAS!!

Thanks for your suggestions, gentlemen... Using the print_r (function/construct) was a big help. I did not know that PHP had that ability.

The problem was that the "correct_answer" field was not being loaded. Took me a good while to figure how to update "correct_answer", but all is well... until I hit a another "bump" in the road... :o)

Buck