SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast Kisan's Avatar
    Join Date
    Jun 2003
    Location
    India
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    10 random quiz questions from a bank of 15

    I have a table for a quiz program

    Code:
    quiz(
      quizid,
      quizlevel,
      questionid,
      question,
      answer,
      PRIMARY KEY (quizid),
      KEY quizquestion (quizid,questionid)
    );
    
    quizchoices(
      choiceid,
      quizid,
      questionid,
      choice
    );
    
    "SELECT
        quiz.quizid, quiz.questionid, question, choiceid, choice, answer, quizlevel FROM 
        quiz 
    INNER JOIN 
        quizchoices 
    ON 
        quiz.quizid = quizchoices.quizid 
    ORDER BY 
        quiz.quizid, quiz.questionid, choiceid"
    I get 15 questions with four options for each question.

    Now I need to randomize 10 questions with four options for each question from a bank of 15 question.

    I am totally clueless...

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,253
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)

    10 out of 15 random

    You could put the db results into an array. shuffle() the array, and use the first 10.

  3. #3
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can do this in your database query:
    Code:
    ORDER BY RAND()
    LIMIT 10
    You may need to put the RAND() in your SELECT column list (something like SELECT ...., RAND() AS random) and then ORDER BY that column (ORDER BY random).
    PHP questions? RTFM
    MySQL questions? RTFM


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •