Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 10 of 10
  1. #1
    Senior Member ericbusch's Avatar
    Join Date
    Aug 2003
    Location
    Daejeon, South Korea
    Posts
    241
    Member #
    2779
    Trouble with a while and loop

    I am offering 10 Turtle Bucks to anyone who can lead me to the answer to my question.

    i am building a multiple choice quiz. The questions and answers are all stored in a table in MySQL.

    Before I say the problem let me show you the 2 pages I have so far. this first page is the page that actually presents the quiz to the user. each answer has a value("a", "b", etc..)

    PHP Code:
    <form action="/gradeQuiz2.php" method="post">

    <?php
    while($row mysql_fetch_assoc($result))
        {
        
    ?>
        
        <table width="95%" border=1 cellpadding=5 cellspacing=0 align=center>
            <tr>
                <td width="200px" rowspan=5>&nbsp;</td>
                <td><?php echo $row["questionNumber"]; ?><?php echo $row["question"]; ?></td>
            </tr>
            <tr>
                <td><input type="radio" name="quiz[<?php echo $row["questionNumber"]; ?>]" value="a">
                <?php echo $row["answerA"]; ?></td>
            </tr>
            <tr>
                <td><input type="radio" name="quiz[<?php echo $row["questionNumber"]; ?>]" value="b">
                <?php echo $row["answerB"]; ?></td>
            </tr>
            <tr>
                <td><input type="radio" name="quiz[<?php echo $row["questionNumber"]; ?>]" value="c">
                <?php echo $row["answerC"]; ?></td>
            </tr>
            <tr>
                <td><input type="radio" name="quiz[<?php echo $row["questionNumber"]; ?>]" value="d">
                <?php echo $row["answerD"]; ?></td>
            </tr>
            
        </table>
        <br>
    <?php
    }
    ?>
    <input type="submit" name="Submit" value="I'm finished">
    </form>
    Then these variables are sent to the gradeQuiz2.php page which looks like this:
    PHP Code:
    mysql_connect($serverurl$username$password);

    mysql_select_db($database);

    $query "SELECT * 

              FROM 
    $table_name
                          
              ORDER BY questionNumber"
    ;
              
    $result mysql_query($query);


    while(
    $row mysql_fetch_array($result))
            
    {


        
    $correct[$questionNumber] = $row["correctAnswer"];
                        
        
    $correct $correct[$questionNumber];
        
        echo 
    $correct " is correct.<br>";    
            
        
                    
    }
    //end while


    for($questionNumber 1$questionNumber <= 4$questionNumber++) 

        {

            echo 
    $quiz[$questionNumber];

        }
    // end for

    ?> 
    The problem is I am unable to loop through the user's answer and the correctAnswer at the same time so i can match these up and see if the user was correct or incorrect.

    Heres an example of what i am getting http://www.ericbusch.net/quizHome.html. It prints the correct answer first and then prints the user answer second but I cant figure out how to use an "if" statement where it pulls from both of these variables.

    Any ideas?

    Thanks for your help!
    Eric
    Blogs For Sale - Each blog is packed with 1000s of signatures and ads are highly integrated into the site. Great CTR! Completely customizable from Admin CP. On Sale!
    Complete Websites For Sale - Fantastic looking sites rich with content. Come complete with domain names. I only have a couple of these.

  2.  

  3. #2
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Hi Eric.

    You will have to make a while (or a "for") loop that contains an if/else condition. You compare your POST values with the equivalent question number from your answers table. If the two values match, echo "correct answer" else "wrong".

    I'll see if I can make a suggestion for you if I can find the time today.
    (could you post your table-column-names for the table containing the quiz answers)

    There are some oddities in your code.
    $correct[$questionNumber] = $row["correctAnswer"];

    $correct = $correct[$questionNumber];
    I don't think $correct[$questionNumber] is valid array syntax.
    Also, if it was an attempt to compare the two values, remember to use == and not =, else you're just setting value A is equal to B.
    Anyway, thats what you're doing in the following line.
    The way it's written now, you could just as well write
    Code:
    $correct=$row["correctAnswer"];
    S. Rosland

  4. #3
    Senior Member ericbusch's Avatar
    Join Date
    Aug 2003
    Location
    Daejeon, South Korea
    Posts
    241
    Member #
    2779
    Thanks Rosland,

    Ok, I will change the funky syntax. I had that in there when I was trying something esle but it works with or without it.

    i have tried to stick the "if" statement in a "for" loop but i couldnt get that to work either.

    one thing i was thinking of doing was passing the correctAnswer variable from the takeQuiz.php page but i want the value hidden to the user and i couldn't do that with just HTML..

    anyway, here are my column headers for my table:

    id
    level (book number)
    unit (chapter number)
    questionNumber (ex. "1" "2" the order i want the questions to appear)
    question (this is the question)
    answerA (option number 1)
    answerB (option number 2)
    answerC (option number 3)
    answerD (option number 4)
    correctAnswer (this is the correct answer ex. "c" or "a")
    picture (this will be the path to the image for the question)

    i hope this helps!

    Thanks,
    Eric
    Blogs For Sale - Each blog is packed with 1000s of signatures and ads are highly integrated into the site. Great CTR! Completely customizable from Admin CP. On Sale!
    Complete Websites For Sale - Fantastic looking sites rich with content. Come complete with domain names. I only have a couple of these.

  5. #4
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Hi again Eric!

    While you where posting your original tables, I was creating a small script to try to illustrate a comparison loop.

    Not knowing your table structure, I made a very simple one myself.
    The one I made for the example to work, contains three columns:
    ID, question_number, correct_answer. (you could of course name them anything for the following to work.)

    I inserted four rows where the answer column contain a letter value (like one of four possible answers "a, b, c ,d")

    In my code example I extract all the rows from my answer table, and compare them value for value with the posted values from the quiz.
    I didn't have the energy to construct a new quiz page posting values to an array, so I just made a shortcut and created a quiz array in the beginning of the script.
    PHP Code:
    <?php
    $quiz
    =array("a","b","d","d");
    $conn=mysql_connect("$host""$user""$password")
             or die(
    "Could not connect");
    $rs mysql_select_db("test"$conn
            or die(
    "Could not select database");

    $sql="select * from quiz";

    $rs=mysql_query($sql$conn
            or die(
    "Could not execute query");

    $num=mysql_num_rows($rs);

    for (
    $i=0$i $num$i++)
     {
      
    $row=mysql_fetch_array($rs);

       if(
    $quiz[$i]==$row[2])
       {
        echo 
    "Question $row[1], answer $row[2] is correct<br>";
       }
       else
       {
        echo 
    "Question $row[1], answer $quiz[$i] is wrong<br>";
       }
     }
    ?>
    The preceeding code will give an output like this:
    Code:
    Question 1, answer a is correct
    Question 2, answer b is wrong
    Question 3, answer d is wrong
    Question 4, answer d is correct
    PS.
    Did you notice who have birthday today?
    S. Rosland

  6. #5
    Senior Member ericbusch's Avatar
    Join Date
    Aug 2003
    Location
    Daejeon, South Korea
    Posts
    241
    Member #
    2779
    WOOHOO, I LOVE WHEN THINGS WORK!

    Thanks Rosland,

    With a little messing around with the code I was able to get it to work. Thanks a lot for your help.

    You are now $10 richer!

    Eric
    Blogs For Sale - Each blog is packed with 1000s of signatures and ads are highly integrated into the site. Great CTR! Completely customizable from Admin CP. On Sale!
    Complete Websites For Sale - Fantastic looking sites rich with content. Come complete with domain names. I only have a couple of these.

  7. #6
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Thank you!

    I'll spend it wisely.
    I made a little alteration to the previous code. I unintentionally posted it with a small bug.

    Glad to be of help.
    S. Rosland

  8. #7
    Senior Member ericbusch's Avatar
    Join Date
    Aug 2003
    Location
    Daejeon, South Korea
    Posts
    241
    Member #
    2779
    BTW,

    I am passing variables through a couple of pages. for example i need to pass the level, unit, quiz[] variables on to a "validation" page and then pass them on to a "view" via a header("location: ") function.

    My question is how can i keep passing those variables through multiple pages and still be able to call them when i choose?

    Thanks
    eric
    Blogs For Sale - Each blog is packed with 1000s of signatures and ads are highly integrated into the site. Great CTR! Completely customizable from Admin CP. On Sale!
    Complete Websites For Sale - Fantastic looking sites rich with content. Come complete with domain names. I only have a couple of these.

  9. #8
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    If you need variables available through multiple pages, I would use sessions.
    After starting a session on the initial page, you can register session variables (session_register()). These can hold any type of variable or array.

    On the top of each of the following pages, you just add session_start(). If a session is already started, it will use that session and automatically make the registered session variables available.
    The variables will also pass between pages in the POST mode, meaning they won't be visible in the URL.

    There might be other ways of making them available, but this is a fairly easy and safe way to do it.
    S. Rosland

  10. #9
    Senior Member ericbusch's Avatar
    Join Date
    Aug 2003
    Location
    Daejeon, South Korea
    Posts
    241
    Member #
    2779
    Thanks for turning me on to SESSIONS. they are so easy. i was always intimidated to learn them but it just made life soooo much easier.

    any downsides or traps to keep in mind?

    eric
    Blogs For Sale - Each blog is packed with 1000s of signatures and ads are highly integrated into the site. Great CTR! Completely customizable from Admin CP. On Sale!
    Complete Websites For Sale - Fantastic looking sites rich with content. Come complete with domain names. I only have a couple of these.

  11. #10
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Not that I'm aware of.

    Session is a nice option to keep track of authorized users as well. If you want to hide protected pages from prying (non authorized) eyes, you could start a session if a user is authorized, and hide a session variable that must be set before users can view protected pages. You start each of the protected pages with your normal session_start() followed by an if clause that checks if the variable is set, and is valid. If not, you can use a header-location-statement to relocate them to your login page.

    Cookies do the same job, and allows you to store information on the users computer. (to recognize him/her when they return etc). The downside is that they won't work if users have cookie reception turned off. Sessions work regardlessly, as all varibales are stored on the server.
    S. Rosland


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:44 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com