0

I am compairing two tables and on mysql it shows the once that are not matched as NULL. How would I go onto printing this on a php.

so the once that are matched should just print the value. and the once that are not matched should print the original value.

the table structure is
Question: Que_ID, Que_Question, Que_Choice1, Que_Choice2, Que_Choice3, Que_Choice4, Que_Answer1, Que_Answer2, Que_Answer3, Que_Answer4

Answer: Ans_ID, Ans_Answer1, Ans_Answer2, Ans_Answer3, Ans_Answer4, Que_ID, Use_ID

I am using LEFT join to compare these tables.

SELECT question.Que_ID, Que_Answer1, Que_Answer2, Que_Answer3, Que_Answer4, Ans_Answer1, Ans_Answer2, Ans_Answer3, Ans_Answer4 
FROM question 
    LEFT JOIN answer ON question.Que_ID=answer.Que_ID AND question.Que_Answer1=answer.Ans_Answer1 AND answer.Use_ID=1
2
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by Insensus
0

Maybe you can post a little more about the structure of your table, maybe some sample data?

I can't really visualize what you're trying to accomplish right now.

0

The question that the user got it right will be displayed as checkbox and it will be ticked and the once the user got it wrong will displayed no ticked but just the question and the choices. Now I want to say they got that question wrong and give them the right answer.

$intNumber = 1;
$sql=correctanswer($_SESSION['username1'], $_SESSION['smodule']);

This is a function and the query that I pasted earlier on will be used here.

echo "<strong>the ones are ticked is right answer </strong><br />\n";

 while($info = mysql_fetch_array( $sql )) { 
    echo " <strong> $intNumber,  {$info['Que_Question']} </strong><br />\n";

    if($info['Ans_Answer1'] == '1') {
    echo "<input type=\"checkbox\" name=\"choice1[]\" value='1'\" checked> ";
    echo "{$info['Que_Choice1']} <br />\n";
    }
        else {  

                echo "<input type=\"checkbox\" name=\"choice1[]\" value='1'\" {$info['Que_Choice1']}\" /> ";
                echo "{$info['Que_Choice1']} <br />\n";
            }

    if($info['Ans_Answer2'] == '1') {
    echo "<input type=\"checkbox\" name=\"choice2[]\" value='1'\" checked> ";
    echo "{$info['Que_Choice2']} <br />\n";
    }
        else {  
                echo "<input type=\"checkbox\" name=\"choice2[]\" value=\" {$info['Que_Choice2']}\" /> ";
                echo "{$info['Que_Choice2']} <br />\n";
            }

    if($info['Ans_Answer3'] == '1') {
    echo "<input type=\"checkbox\" name=\"choice3[]\" value='1'\" checked> ";
    echo "{$info['Que_Choice3']} <br />\n";
    }
        else {  
                echo "<input type=\"checkbox\" name=\"choice3[]\" value=\" {$info['Que_Choice3']}\" /> ";
                echo "{$info['Que_Choice3']} <br />\n";
            }
    if($info['Ans_Answer4'] == '1') {
    echo "<input type=\"checkbox\" name=\"choice4[]\" value='1'\" checked> ";
    echo "{$info['Que_Choice4']} <br />\n";
    }
        else {  
                echo "<input type=\"checkbox\" name=\"choice4[]\" value=\" {$info['Que_Choice4']}\" /> ";
                echo "{$info['Que_Choice4']} <br />\n";
            }
    $intNumber++; 
    } 

Edited by mike_2000_17: Fixed formatting

0

Could you maybe give an example question and choices showing what each table-field represents in a question? Because I see you're not using the Que_Answer1-4 for example.

0

Question ID= 1
Question= What is your name
Choice1= Nick
Choice2= John
Choice3= Lisa
Choice4= Lilly
Answer1= 0
Answer2= 0
Answer3= 0
Answer4= 1

That is an example of question which is being poster by a staff or admin.

When a user takes this exam then the answer will be saved in answer table.
eg of answer table
answer ID= 1
answer1 = 0
answer2 = 0
answer3 = 0
answer4 = 1
Question id=1
User ID= 3

When I compare the question table with answer table this should output a message saying correct with the answer displaying. If the user entered answer3 then wrong message should display.

Have I lost you???

0

Nope you've just got me back. :)

If the number of choices doesn't differ (always 4) then this approach is good enough so I won't talk about that unless you want me to of course.

Now this still leaves one way to make your system a little less complex by changing the 4 fields Answer1-4 to one Answer field which simply contains an integer refering to the correct answer.
The same can then be done for your Answers table so you'd get something like:

Que_Question_ID= 1
Que_Question= What is your name
Choice1= Nick
Choice2= John
Choice3= Lisa
Choice4= Lilly
Que_Answer= 4
Ans_Answer_ID= 1
Ans_Answer= 4
Question_id=1
User_ID= 3

This allows for some simplifications in your PHP code:

while($info = mysql_fetch_array( $sql)) {
    echo " <strong> $intNumber, {$info['Que_Question']} </strong><br />\n";

    // Loop the possible answers instead of writing everything manually
    for($i = 1; $i <= 4; $i++) {
        echo "<input type=\"checkbox\" name=\"choice{$i}[]\"";

        // Make the checkbox checked if this is the submitted answer number
        echo ($i == $info['Ans_Answer']) ? " checked" : "";

        // Give it a green background if it's the correct answer
        // This is something you'd want to change yourself, just showing how it's done
        echo ($i == $info['Que_Answer']) ? " style=\"background: #0f0;\" " : "";

        echo " /> {$info['Que_Choice'.$i]} <br />\n";
    }
    $intNumber++;
}

I hope this is enough to at least get you started.

0

That is much easier way, but my little problem is I kind of got checkbox for all the other webpages and done my design and everything. Changing this would take me a little while and need to sort most of this stuff by tomorrow. so Kind of hard. However I will try it this way and see where it takes me.

Thanks for helping me but it would be great if you could possibly help me with Answer 1-4 :). If not its fine.

0

Well if you really want to keep the table format as you have it right now just replace the

$i == $info['Ans_Answer']

with

$info['Ans_Answer'.$i]

and

$i == $info['Que_Answer']

with

$info['Que_Anwer'.$i]
0

So far it just output the answers that the user have selected but the colour does not apply or anything like that. However I did play around with the code and when I input

echo "<input type=\"text\" name=\"choices{$i}[]\"";

that line as text then the colour appears. But with the button it does not work. Is there any other ways where I can still print the value they selected and print the correct answers.

Thanks for helping me :)

0

Hmm that's maybe because I just created that from the top of my head and checkboxes don't actually have a visible background.
In that case, replace the

background: #0f0;

by

border: 1px solid #0f0;

Which should give the checkbox a green border.
It's not the most elegant way to do this, but then again it was just for demonstration.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.