php - I have a quiz and I want to get random questions (no repeat)

68

As the title said, I have a huge problem with a PHP script. I want to extract(Get) random questions from my database and after that display it.I want the script to look like here: http://www.filebox.ro/download.php?key=d07a7jdcf2ouwu62 but with a MySQL database, and a admin dashboard for administrating questions and users. Here is my script: http://fbx.ro/hxsztelz2chq1ekk

Here is my index.php As you see on line 6 I don't know how to do it... If you can teach me how to extract more than 1 question, like a quiz (aprox.:20 random questions)

 <?php
include("config.php");
?>
<?php
session_start();
$sql = "select * from quiz order by rand() LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_object($result);
$question = "$row->questions";
$correctanswer = "$row->answers";
$money = '1';
$today = date('Y-m-d');
if (isset($_POST['submitted'])) {
if (empty($_POST['answer']) || empty($_POST['id'])) {
echo '<p><span class="page_speed_865619382">Trebuie sa introduci un raspuns!</span></p>';
} else {

$answer = $_POST['answer'];
$sql_answer = "select * from quiz where id = '".addslashes($_POST["id"])."'";
$result1 = mysql_query($sql_answer);
$row1 = mysql_fetch_object($result1);
$correctanswer = $row1->answers;
if (strtolower($answer) == strtolower($correctanswer))
{
echo 'Bravo, ai raspuns corect!';
$sql3= "INSERT INTO money VALUES ('','$money','$today')";
$res3 = mysql_query($sql3);
}
elseif (strtolower($answer) != strtolower($correctanswer)){

echo 'Raspuns gresit! Raspunsul corect era '.$correctanswer.'.';
}
}
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<?php echo "Intrebare: $question"; ?>
<br>
Raspuns: <input maxlength="60" name="answer" size="25" type="input">
<input name="id" type="hidden" value="<?php echo $row->id ?>">
<input name="submit" type="submit" value="Go">
<input name="submitted" type="hidden" value="TRUE">
</form>



REZULTATE:
<?php
$query1="SELECT money, data, Sum(money) AS SumOfMoney FROM money GROUP BY DATE_FORMAT(data, '%M %d, %Y') = NOW()";
$result1=mysql_query($query1);
$row1=mysql_fetch_assoc($result1);
echo '<br>Total: '.$row1['SumOfMoney'].' puncte

';
?>
237

Answer

Solution:

to get a random row from mysql database you can try this:

SELECT * FROM mytable ORDER BY RAND() LIMIT 1

if you want to show them just once in each session you could use $_SESSION variable to keep already displayed questions and exclude those in your query.

SELECT * FROM mytable WHERE question_id NOT IN(1,4,6) ORDER BY RAND() LIMIT 1

if you need help using session to keep and exclude asked questions I can add answer to those as well.

766

Answer

Solution:

 $max_value = 10;
                    $values = range(0, $max_value);
                    $counter = $max_value;
                    for($i = 1; $i < 11; $i++){
                        $rand_num = rand(1,$counter);
                        $current_number = $values[$rand_num];
                        array_splice($values, $rand_num, 1);
                        $counter--;

                        $query = "SELECT `question`, `op1`, `op2`, `op3`, `op4` FROM  `physics` WHERE `number`='$current_number'";
                        $query_run = mysql_query($query);

People are also looking for solutions to the problem: php - Posting a Laravel form is not working

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.