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    

$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.

<button onClick="startQuiz('quiz.php?question=1')">Click Here To Begin</button>  

Any help would be much appreciated

Dani AI

Generated

Short answer: pick the 20 questions once when the quiz is started, save that ordered list in the session, and then serve questions by index from that list. That prevents a new random row on every page load (which is probably why the order keeps changing) and lets the quiz behave like a single run of 20 fixed questions.

Example (PHP + PDO): create the randomized order at quiz start and store it in $_SESSION, then fetch questions by id from that list.

<?php
session_start();

// on start (link / form should call ?start=1 or POST to begin)
if (isset($_GET['start']) || !isset($_SESSION['quiz_ids'])) {
    $pdo = new PDO('mysql:host=HOST;dbname=DB','USER','PASS',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);

    // simple: fine for 80 rows
    $ids = $pdo->query('SELECT id FROM questions ORDER BY RAND() LIMIT 20')->fetchAll(PDO::FETCH_COLUMN);

    // alternative for much larger tables: fetch all ids, shuffle(), array_slice()
    $_SESSION['quiz_ids'] = $ids;
    $_SESSION['quiz_pos'] = 0;
}

// determine safe position (map GET index to session list; don't trust arbitrary IDs)
$pos = isset($_GET['q']) ? max(0, (int)$_GET['q'] - 1) : $_SESSION['quiz_pos'];
$pos = min($pos, count($_SESSION['quiz_ids']) - 1);
$id = $_SESSION['quiz_ids'][$pos];

// fetch the actual question row
$st = $pdo->prepare('SELECT * FROM questions WHERE id = ?');
$st->execute([$id]);
$question = $st->fetch(PDO::FETCH_ASSOC);

Notes and troubleshooting:

  • This solves the “different order each page” problem by randomizing once. ’s current start link quiz.php?question=1 should instead trigger the quiz initialization (e.g. quiz.php?start=1), or be a simple anchor styled as a button as suggested.
  • Use prepared statements and htmlspecialchars() when outputting text to avoid SQL injection/XSS.
  • ORDER BY RAND() is OK for 80 rows; for large tables use the fetch-all-IDs + shuffle() approach or a more scalable sampling strategy.
  • Keep answers/progress in session or write them to a DB row keyed to the session/user so refreshes and navigation behave correctly.

Recommended Answers

All 4 Replies

Member Avatar for Member #120589

This looks a bit screwy. So the quiz depends on sending a question number to a php page. Why are you using javascript at all? If this is a workaround to use a button as a link, you're far better styling a link to look like a button.

hey i got the code from this webiste So there will be things that I will need to change to get it to work how I want it to work

Member Avatar for Member #120589

Ok if you.re using a third party script have you asked over on that site?

yes servral times and the person who created the code cannot be contacted. There are a number of people who are wanting to ask the person questions and they cannot get any responses

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.