Help with PHP guess the object game

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sunaina

    Help with PHP guess the object game

    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.
  • sunaina

    #2
    Re: Help with PHP guess the object game

    sunaina.cs@gmai l.com (sunaina) wrote in message news:<3af468c8. 0407231102.44eb bd0b@posting.go ogle.com>...[color=blue]
    > 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.[/color]

    _______________ _______________ _______________ _______________ _______________ __

    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_question s = array();
    $asked_answers = array();
    $num_questions = 0;

    if (isset($_POST["prev_quest ion"])) {
    $asked_question s[$i] = $_POST["prev_quest ion"];
    $asked_answers[$i] = $_POST["prev_answe r"];
    $num_questions = $i++;
    echo "Asked Ques Array = $num_questions< br>";
    }

    //finds possible questions still left that are not in asked questions
    array
    $possible_quest ions = array();
    $questions_sum = array();
    $i = 0;
    while ($row = mysql_fetch_ass oc($result)) {
    extract($row);
    $possible_quest ions[$i] = $qid;
    $questions_sum[$i] = $sum;
    print_r($qid . " " . $sum . "<br>");
    $i++;
    }
    $num_possible_q uestions = $i;
    $ideal_sum = $num_objs / 2;
    $question_to_as k_index = 0;
    for ($i=0; $i<$num_possibl e_questions; $i++) {
    $question_to_as k_index = $i;
    if ($questions_sum[$i] < $ideal_sum)
    break;
    }
    $question_to_as k = $possible_quest ions[$question_to_as k_index];

    // do query in database to find the question to be asked
    $question_query = "select question from question
    where(id=$quest ion_to_ask)";
    $current_questi on=mysql_query( $question_query , $db);
    while ($row = mysql_fetch_ass oc ($current_quest ion)){
    extract ($row);
    echo "<br>QUESTI ON = $question<br><b r>";
    unset($possible _questions[$question_to_as k]);
    }

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

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

    } else if(isset($_POST['NO'])) {
    $prev_answer=0;
    echo "Q=$prev_questi on";
    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_ques tion" value="<?php echo
    $prev_question; ?>">
    <input type="hidden" name="prev_answ er" value="<?php echo
    $prev_answer;?> ">
    <input type="hidden" name="question_ to_ask" value="<?php echo
    $question_to_as k;?>">
    <input type="hidden" name="$num_aske d_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.

    Comment

    Working...