By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,434 Members | 2,884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,434 IT Pros & Developers. It's quick & easy.

Help with PHP guess the object game

P: n/a
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.
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
su********@gmail.com (sunaina) wrote in message news:<3a**************************@posting.google. com>...
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.


__________________________________________________ ___________________________

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:

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

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

$i=0;
$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
array
$possible_questions = array();
$questions_sum = array();
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
extract($row);
$possible_questions[$i] = $qid;
$questions_sum[$i] = $sum;
print_r($qid . " " . $sum . "<br>");
$i++;
}
$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)
break;
}
$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
where(id=$question_to_ask)";
$current_question=mysql_query($question_query, $db);
while ($row = mysql_fetch_assoc ($current_question)){
extract ($row);
echo "<br>QUESTION = $question<br><br>";
unset($possible_questions[$question_to_ask]);
}

if (isset($_POST['YES']) || ($_POST['NO'])) {
$prev_question = $question;

if(isset($_POST['YES'])){
$prev_answer = 1;
echo "Q=$prev_question";
echo "A=$prev_answer";

} else if(isset($_POST['NO'])) {
$prev_answer=0;
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
Helper\Pictures\wink.gif">
<input type="submit" name="YES" value="YES">
<input type="submit" name="NO" value="NO">
<input type="hidden" name="prev_question" value="<?php echo
$prev_question;?>">
<input type="hidden" name="prev_answer" value="<?php echo
$prev_answer;?>">
<input type="hidden" name="question_to_ask" value="<?php echo
$question_to_ask;?>">
<input type="hidden" name="$num_asked_ques" value="<?php echo
$num_asked_ques;?>">
</form>
?>

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.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.