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
if then else Syntax
Re: if then else Syntax
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?
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?
-
- AppGini Super Hero
- Posts: 336
- Joined: 2015-12-23 16:52
Re: if then else Syntax
The syntax is ok. You can run the file through PHP and check it:
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:
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:
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.
Code: Select all
php -l tablename.php
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>';
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>';
Re: if then else Syntax
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...
)
Buck
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...

Buck