简体   繁体   English

如何从数据库PHP的两个表中随机选择列

[英]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.

相关问题 如何在表行中显示两个数据库表中的选定值 - How to display selected values from two database tables in table row 如何将从数据库中随机选择的图像放置在页面上的随机位置? - How to place randomly selected images from a database at random locations on a page? 如何为从数据库中随机选择的记录提供链接 - How to give a record that is being randomly selected from a database a link 如何用数据库中的值创建PHP两列表? - How to create PHP two column table with values from the database? 从数组PHP中的先前随机选择的元素中随机选择 - Randomly selecting from previous randomly selected elements from array PHP php左连接来自两个differents数据库的两个mssql表 - php left join two mssql tables from two differents database 从数据库列中选择的选项无结果(php-mysql) - No result from options selected from a database column (php-mysql) 如何在Codeigniter中从具有相同数据库列名称的两个联接表中获取数据? - How to fetch data from two join tables with same database column names in codeigniter? 如果它与 laravel 中的相同记录匹配,如何比较数据库的两个不同表中的多个列并更新 - how to compare more than one column from two different tables of database and update, if it match the same record in laravel 如何在PHP中从数据库中选择一个选定的值 - How to select a selected value from database in PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM