English 中文(简体)
如何查询随机行, 并追踪询问的内容( 重要部分)?
原标题:How do I query for random rows AND track what has been queried (that s the important part)
  • 时间:2012-05-24 20:22:20
  •  标签:
  • php
  • mysql

我已经知道如何随机进行 Mysql/php 查询, 但我如何追踪已经询问过的问题却不再被拉动。 使用大小写: 测试有10个问题。 问题需要随机顺序。 我如何追踪问题( rows) 2, 5,6, 8, 9 已经回答过, 问题1, 34, 7, 10 仍然存在。 这里我所知道的 :

$current_test = $_SESSION[ testid ];
// v v v query for this TEST
$tests = mysql_query("SELECT * FROM the_tests WHERE the_tests.testid=$current_test");
$test = mysql_fetch_array($tests);
// ^ ^ ^ query for this TEST


// v v v query for the QUESTIONS from this Test
// Generate
if ($test[ randomize ]==1){
    $offset_result = mysql_query("SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM the_questions WHERE the_questions.test_id_q=$current_test");//SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM the_questions WHERE `qid` NOT IN (1,5,6) AND the_questions.test_id_q=1
    $offset_row = mysql_fetch_object($offset_result);
    $offset = $offset_row->offset;
    $questions = mysql_query("SELECT * FROM the_questions WHERE the_questions.test_id_q=$current_test LIMIT $offset, 1 " );
}else{
    $questions = mysql_query("SELECT * FROM the_questions WHERE the_questions.test_id_q=$current_test");
}
$totalQuestions = mysql_query("SELECT * FROM the_questions WHERE the_questions.test_id_q=$current_test");
$totalQs = mysql_num_rows($totalQuestions);
$testQ = mysql_fetch_array($questions);

提前感谢!

最佳回答

这就是我想出的:

假设$_SESSION[qList]是一个看起来像这样的阵列:

$_SESSION[ qList ] = array(1,2,3,4,5,6,7,8,9,10);

当用户提交问题时, 存在一个 & lt; imput 类型 = "hidden" 值=" 5" 。 & gt; 其值为当前问题 ID 。 下面的 < 坚固 > $endend@/ 坚固 > 是这个问题 ID :

$_SESSION[ qList ] = array_diff($_SESSION[ qList ],array($eliminateQ));
$_SESSION[ qList ] = array_values($_SESSION[ qList ]);
$questions = mysql_query("SELECT * FROM tan_questions WHERE test_id_q=$current_test AND qid IN (".implode( , ,$_SESSION[qList]).") ORDER BY RAND() LIMIT 1");

这导致更新的qList阵列没有ID编号5:

$_SESSION[ qList ] = array(1,2,3,4,6,7,8,9,10);

整个过程一直持续到没有问题了为止。如果( ) 声明将用户传送到结果页面 。 Viola!

感谢 @pixeline, 感谢您的洞察力。 我把你的“ 坚固的” NOT 移到“ 坚固的” / " 坚固的 " 。 ) :

问题回答

您想要用 NOT IN (5,23,45); 补充 SQL Where 条款(5,23,45);

所以基本上保持每个随机项目 ID 值的数组, 并将其输入您的 SQL 条款 。

mysql_query("SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM the_questions WHERE the_questions.test_id_q=$current_test AND the_questions.id NOT IN (".implode( , ,$excluded).")");

Now, this raises another question: the $excluded array. Should it be a temporary exclusion or should it be permanent? If it should not be permanent, then store it in a session (ex: $_SESSION[ excluded ] ) If the user comes 3 days later, he will probably be able to re-answer that question.

如果这必须是永久性的,那么就保留一个事实,即这个问题已在一个专栏中回答过,或者简单地检查是否有答案?

mysql_query("SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM the_questions WHERE the_questions.test_id_q=$current_test AND the_questions.answer ==  ");




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签