我有一个测验,我想得到随机问题(不重复)

时间:2014-02-04 15:05:22

标签: php jquery mysql ajax

正如标题所说,我的PHP脚本存在很大问题。我想从我的数据库中提取(获取)随机问题,然后显示它。我希望脚本看起来像这样:http://www.filebox.ro/download.php?key=d07a7jdcf2ouwu62 但是有一个MySQL数据库和一个用于管理问题和用户的管理仪表板。 这是我的脚本:http://fbx.ro/hxsztelz2chq1ekk

这是我的index.php 正如你在第6行看到的,我不知道该怎么做...... 如果你可以教我如何提取一个以上的问题,比如测验(aprox.:20随机问题)

 <?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 style="color: red;">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

';
?>

2 个答案:

答案 0 :(得分:2)

从mysql数据库中获取一个随机行,你可以试试这个:

SELECT * FROM mytable ORDER BY RAND() LIMIT 1

如果您想在每个会话中只显示一次,您可以使用$ _SESSION变量来保留已显示的问题并排除查询中的问题。

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

如果您需要使用会话帮助保留和排除问题我也可以添加答案。

答案 1 :(得分:0)

 $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);