Help with PHP guess the object game

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
This is the first program I am writing using PHP and Mysql. I am
creating a game where user thinks of an object and my program
guesses the object while asking series of yes/no questions. All a
user has to do is say yes or no to the questions that my program
asks. I have coded until the part where once user thinks of an
object and enters submit and the program asks him the first
question. Now when the user enters yes/no for this first quesiton I
want to display the same form again but with next question. I know
which question to ask but don't know two things:

1. how to make the same form appear again with different question.
In the form action I have tried calling the same form again and also
tried using PHP_SELF but both doesnot seem to work.

2. how to store the previously asked question in an array that can be
read later in the program when the next question is asked.

Re: Help with PHP guess the object game (sunaina) wrote in message
Quoted text here. Click to load it


So far, have been able to display the same form again using PHP_SELF
function but not able to read and display the next question onto the
same form. My first html form calls the second form, when user hits
submit. My code looks like this for second form:

$db = mysql_connect("localhost", "root", "mmmm") or
mysql_select_db("guess") or die(mysql_error());

// Find out all questions and answers which have
// already been asked

$asked_questions = array();
$asked_answers = array();
$num_questions = 0;

if (isset($_POST["prev_question"])) {
  $asked_questions[$i] = $_POST["prev_question"];
  $asked_answers[$i] = $_POST["prev_answer"];
  $num_questions = $i++;
  echo "Asked Ques Array = $num_questions<br>";

//finds possible questions still left that are not in asked questions
$possible_questions = array();
$questions_sum = array();
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
  $possible_questions[$i] = $qid;
  $questions_sum[$i] = $sum;
 print_r($qid . " " . $sum . "<br>");
$num_possible_questions = $i;
$ideal_sum = $num_objs / 2;
$question_to_ask_index = 0;
for ($i=0; $i<$num_possible_questions; $i++) {
  $question_to_ask_index = $i;
  if ($questions_sum[$i] < $ideal_sum)
$question_to_ask = $possible_questions[$question_to_ask_index];

// do query in database to find the question to be asked
      $question_query = "select question from question
      $current_question=mysql_query($question_query, $db);
      while ($row = mysql_fetch_assoc ($current_question)){
          extract ($row);
          echo "<br>QUESTION = $question<br><br>";
if (isset($_POST['YES']) || ($_POST['NO'])) {
         $prev_question = $question;
              $prev_answer = 1;
              echo "Q=$prev_question";
              echo "A=$prev_answer";

          } else if(isset($_POST['NO'])) {
              echo "Q=$prev_question";
              echo "A=$prev_answer";
<form method="POST" action="<?php echo $PHP_SELF; ?>">
<input type="image" alt="Theimage" src="C:\Program Files\Modem
<input type="submit" name="YES" value="YES">
<input type="submit" name="NO" value="NO">
<input type="hidden" name="prev_question" value="<?php echo
<input type="hidden" name="prev_answer" value="<?php echo
<input type="hidden" name="question_to_ask" value="<?php echo
<input type="hidden" name="$num_asked_ques" value="<?php echo

What I am trying to do here is once current question is asked, puts
current question in asked question array, deletes current question
from possible questions array, and displays next current question from
possible questions array. For some reason my program is not doing the
above mentioned steps. Serious help reqd.

Site Timeline