Solved

How can I tell which variable is missing?

Posted on 2013-12-21
8
265 Views
Last Modified: 2013-12-23
A user takes a test, but they fail to answer a couple of questions. I need to determine which questions they didn't answer and insert those instances into a table.

Right now, my quiz table looks like this:

quiz

id, class_content_id, question,     answer,       correct_answer, question_id
1             27                 blah, blah     blah,blah          Y                       1
2             27                 blah, blah     blah, blah                                  1
3             27                 blah, blah     blah, blah                                  1
4             27                 blah, blah     blah, blah                                  2
5             27                 blah, blah     blah, blah         Y                       2
6             27                 blah, blah     blah, blah                                  3
7             27                 blah, blah     blah, blah                                  3
8             27                 blah, blah     blah, blah                                  3
9             27                 blah, blah     blah, blah         Y                       3

...there's more than one question, but hopefully you can trace my logic. You've got several questions, each is a multiple choice question so you have several possible answers, but the correct answer is distinguished by the presence of the "Y" under correct answer.

When the system goes to correct the test, it uploads each response into a separate table in order to first determine whether or not all of the questions were answered. That table looks like this:

quiz_finish

id         class_content_id         question_id            session_number
1               27                                    1                            12345
2               27                                    2                            12345

I'll count the number of rows in the quiz_finish table and then compare that to the number of distinct rows in the quiz table, based on the question_id.

I'm at that point now where I need to figure out which question specifically was left unanswered. I've played around with a couple of options, but I'm not getting it done.

I began by looping the quiz_finish table and attempting to compare that to the quiz table using a for loop, thinking that I could establish $i as the actual question id, and do a comparison and while I think that could do it, I've yet to get it to work.

Any ideas?
0
Comment
Question by:brucegust
8 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 150 total points
ID: 39734147
I think you want to change the table structure (use ALTER TABLE) from this:

id, class_content_id, question, answer, correct_answer, question_id

to something like this:

id, class_content_id, question, was_answered, answer, correct_answer, question_id

By adding the additional column you will be able to keep track of more information.
0
 

Author Comment

by:brucegust
ID: 39734160
I smell what you're cooking', Ray! Let me give it a whirl!
0
 
LVL 14

Assisted Solution

by:ThG
ThG earned 50 total points
ID: 39735210
Either i'm not following the problem or there is some serious problem with your tables structure.
Can you give the detailed structure of your tables? Can you give a real example, because "blah blah" is not clear.

So I'd expect to see in quiz_finish which answer was given to which question! Please provide more data so we can help you better.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 150 total points
ID: 39735768
Assuming you will have also "N" in the correct answer column, you can use the query - yes, not very effective one - to see what questions were missed

select distinct(question_id) from quiz where question_id not in(select distinct(question_id) from quiz where correct_answer is not null)

Open in new window

0
 
LVL 13

Assisted Solution

by:AielloJ
AielloJ earned 150 total points
ID: 39735990
brucegust:

Have you tried using an outer join to identify the missing ID's?  It's simple and straightforward.  You may need to add the class_content_id to the query.

SELECT
  Quiz.id
FROM
  Quiz
LEFT JOIN
  Answers
 ON
  Answers.id = Quiz.id
WHERE
  Answers.id IS NULL

Best regards,

Aielloj
0
 

Author Comment

by:brucegust
ID: 39736787
Hey, Folks!

Forgive me if I wasn't as detailed in my explanation as I should've been. Here's what wound up working:

The problem I was having is that my code was looking at the answers to a multiple choice quiz and if the answer was wrong, I would throw that question id into a table and then look to see which question and how many questions were answered incorrectly to determine whether or not they and passed.

Problem was in that it didn't accommodate the situation where the user left a question blank. I tried a couple of different scenarios including a join, but nothing worked until I did this:

I threw all of the questions that had been answered into a separate table called "quiz_finish." I then used a "for" statement to loop through all of those questions that hadn't been answered according to "i" which was the number of questions in the actual quiz.

As I looped through things, since "i" was also the question_id number, I could figure out where the blanks were and then throw those questions into another table called quiz_not_answered. The result was a list of the questions that weren't answered so I could then come up with an accurate score as well as refer to the questions that weren't answered as opposed to those that were answered incorrectly.

$perry="select question_id from quiz_finish where session_number='$_SESSION[number]' order by question_id";
$perry_query=mysqli_query($cxn, $perry)
or die("Couldn't make Perry happen.");
$perry_count=mysqli_num_rows($perry_query);
if($perry_count<>$jennifer_count)
{
//here's where I'm figuring out which question didn't get answered
      for($i=1; $i<=$jennifer_count; $i++)
      {
      $rob="select question_id from quiz_finish where question_id='$i' and class_content_id='$the_class_content_id' and session_number='$_SESSION[number]'";
      $rob_query=mysqli_query($cxn, $rob)
      or die("Rob didn't happen.");
      $rob_count=mysqli_num_rows($rob_query);
            if($rob_count>0)
            {
            continue;
            }
            else
            {
            $sharon="insert into quiz_not_answered (class_content_id, question_id, session_number) values ('$the_class_content_id', '$i', '$_SESSION[number]')";
            $sharon_query=mysqli_query($cxn, $sharon)
            or die("Couldn't make Sharon happen.");
            $question_not_answered=$question_not_answered+1;
            }
      }
}

It might not be the most streamlined approach, but it worked.

Thanks for the feedback!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39737021
Now that we understand what the real question is all about, you might want to consider posting a question to the effect of, "What is the standard design pattern for a multiple-choice online test?"  It's pretty well understood nowadays.  Best regards, ~Ray
0
 

Author Comment

by:brucegust
ID: 39737089
I'll do that, Ray. Thanks!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
designing in object programming 12 79
Curl & PHP Command Help 4 23
Creating Functions in phpMyAdmin 8 18
PHP error function not working on AWS 10 11
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question