Jump to content

highlighted incorrect responses bases on PHP/MySQL results data


Recommended Posts

Hi, I want to make an alteration to a page that reports results of an online exam, and one column displays the incorrect responses. For example if someone missed questions 1, 5 and 10, this is recorded in a field in the MySQL named incorrect_resp. These results are then echoed out into the HTML page. What I want to do if possible is to code the exam question and answers on the page and using CSS hilight the incorrect responses in a yellow background or something like that. What I need to figure out is how to tie the two together. How can I set it up so it will display <td class="wrong"> for a wrong answer that is displayed in mySQL data?

 

let me know if I need to explain anything more or supply some code.

well you would have to have a function called wrong that detects if the answer is wrong or not. However you determine if the answer is wrong you would put in that if statement.

 

How do you determine when an answer is wrong?

ahh ok, So if you have an array of all the answers, you can iterate through that array to display the answers, and then have an array with all the wrong answers, and use the in_array() function to tell whether or not to highlight the text

Oh ok sure.

 

$wrong = array(1, 4, 3, 10);//You will get this from your table, but for simplicities sake
$answers = array("16", "25", "88", etc. etc.);// again  you will get this from your table

//im assuming your answers array is numeric
foreach($answers as $key => $value){
     if (in_array($key, $wrong)){
          $class = "wrong";
     }
     else {
          $class="correct";//or whatever your right answer class is
     }

     echo "<td class=\"$class\">$value</a>";//or whatever you want to output.
}

 

this is a basic example. obviously yours will be different, but the basic logic is there

 

the mysql_fetch_array and mysql_fetch_assoc functions can get the data from your mySQL resource and turn it into an array

 

fetch array: http://us.php.net/manual/en/function.mysql-fetch-array.php

fetch assoc: http://us2.php.net/mysql_fetch_assoc

 

 

  • 2 weeks later...

Hi,

 

I am still trying to figure this out. I can pull out the MySQL data into an array using the following code.

 


<?php
ini_set("display_errors","1");
ERROR_REPORTING(E_ALL);
$con = mysql_connect("localhost","***","***") or die('Could not connect: ' . mysql_error());

mysql_select_db("****") or die(mysql_error());

$result = mysql_query("SELECT incorrect_resp FROM TableName");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    printf("Incorrect response %s", $row["incorrect_resp"]);
}

mysql_free_result($result);
?>

 

but then how do I get the results of the array to print out as such?

 

$wrong = array(1, 4, 3, 10);//You will get this from your table, but for simplicities sake
$answers = array("16", "25", "88", etc. etc.);// again  you will get this from your table

//im assuming your answers array is numeric
foreach($answers as $key => $value){
     if (in_array($key, $wrong)){
          $class = "wrong";
     }
     else {
          $class="correct";//or whatever your right answer class is
     }

     echo "<td class=\"$class\">$value</a>";//or whatever you want to output.
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.