Jump to content

How would I create this app (fetching questions & answers from MySQL)


KyZu

Recommended Posts

I've just learned the basics of MySQL last night and made a simple app (where a user enters a song name, song title, and rating (out of 5) and it displays the list of songs with the highest rated songs first).

 

So I decided I wanted to make something similar yet slightly more complicated and I'm not sure if I'm on the right track. Basically it's a "flash card" game where:

 

- The user gets to input their own questions and answers into a simple form, which would then go into an MySQL table with 2 columns, questions and answers

- A new page where the user see's random question, must input an answer, if the answer matches goes to next question otherwise gives error

 

Now I'm confused as to how to go about making this. I was thinking of making some session variables and then do something along the lines of

 

	 
$display_query = "SELECT * FROM questions";
$display = mysqli_query($connection, $display_query);
      
      while ($row = mysqli_fetch_array($display)) {
	$_SESSION['question'] = $row[0];
      }[/syntax]

//and then make some PHP that might do something like this:


            if ($_SESSION['answer'] != $row[1]) {
           echo "error, try again"
} else {
           // I would write something here to grab the next question
}

 

 

So am I just completely off the wall with this, how would I make such an app? How would I make it, for example, fetch new questions?

 

Link to comment
Share on other sites

You can use a session variable as an array to keep track of the replied questions and use that array to find a random question that has not been replied yet. The following code is not tested, but should give a general idea.

 

<?php
session_start();
//processing the form
if (isset($_POST['submit'])) {
     $id = (int) $_POST['id'];
     $answer = mysql_real_escape_string($_POST['answer']);
     $results = mysql_query("SELECT answer FROM questions WHERE id=$id");
     
     if (mysql_num_rows($results)) {
          $values = mysql_fetch_assoc($results);
          if ($values['answer'] == $answer) {
               //add the question ID to the session array
               $replied = $_SESSION['replied'];
               $replied[] = $id;
               $_SESSION['replied'] = $replied;
               
               echo 'Correct answer. Moved to the next question';
          } else {
               echo 'Incorrect answer. Please try again.';
          }
     }
}

$replied = $_SESSION['replied'];
$query_modifier = '';

//if any question has been replied
if (count($replied)) {
     $query_modifier = "WHERE id NOT IN (" . implode(', ', $replied) . ")";
}

//you could even get a random question, but that won't be much needed. It will always show 1 question
//that has not been answered yet.
$results = mysql_query("SELECT id, question, answer FROM questions $query_modifier LIMIT 1");
$values = mysql_fetch_assoc($results);
?>

<form method="post" action="">
     <h1><?php echo $values['question']; ?></h1>
     <input type="hidden" name="id" value="<?php echo $values['id']; ?>" />
     <input type="text" name="answer" />
     <button type="submit" name="submit">Reply</button>
</form>

 

As you can see, the logic is quite simple. I used a session variable as array to hold the question IDs that are already successfully replied and made the query such that it doesn't show any of the replied questions. The same session is used when submitting a reply; it is added the question ID when the answer is correct.

 

Try it and tell me if you have any problems.

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.