[英]How to randomly selected column from two tables in database PHP
i want to make reading toefl test. 我想做阅读托福考试。 i have two tables named 'id_reading' and 'soal_reading'.
我有两个名为“ id_reading”和“ soal_reading”的表。 id_reading table contain id and text.
id_reading表包含id和文本。 soal_reading contain id, question, option abcd and answer.
soal_reading包含ID,问题,选项abcd和答案。 so i want to display data from that tables.
所以我想显示该表中的数据。 but i want to display randomly all column in soal_reading tables except id column.
但我想随机显示soal_reading表中的所有列(id列除外)。 i try but it wont random.
我尝试,但不会随机。 please help.
请帮忙。
<?php
include "conection.php";
$query = mysql_query("
SELECT id_reading.id
, text
, id_reading.text
, soal_reading.pertanyaan
, a
, b
, c
, d
, jawaban
from id_reading
, soal_reading
where id_reading.id = soal_reading.id
");
if ($query) {
while ($row = mysql_fetch_array($query)) {
echo "
<tr>
<td>".$row['id']."</td>
<td>".$row['text']."</td>";
$q = mysql_query("SELECT * from soal_reading order by rand()");
if ($q) {
while ($r = mysql_fetch_array($query)) {
echo "
<td>".$r['pertanyaan']."</td>
<td>".$r['a']."</td>
<td>".$r['b']."</td>
<td>".$r['c']."</td>
<td>".$r['d']."</td>
<td>".$r['jawaban']."</td>
<td>
<a href=\"edit_reading.php?id=".$row['id']."\">Edit</a> |
<a href=\"hapus_reading.php?id=".$row['id']."\">Delete</a>
</td>
</tr>";
}
}
}
}
?>
Your query set in a random way questions but not fields in the question. 您的查询以随机方式设置问题,但不设置问题中的字段。 So if you have 3 questions for example Q1, Q2,Q3 they are ordered in a random way, not the fields.
因此,如果您有3个问题(例如Q1,Q2,Q3),则这些问题将以随机方式(而不是字段)排序。
I suggest you to put your $r['a'] .... $r['d']
into an array and then extract a number from 0 to count($array)
to get a possible answer and remove that element from the array. 我建议您将
$r['a'] .... $r['d']
放入数组,然后从0 to count($array)
提取一个数字0 to count($array)
以获取可能的答案,然后从中删除该元素数组。 Do that until all possible answers are extracted. 这样做直到提取出所有可能的答案。
(count($array)==0)
. (count($array)==0)
。
In summary, your query provides you random order for your questions and in PHP you give a random order to your fields. 总之,您的查询为您的问题提供了随机顺序,而在PHP中,您为字段提供了随机顺序。
For some code, please comment. 对于一些代码,请发表评论。 Bye
再见
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.