www.webdeveloper.com
Results 1 to 6 of 6

Thread: Changing A Select Statement

  1. #1
    Join Date
    Apr 2014
    Posts
    33

    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.


    Code:
    $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

    Code:
    $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

    Code:
    $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
    Posts
    33
    any ideas folks? 188 views and no replays?

  3. #3
    Join Date
    May 2014
    Posts
    49
    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
    Posts
    33
    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
    Location
    Ankh-Morpork
    Posts
    19,176
    If you want 20 random rows:
    Code:
    SELECT ... ORDER BY RAND() LIMIT 20
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Apr 2014
    Posts
    33
    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



Recent Articles