#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    45
    Rep Power
    3

    how to show 20 random questions in a quiz


    Hi guys,

    Hope you're all well. I have managed to get the quiz that I have been working on to work now thank god!

    I just want to show just 20 random questions from the 80 questions that I have in the question bank.

    Looking at the quiz files I think this is where I need to make the changes
    PHP Code:
    PHP Code:
    <?php   

    $msg 
    "";  
    if(isset(
    $_GET['msg'])){  
        
    $msg $_GET['msg'];  
        
    $msg strip_tags($msg);  
        
    $msg addslashes($msg);  
    }  
    ?>  
    <!doctype html>  
    <html lang="en">  
    <head>  
    <meta charset="utf-8">  
    <title>Quiz Tut</title>  
    <script>  
    function startQuiz(url){  
        window.location = url;  
    }  
    </script>  
    </head>  
    <body>  
    <?php echo $msg?>  
    <h3>Click below when you are ready to start the quiz. This is a ramdom quiz so each time you take the quiz the questions will show in a different order each time!</h3>  
    <button onClick="startQuiz('quiz.php?question=1')">Click Here To Begin</button>  
    </body>  
    </html>
    I want to show 20 questions at random. I think this is the line of code that I need to do something with.

    PHP Code:
    PHP Code:
    <button onClick="startQuiz('quiz.php?question=1')">Click Here To Begin</button
    Any help would be much appreciated
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,010
    Rep Power
    599
    What kind of help? You don't show the code in 'quiz.php' nor do you say what you get that isn't working.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Location
    Kumasi, Ghana
    Posts
    19
    Rep Power
    0
    Can you show the codes of quiz.php, I think that's where the problem is.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    45
    Rep Power
    3
    Originally Posted by gw1500se
    What kind of help? You don't show the code in 'quiz.php' nor do you say what you get that isn't working.
    The quiz is working, it works perfect. All I want to do it show 20 random questions instead of showing all 80 questions.

    This is the question.php file code

    PHP Code:
                        
                    

    <?php 

    session_start
    ();
    require_once(
    "scripts/connect_db.php");
    $arrCount "";
    if(isset(
    $_GET['question'])){
        
    $question preg_replace('/[^0-9]/'""$_GET['question']);
        
    $output "";
        
    $answers "";
        
    $q "";
        
    $sql mysql_query("SELECT id FROM questions");
        
    $numQuestions mysql_num_rows($sql);
        if(!isset(
    $_SESSION['answer_array']) || $_SESSION['answer_array'] < 1){
            
    $currQuestion "1";
        }else{
            
    $arrCount count($_SESSION['answer_array']);
        }
        if(
    $arrCount $numQuestions){
            unset(
    $_SESSION['answer_array']);
            
    header("location: index.php");
            exit();
        }
        if(
    $arrCount >= $numQuestions){
            echo 
    'finished|<p>There are no more questions. Please enter your first and last name and click next</p>
                    <form action="userAnswers.php" method="post">
                    <input type="hidden" name="complete" value="true">
                    <input type="text" name="username">
                    <input type="submit" value="Finish">
                    </form>'
    ;
            exit();
        }
        
    $singleSQL mysql_query("SELECT * FROM questions WHERE id='$question' LIMIT 1");
            while(
    $row mysql_fetch_array($singleSQL)){
                
    $id $row['id'];
                
    $thisQuestion $row['question'];
                
    $type $row['type'];
                
    $question_id $row['question_id'];
                
    $q '<h2>'.$thisQuestion.'</h2>';
                
    $sql2 mysql_query("SELECT * FROM answers WHERE question_id='$question' ORDER BY rand()");
                while(
    $row2 mysql_fetch_array($sql2)){
                    
    $answer $row2['answer'];
                    
    $correct $row2['correct'];
                    
    $answers .= '<label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correct.'">'.$answer.'</label> 
                    <input type="hidden" id="qid" value="'
    .$id.'" name="qid"><br /><br />
                    '
    ;
                    
                }
                
    $output ''.$q.','.$answers.',<span id="btnSpan"><button onclick="post_answer()">Submit</button></span>';
                echo 
    $output;
               }
            }
        

    ?>
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,010
    Rep Power
    599
    Yikes, first you need to take down that web site immediately and rewrite the code using PDO. You are using the deprecated (for more than a decade) MySQL extensions leaving your web site wide open to injection attack. It will also break with the next release of PHP.

    As for your specific problem, since you are randomizing the order in your query, just process the first 20 rows rather than all of them. Change the while loop to a for loop with a count of 20.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Location
    Kumasi, Ghana
    Posts
    19
    Rep Power
    0
    You can use the rand(); function. so maybe you set a variable for your question id and then use maybe
    PHP Code:
    $question_id rand(180); 
    1 is the minimum id and 80 is the maximum id so it will output random questions. See ya
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    45
    Rep Power
    3
    Originally Posted by Techneut
    You can use the rand(); function. so maybe you set a variable for your question id and then use maybe
    PHP Code:
    $question_id rand(180); 
    1 is the minimum id and 80 is the maximum id so it will output random questions. See ya
    Hey thanks for the advice just put the code into the file and tried to run the quiz. Its still showing all of the questions
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,010
    Rep Power
    599
    Because you are outputting all the questions instead of processing just 20.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    45
    Rep Power
    3
    Originally Posted by gw1500se
    Because you are outputting all the questions instead of processing just 20.
    ahhh right ok. So
    PHP Code:
    $question_id rand(180); 
    would help me to just random and show 20 questions then?
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Location
    Kumasi, Ghana
    Posts
    19
    Rep Power
    0
    So you can use either a for loop or while loop to create the 20 questions criteria.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,010
    Rep Power
    599
    I already told you how to handle it.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Location
    Kumasi, Ghana
    Posts
    19
    Rep Power
    0
    Hmm, it seems like I'm stuck in my logic but why don't you try to use 20 as the maximum id instead of 80 to randomize the first 80 questions
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,010
    Rep Power
    599
    That will exclude 60 of the questions in the randomization. Just add 'LIMIT 20' to the query.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo