Jump to content

highlighted incorrect responses bases on PHP/MySQL results data


webguync

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

  • 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.
}

Link to comment
Share on other sites

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.