【问题标题】:php shuffle accessphp随机访问
【发布时间】:2013-04-10 13:55:54
【问题描述】:

我正在使用 PHP 和 Access 数据库。此代码运行良好,并显示 34 个问题。我想随机化问题的显示顺序,但 RAND() 函数不适用于 Access。我想对数组进行洗牌,但无法正确使用语法。任何帮助将不胜感激。

$info2 = "SELECT * FROM CCNAATQuestions";
$rs2=odbc_exec($conn1, $info2);

while ($row = odbc_fetch_array($rs2)) {

    echo "<strong>" . $row["Question"] . "</strong>";

}

【问题讨论】:

  • 在 Access 中,函数是 Rnd() 而不是 RAND()。请参阅下面戴夫的回答。

标签: php ms-access


【解决方案1】:
SELECT * FROM [tableName] ORDER BY rnd(INT(NOW*id)-NOW*id)

尝试上述方法,其中 id = 您的主键列

【讨论】:

  • @James 不客气,如果你对它感到满意,我会很感激你点击接受的答案。
【解决方案2】:
$rows = array();
while ($row = odbc_fetch_array($rs2)) {

$rows[]="<strong>" . $row["Question"] . "</strong>";

}

shuffle($rows);

【讨论】:

    【解决方案3】:
    $info2 = "SELECT * FROM CCNAATQuestions";
    $rs2=odbc_exec($conn1, $info2);
    
    $questions = array();
    
    while ($row = odbc_fetch_array($rs2))
    {
        $questions[] = $row;
    }
    
    shuffle($questions);
    
    foreach ($questions as $row)
    {
        echo "<strong>" . $row['Question'] . "</strong>";
    }
    

    使用 PHP 中的随机播放:http://php.net/manual/en/function.shuffle.php

    【讨论】:

      【解决方案4】:

      您必须先获取所有行,然后才能对其进行随机播放:

      $rows = array();
      while ($row = odbc_fetch_array($rs2)) {
          $rows[]= $row;
      }
      
      shuffle($rows);
      foreach ($rows as $row) {
          echo "<strong>" . $row["Question"] . "</strong>";
      }
      

      您应该看看@Dave 的解决方案,因为它将使用访问功能。不幸的是,我无法测试它,因为我手头没有 Windows 系统

      【讨论】:

        猜你喜欢
        • 2013-11-29
        • 1970-01-01
        • 1970-01-01
        • 2011-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-07
        相关资源
        最近更新 更多