/    Sign up×
Community /Pin to ProfileBookmark

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

[code=php]<?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>[/code]

I want to show 20 questions at random. I think this is the line of code that I need to do something with.

[code=php]<button onClick=”startQuiz(‘quiz.php?question=1’)”>Click Here To Begin</button>[/code]

Any help would be much appreciated

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 10.2015 — If you know how many questions there are, and they are consecutively numbered starting at 1, you could get a randomized range of numbers with something like:
[code=php]
$numbers = range(1, $totalNbrOfQuestions);
shuffle($numbers);
$numbers = array_slice($numbers, 0, 20);
[/code]

At this point you could do a foreach() on $numbers to loop through the selected question numbers, or however you want to handle things.
Copy linkTweet thisAlerts:
@stokie-ruchauthorApr 10.2015 — If you know how many questions there are, and they are consecutively numbered starting at 1, you could get a randomized range of numbers with something like:
[code=php]
$numbers = range(1, $totalNbrOfQuestions);
shuffle($numbers);
$numbers = array_slice($numbers, 0, 20);
[/code]

At this point you could do a foreach() on $numbers to loop through the selected question numbers, or however you want to handle things.[/QUOTE]


that sounds something like what I want! Yeah its just so each user doesn't have to go through all 80 questions each time they use the quiz. So how do I implement this into my code?
Copy linkTweet thisAlerts:
@NogDogApr 10.2015 — Well...now it gets down to the exact user experience you want. If you want to cycle through each question a page at a time, with some sort of "next question" link on each, then you probably need to use a session variable to store the random list, and maybe use array_pop() to pull off a question number for each link? (I'm trying not to just write the application for you, but give you some ideas and tools. If you want me to write the whole thing, we may need to discuss payment terms. ? )
Copy linkTweet thisAlerts:
@stokie-ruchauthorApr 10.2015 — Well...now it gets down to the exact user experience you want. If you want to cycle through each question a page at a time, with some sort of "next question" link on each, then you probably need to use a session variable to store the random list, and maybe use array_pop() to pull off a question number for each link? (I'm trying not to just write the application for you, but give you some ideas and tools. If you want me to write the whole thing, we may need to discuss payment terms. ? )[/QUOTE]

Just looking for hints and tips. Someone has suggested that I use this line of code

[code=php]$question_id = rand(1, 80); [/code] but the quiz is still showing all of the 80 questions
Copy linkTweet thisAlerts:
@NogDogApr 10.2015 — The problem with just doing a rand() for each link is that it has no memory of what it randomly selected before, so any selected question still has the same odds of showing up again during that user's session (thus my idea of creating a randomized array of 20 unique question numbers). In any case, if you use that rand() method, you'd need to keep track how many questions the user has answered so far, which again seems to point to using sessions, in this case something like a $_SESSION['questions_answered'] element that you could increment and test against to see when they have answered 20 questions.
×

Success!

Help @stokie-ruch spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...