Results 1 to 6 of 6

Thread: Changing A Select Statement

Hybrid View

  1. #1
    Join Date
    Apr 2014

    Changing A Select Statement

    Hi guys.

    I wanted to create a quiz for my website. I have found some code from a website called (getcode.org) (prev world of webcraft).

    The quiz now works as it should and I'm very happy it does. There is one problem I have adapited it to the needs of my website. The website is an learning aid for people like myself who suffer from Dyscaculia and its foucing on Fractions. I have added in 80 question into the database and at the moment the PHP/SQL code is selecting all of the question from the database.

    I only want each quiz to contain 20 questions per quiz.

    $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;
    Is there anyway, how I can just state in the SQL code to only select 20 questions from the "questions" table and to have them in a random order so each quiz will contain 20 different questions each time a user takes the quiz?

    Here is the specific line of code that is the concern

    This is taking the questions from the questions table

    $singleSQL = mysql_query("SELECT * FROM questions WHERE id='$question' LIMIT 1");
    And this is the code that is taking the answers from the answers table and putting the answers in a ramdom order each time the questions are displayed

    $sql2 = mysql_query("SELECT * FROM answers WHERE question_id='$question' ORDER BY rand()");
    Any help would be much appreciated

  2. #2
    Join Date
    Apr 2014
    any ideas folks? 188 views and no replays?

  3. #3
    Join Date
    May 2014
    PHP Code:
    $singleSQL mysql_query("SELECT * FROM questions WHERE id='$question' ORDER BY rand() LIMIT 20;"); 
    Is something like this what you're looking for?

    Please not that you shouldn't use mysql_ functions, they're outdated, insecure, and soon to be removed from PHP and your site will likely stop working.

    You should use mysqli_ or PDO instead.
    Also remember to clean variables passed by the client before inserting them into an SQL string to prevent your site being hacked.

  4. #4
    Join Date
    Apr 2014
    hi thanks for this but its still not showing the questions in a random way at all.... its still going from question 1 to question 80 still

  5. #5
    Join Date
    Aug 2004
    If you want 20 random rows:
    There are some more efficient (and more complicated) ways to do this, but for the scale we're talking about here (~100 questions to randomly sort), it's probably not worth worrying about and prematurely optimizing it.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  6. #6
    Join Date
    Apr 2014
    ok I will have a try and see if that works

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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