charliepoo Posted July 16, 2009 Share Posted July 16, 2009 Okay, so I'm trying to do a random question and answer php coding using a database to store each question, answer and ID. So far I've managed to get the random question, using this code: <?php //select database for use mysql_select_db ('carlott_ravie') or die ('Unable to select database!'); //create and exectue query $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `questions` "); $offset_row = mysql_fetch_object( $offset_result ); $offset = $offset_row->offset; $result = mysql_query( " SELECT * FROM `questions` LIMIT $offset, 1 " ); //check if records were returned if (mysql_num_rows($result) > 0) { //print HTML //iterate over record set // print each field while($row = mysql_fetch_row($result)) { echo '' . $row[1] . ''; echo '</div></center>'; } } else { //print error message echo 'No events in planning.'; } //once processing is complete // free result set mysql_free_result($result); // close connection to MySQL server mysql_close($connection) ?> What I'm wanting to do further than that, is have the user enter the answer and check it against the stored answer in the database. If it's right the user gets a button or redirected to another page, if it's wrong, the user gets redirected to a different page telling them it's wrong. row[2] is where the answers are in the database, so I'm figuring since it's random, I'd have to load the question into a variable in the same php session, but my attempts have been unsuccessful. I have no idea how to check the user's answer is the same as the answer stored. Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/166205-solved-can-this-be-done/ Share on other sites More sharing options...
ignace Posted July 16, 2009 Share Posted July 16, 2009 Your doing double work doing: SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `questions` While it should be: SELECT * FROM questions ORDER BY rand() Please post your db scheme tableName (columnName, ..) .. Quote Link to comment https://forums.phpfreaks.com/topic/166205-solved-can-this-be-done/#findComment-876429 Share on other sites More sharing options...
charliepoo Posted July 16, 2009 Author Share Posted July 16, 2009 I tried the latter, but it showed up all of the input, while I only want to show up one on each refresh. questions (Qid, Question, Answer) Quote Link to comment https://forums.phpfreaks.com/topic/166205-solved-can-this-be-done/#findComment-876463 Share on other sites More sharing options...
akitchin Posted July 16, 2009 Share Posted July 16, 2009 first, you can use ignace's query if you add "LIMIT 1" to the end of it to restrict the set to one. second, in order to check the user's input against the answer, simply put a hidden input into the form containing the question's ID. then on submission, use that ID to summon the answer from the database and check it against the answer the user provided. from there, i presume you're able to do the rest (forwarding the user or showing a button, etc). Quote Link to comment https://forums.phpfreaks.com/topic/166205-solved-can-this-be-done/#findComment-876618 Share on other sites More sharing options...
charliepoo Posted July 18, 2009 Author Share Posted July 18, 2009 Cheers, that helped alot (: Sorry, I'm really new to this. I've managed to do what you said, to put in the hidden input and to write the query needed, but now I'm stuck on how to process it so that I can check that the answer is the same as the one in the database. I am also not sure if my calling of the variable $qid is correct. <?php $answer = $_POST['answer'] $qid = $_POST['qid']; //select database for use mysql_select_db ('carlott_ravie') or die ('Unable to select database!'); //create and execute query $offset_result = mysql_query( " SELECT `questions`.`Qid`, `questions`.`Answer` FROM questions WHERE (`questions`.`Qid` $qid) " ) // is the last part correct? //check if records were returned if (mysql_num_rows($result) > 0) { this is the part I am having trouble with } //once processing is complete // free result set mysql_free_result($result); // close connection to MySQL server mysql_close($connection) ?> Quote Link to comment https://forums.phpfreaks.com/topic/166205-solved-can-this-be-done/#findComment-877772 Share on other sites More sharing options...
charliepoo Posted July 19, 2009 Author Share Posted July 19, 2009 Any help here? Quote Link to comment https://forums.phpfreaks.com/topic/166205-solved-can-this-be-done/#findComment-878073 Share on other sites More sharing options...
Batosi Posted July 19, 2009 Share Posted July 19, 2009 <?php //create and execute query $offset_result = mysql_query( " SELECT `questions`.`Qid`, `questions`.`Answer` FROM questions WHERE (`questions`.`Qid` $qid) " ) // is the last part correct? //check if records were returned if (mysql_num_rows($offset_result) > 0) { You made a typo on the variable. These mistakes are a pain in the ass } ?> Quote Link to comment https://forums.phpfreaks.com/topic/166205-solved-can-this-be-done/#findComment-878279 Share on other sites More sharing options...
akitchin Posted July 20, 2009 Share Posted July 20, 2009 Cheers, that helped alot (: Sorry, I'm really new to this. I've managed to do what you said, to put in the hidden input and to write the query needed, but now I'm stuck on how to process it so that I can check that the answer is the same as the one in the database. I am also not sure if my calling of the variable $qid is correct. <?php $answer = $_POST['answer'] $qid = $_POST['qid']; //select database for use mysql_select_db ('carlott_ravie') or die ('Unable to select database!'); //create and execute query $offset_result = mysql_query( " SELECT `questions`.`Qid`, `questions`.`Answer` FROM questions WHERE (`questions`.`Qid` $qid) " ) // is the last part correct? //check if records were returned if (mysql_num_rows($result) > 0) { this is the part I am having trouble with } //once processing is complete // free result set mysql_free_result($result); // close connection to MySQL server mysql_close($connection) ?> first off, you've got an error in your query. you need to add an operator to that WHERE clause: SELECT `Answer` FROM `questions` WHERE `Qid` = '$qid') i also removed some unnecessary stuff (didn't need to select Qid, and you don't need the table notation when selecting from only one table). second, you're on the right track for checking the answer against the one submitted. within that if() statement (which batosi has corrected for you), you will simply need to check if the two match: if (num_rows thing) { $row = mysql_fetch_assoc($offset_result); if ($_POST['answer'] == $row['Answer']) { // answer is correct } else { // answer is incorrect } } keep in mind you're at the mercy of the machine here - if they aren't identical in spelling, case, and whitespace, the computer won't technically consider them matched. Quote Link to comment https://forums.phpfreaks.com/topic/166205-solved-can-this-be-done/#findComment-878921 Share on other sites More sharing options...
charliepoo Posted July 22, 2009 Author Share Posted July 22, 2009 Thankyou so much (: Quote Link to comment https://forums.phpfreaks.com/topic/166205-solved-can-this-be-done/#findComment-880330 Share on other sites More sharing options...
Batosi Posted July 22, 2009 Share Posted July 22, 2009 Eh that query confused me I hate writing it out with the table names >.< Quote Link to comment https://forums.phpfreaks.com/topic/166205-solved-can-this-be-done/#findComment-880402 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.