webguync Posted August 14, 2009 Share Posted August 14, 2009 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 https://forums.phpfreaks.com/topic/170283-highlighted-incorrect-responses-bases-on-phpmysql-results-data/ Share on other sites More sharing options...
mikesta707 Posted August 14, 2009 Share Posted August 14, 2009 if (wrong($answer)){ $class = "wrong"; } echo "<td class=\"$class\">"; ? Link to comment https://forums.phpfreaks.com/topic/170283-highlighted-incorrect-responses-bases-on-phpmysql-results-data/#findComment-898247 Share on other sites More sharing options...
webguync Posted August 14, 2009 Author Share Posted August 14, 2009 so in replace of $answer It would be the name of my field for incorrect responses? Link to comment https://forums.phpfreaks.com/topic/170283-highlighted-incorrect-responses-bases-on-phpmysql-results-data/#findComment-898254 Share on other sites More sharing options...
mikesta707 Posted August 14, 2009 Share Posted August 14, 2009 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? Link to comment https://forums.phpfreaks.com/topic/170283-highlighted-incorrect-responses-bases-on-phpmysql-results-data/#findComment-898257 Share on other sites More sharing options...
webguync Posted August 14, 2009 Author Share Posted August 14, 2009 this is actually done in the exam application, which uses Flash and XML. the responses are then submitted into the database. Link to comment https://forums.phpfreaks.com/topic/170283-highlighted-incorrect-responses-bases-on-phpmysql-results-data/#findComment-898262 Share on other sites More sharing options...
mikesta707 Posted August 14, 2009 Share Posted August 14, 2009 ahhh ok. so in your database does it just have the answers, or does it have the answers, and which are wrong and which are right? Link to comment https://forums.phpfreaks.com/topic/170283-highlighted-incorrect-responses-bases-on-phpmysql-results-data/#findComment-898274 Share on other sites More sharing options...
webguync Posted August 14, 2009 Author Share Posted August 14, 2009 there is a field in the MySQL that displays specifically the wrong answers. Is is set to type text and is 1,5,10 etc. Link to comment https://forums.phpfreaks.com/topic/170283-highlighted-incorrect-responses-bases-on-phpmysql-results-data/#findComment-898290 Share on other sites More sharing options...
mikesta707 Posted August 14, 2009 Share Posted August 14, 2009 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 Link to comment https://forums.phpfreaks.com/topic/170283-highlighted-incorrect-responses-bases-on-phpmysql-results-data/#findComment-898297 Share on other sites More sharing options...
webguync Posted August 14, 2009 Author Share Posted August 14, 2009 would you or someone else be able to post an example code? Link to comment https://forums.phpfreaks.com/topic/170283-highlighted-incorrect-responses-bases-on-phpmysql-results-data/#findComment-898303 Share on other sites More sharing options...
mikesta707 Posted August 14, 2009 Share Posted August 14, 2009 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 https://forums.phpfreaks.com/topic/170283-highlighted-incorrect-responses-bases-on-phpmysql-results-data/#findComment-898308 Share on other sites More sharing options...
webguync Posted August 14, 2009 Author Share Posted August 14, 2009 how would I convert the data from the table into an array in PHP? Link to comment https://forums.phpfreaks.com/topic/170283-highlighted-incorrect-responses-bases-on-phpmysql-results-data/#findComment-898320 Share on other sites More sharing options...
mikesta707 Posted August 14, 2009 Share Posted August 14, 2009 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 Link to comment https://forums.phpfreaks.com/topic/170283-highlighted-incorrect-responses-bases-on-phpmysql-results-data/#findComment-898334 Share on other sites More sharing options...
webguync Posted August 14, 2009 Author Share Posted August 14, 2009 I will look into that thanks. Link to comment https://forums.phpfreaks.com/topic/170283-highlighted-incorrect-responses-bases-on-phpmysql-results-data/#findComment-898374 Share on other sites More sharing options...
webguync Posted August 28, 2009 Author Share Posted August 28, 2009 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 https://forums.phpfreaks.com/topic/170283-highlighted-incorrect-responses-bases-on-phpmysql-results-data/#findComment-908589 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.