2

I can't find what I need. I trying to create at quizzer that takes questions from the database. Currently, it takes all the questions in the database and what I need it to take it randomly 10 questions. I am just starting to learn how to code.

Here are my getting questions code with answers:

<?php
    //Set question number
    $number = (int) $_GET['n'];

    /*
    *   Get total questions
    */
    $query = "SELECT * FROM `klausimai`";
    //Get result
    $results = $mysqli->query($query) or die($mysqli->error.__LINE__);
    $total = $results->num_rows;

    /*
    *   Get Question
    */
    $query = "SELECT * FROM `klausimai`
                WHERE question_number = $number";
    //Get result
    $result = $mysqli->query($query) or die($mysqli->error.__LINE__);

    $question = $result->fetch_assoc();

    /*
    *   Get Choices
    */
    $query = "SELECT * FROM `atsakymai`
                WHERE question_number = $number";
    //Get results
    $choices = $mysqli->query($query) or die($mysqli->error.__LINE__);
?>
nyi
  • 3,123
  • 4
  • 22
  • 45

3 Answers3

1

As mentioned above, limit the klausimai query results to 10 by using the SQL LIMIT statement ($query = "SELECT * FROM klausimai LIMIT 10";):

<?php
//Set question number
$number = (int) $_GET['n'];

/*
*   Get total questions
*/
$query = "SELECT * FROM `klausimai`  LIMIT 10";
//Get result
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $results->num_rows;

/*
*   Get Question
*/
$query = "SELECT * FROM `klausimai`
            WHERE question_number = $number";
//Get result
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

$question = $result->fetch_assoc();

/*
*   Get Choices
*/
$query = "SELECT * FROM `atsakymai`
            WHERE question_number = $number";
//Get results
$choices = $mysqli->query($query) or die($mysqli->error.__LINE__);
?>
  • I did limited but it will take first 10 question. And I need to be completely random. – Lukas Šliužas May 08 '18 at 06:47
  • To get random records one needs to know the structure of the klausmai table. Check this topic: https://stackoverflow.com/questions/4329396/mysql-select-10-random-rows-from-600k-rows-fast – Alexandar Penkin May 08 '18 at 12:26
1

Your Code good to go, but to print 10 random question fetched directly from your mysql database, Execute the following query :

 $query = SELECT * FROM `klausimai`
ORDER BY RAND()
 LIMIT 10;
while(row=mysql_fetch_array($query) {

echo $row['question_feild'];
//Replace 'question_feild' it with your question_body column of your table 

//assuming you have 2 choices for a particular question 

echo $row['choice1']; // Same replace it with your choice/answer columns
echo $row['choice2']; // Same replace it with your choice/answer columns 

}
hotheadhacker
  • 182
  • 10
0

Try this :

/*
 *  Get Questions
*/
$query = "SELECT * FROM `klausimai` ORDER BY RAND() LIMIT 10";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($result)){
    echo "Question number " . $row["question_number"] . "<br/>";
    echo "Choices of question are:<br/>";
    /*
     *  Get Choices
    */
    $query = "SELECT * FROM `atsakymai` WHERE `question_number` = " . $row["question_number"];
    $result = mysqli_query($conn, $query);
    $i = 1;
    while($row2 = mysqli_fetch_assoc($result)){
        echo $i.") ".$row2["choice_text"]."<br/>";
        $i++;
    }
}