rghollenbeck Posted October 31, 2010 Share Posted October 31, 2010 I'm trying to figure out how to do something. I have two tables for use in a quiz program--one table for questions and one table for answers. I'm thinking about looping through all the questions, one at a time, and all the answers that correspond to the given question, echoing the form: the radio buttons and the submit button, and have the program pause to wait for the submit button to be clicked before looping to the next question/answer set. I'm assuming I'll need to make two queries, one for the Questions table, and one for the Answers table, incrementing the QuestionID with each loop (or randomizing them and marking them as asked so the same question doesn't come up multiple times), processing the answers before the next loop, etc. 1. Is this possible? 2. Is this a good approach? 3. Where would I look to learn how to do all this? I don't have any code to show because I haven't yet written anything about this. I'm still thinking about how I would do it. I have to learn about randomizing, and about nested queries, and about a few other things. I'm just tossing this "out there" for some ideas. Thanks to all. Rich Hollenbeck Quote Link to comment https://forums.phpfreaks.com/topic/217339-pausing-a-loop-awaiting-some-user-input/ Share on other sites More sharing options...
trq Posted October 31, 2010 Share Posted October 31, 2010 Considering that http is stateless and that every time a user hits submit button they make a new request which is completely unrelated to the last, your theory wouldn't work. Quote Link to comment https://forums.phpfreaks.com/topic/217339-pausing-a-loop-awaiting-some-user-input/#findComment-1128576 Share on other sites More sharing options...
MPeter Posted October 31, 2010 Share Posted October 31, 2010 Hello, Rich Hollenbeck. Well, you like to write that in PHP. But I do see more Client-Side work in Javascript for this. In my understanding, at all, it is normal FORM where all things seem to be hidden - running Javascript - they get revealed on click. Later on the analysis runs server-sided as in the language you have present. First you have your questions and answers on the website written out alike in <div style="visibility:hidden" id="questionone"> etc. pp. - so that they cannot be seen by the user. On-Click via Javascript they get revealed! For example like this: function numberone () { if (document.getElementById) document.getElementById("questionone").style.visibility = "visible"; } So you have normal form where you can "play" in JavaScript. There are a lot more things you can add in here now. I hope I could give out my two best cents! Regards, MPeter Quote Link to comment https://forums.phpfreaks.com/topic/217339-pausing-a-loop-awaiting-some-user-input/#findComment-1128578 Share on other sites More sharing options...
rghollenbeck Posted October 31, 2010 Author Share Posted October 31, 2010 I really wanted a chance to learn PHP, and I have seen other quiz programs written in PHP. Javascript will be easier for me because I am more familiar with it. But I have close to a thousand questions and between three and six options to every question. It just seems to me that this would be a good one for SQL. Nevertheless, you may be right. I have the whole thing working as a stand-alone program written in Visual Basic 6.0 compiled into an Windows executable file. I built a "question" object. I don't know what to call it anymore. I got acquainted with so many languages that they're all blurring together. One language talks about a "type," and another language talks about a "class." Anyway, every question was instantiated with the word "new." I learned C, Java, VB, VBA, a little perl, and so many others in small doses. I even learned a little Javascript. But they are all a jumbled mess now. I have now become a multilingual idiot, like the guy who speaks a dozen languages but none of them correctly. I would like to make this quiz program web friendly. I thought the MySQL/PHP combo might be the solution. I will consider some different project to develop my PHP/MySQL education, but I'll do this one in Javascript. Thanks to both of you. Quote Link to comment https://forums.phpfreaks.com/topic/217339-pausing-a-loop-awaiting-some-user-input/#findComment-1128662 Share on other sites More sharing options...
PFMaBiSmAd Posted October 31, 2010 Share Posted October 31, 2010 Yes you can do this using php/mysql. In fact, if you do it all in javascript, the answers will be available in the source code and it would not be much of a quiz because everyone can find the answers simply by looking. Browsers (clients) and web servers don't interact the same way that an application runs on a local computer. They operate on a request/response basis. The client makes a HTTP request. The web server does its best to find what was requested and send out a complete response to that request. Then the web server goes onto service other HTTP requests. As thorpe stated, this process is stateless. The web server does not know or care about any request that occurred before the current one or what will occur after the current one. If one of your pages is a form, the web server is not sitting there specially waiting for that form to be submitted after it has been requested. It is either servicing other HTTP requests or in an idle loop. So as thrope stated, your first post in this thread is not how browsers/web servers interact. There is no waiting involved. A script does not loop through a set of data, outputting a question and waiting for an action in the browser before continuing with the next iteration of the loop. To make this stateless process have the ability to 'remember' where each visitor is at in a process like a quiz, you would use session variables to remember the current state on the server. Quote Link to comment https://forums.phpfreaks.com/topic/217339-pausing-a-loop-awaiting-some-user-input/#findComment-1128668 Share on other sites More sharing options...
DavidAM Posted October 31, 2010 Share Posted October 31, 2010 This might actually be a good project for PHP, but it will involve more than just questions and answers. You'll have to learn how to use sessions as well. Basically, when the script starts, if $_POST is empty, initialize the list of questions and ask the first one. If it is not empty, determine the next question and ask it. This is NOT working code! It is a basic flow (psuedo code) of how the process might work. session_start(); if (! isset($_POST['submit'])) { // First visit - initialize $_SESSIONS['questions'] = range(1, 10); // Range of question numbers shuffle($_SESSIONS['questions']); // Randomize the list $_SESSION['nextIndex'] = 0; // index of next question to ask $message = ''; } else { // user submitted an answer $thisIndex = $_SESSION['nextIndex']; // The index of the question we asked before $thisQuestion = $_SESSION['questions'][$thisIndex ]; // Question # we asked before // Get the user's answer $userAnswer = $_POST['Answer']; $dbAnswer = // SELECT ... WHERE ID = $thisQuestion // SELECT the correct answer from the database $message = ($userAnswer == $dbAnswer ? "Correct" : "Wrong"); $_SESSION['nextIndex']++; // index of next question to ask } // Select the next question $_SESSION['nextIndex'] from the database $thisIndex = $_SESSION['nextIndex']; $thisQuestion = $_SESSION['questions'][$thisIndex]; $dbQuestion = // SELECT ... WHERE ID = $thisQuestion; // Build the form to show to the user This will be a little confusing because the range() function generates an array of question numbers, in the example above, the array will contain the values 1 through 10; but the index (keys) of the array will be 0 through 9. You just have to be careful to NOT use the index as the question number or use a question number as an index. If you use meaningful names for the variables, it will help prevent that problem. Quote Link to comment https://forums.phpfreaks.com/topic/217339-pausing-a-loop-awaiting-some-user-input/#findComment-1128673 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.