views:

53

answers:

1

Please, show me how to get my a random question, $randomQuestion (i.e., "What color is the sky?"), and matching answer, $matchingAnswer (i.e., "BLUE"), from the $quizID's table's list of questions in my SQL database. Then, please help me use jQuery to refresh the $randomQuestion and $matchingAnswer, without refreshing the whole page.

In the end, it should flow like this:

  1. "Random Question from DB: "What color is the sky?" Answer: "_""
  2. User Inputs the Matching String: "BLUE
  3. On e's keydown, the form submits because "BLUE" matches $matchingAnswer.
  4. A new random question and answer are loaded: "Which way am I pointing ^?" $matchingAnswer = 'UP'. {repeat}

HTML:

<form name="$quizID" action="http://asite.com" method="post">
  <fieldset>
    <legend><?php echo $randomQuestion?></legend>
    <p><label>
      Answer: <input type="text" id="answer"
                     onkeydown="submitAns(submit.id)" />
    </label></p>
  </fieldset>
</form>

$.post('get-question_matchinganswer_for_quizID.php', {
  quizID: $quizID,
  question },
  function(data) {
    alert('Question is: ' + data.question)
    alert('Answer is: ' + data.answer)
  },
  'json'
);

Also, need help to prepare the PHP file:

function random_row($table, $column) {
  $max_sql = "SELECT max(" . $column . ") 
          AS max_id
          FROM " . $table;
  $max_row = mysql_fetch_array(mysql_query($max_sql));
  $random_number = mt_rand(1, $max_row['max_id']);
  $random_sql = "SELECT * FROM " . $table . "
          WHERE " . $column . " >= " . $random_number . " 
          ORDER BY " . $column . " ASC
          LIMIT 1";
  $random_row = mysql_fetch_row(mysql_query($random_sql));
  if (!is_array($random_row)) {
    $random_sql = "SELECT * FROM " . $table . "
            WHERE " . $column . " < " . $random_number . " 
            ORDER BY " . $column . " DESC
            LIMIT 1";

    $random_row = mysql_fetch_row(mysql_query($random_sql));
  }
  return $random_row;
}

$randomQuestion =
$matchinAnswer =