php - Making a 'quiz' - how do I show previous answer?

Solution:
First, define some functions to represent what you are trying to do in the application. (I'm going to stop with functions here and not talk about OO principles, to keep it simple).
Understanding this encapsulation of your application's functionality is a first step to good software design.
In your case, your view layer is quite simple:
function displayQuestion($question, $answers) { /* display html for question and possible answers */ }
In order to display the question, you need to get the question and its answers from the database. You might do this:
function getQuestion($questionNumber) { /* performs SQL queries and returns some representation of question, answers and correct answer */ }
Then you have the real "business" (game) logic itself, which is:
function checkAnswer($questionNumber, $answerGiven) { /* returns true or false */ }
You also need some functions to control the game as a whole, maybe like:
function startNewGame() { /* reset internal variables */ }
function getCurrentQuestionNumber() { /* get the question that the user is up to */ }
function displayCurrentScore() { /* display html for current score */ }
The final part is to wire it all together. (I mean final in the discussion, not the last thing you do). This is where you connect the parts together, starting from the HTTP request, through to the model and business logic, and back to the view layer to display the pages to the user via the HTTP response. You can conceivable encapsulate this whole process as a method:
function handleRequest() { /* perform the whole request-response cycle, calling the other methods as required */ }
Note that the above is just a broad and pretty generalised approach to these functions and what they do, I don't know your exact requirements so there will be parts missing. It is also not quite "best practice" but it is better than mixing your business logic and database access into your HTML code :-)
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non well formed numeric value encountered
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.