SQL结果随机循环

发布于 2024-11-17 06:19:49 字数 1391 浏览 0 评论 0原文

我有一个包含字段的 SQL 数据库。我可以通过以下方式获取该字段中的所有元素:

      $result=mysql_query($sql);
      while($row=mysql_fetch_array($result)){
         $ftype  =$row['ftype']; //name of field is 'ftype'
         print $ftype}  //do something  

我希望每次打开/刷新页面时打印出一个随机元素。

知道结果是一个包含我想要的信息的数组,我想随机选择数组的一个元素。另外,如果数组中有 N 个元素,我想在第二次再次显示任何元素之前选择所有 N 个元素一次。这个过程会不断重复。我知道如何在 java 或 python 中编写类似的代码,但我不认为这是我应该走的路。

我想我需要使用javascript,但我只是不确定该使用什么技术。

更新:

帕特里克的想法似乎正是我所寻找的。我想我会分享我现在所拥有的,也许你可以提出优化建议。我希望代码中的意图是显而易见的。

<?
session_start();
if (!isset($_SESSION['count']) || !isset($_SESSION['randomArray'])) {
      $count = 0;
      $randomArray = array();
      $sql="SELECT youtubeurl FROM Foodlist";
      $result=mysql_query($sql);
      while($row=mysql_fetch_array($result)){
           array_push($randomArray,$row['youtubeurl']);
      }
      shuffle($randomArray);
      $_SESSION['randomArray'] = $randomArray;
      $_SESSION['count'] = $count; 
} elseif ($_SESSION['count'] >= sizeof($_SESSION['randomArray'])){
   $_SESSION['count'] = 0;
   $randomArray = $_SESSION['randomArray'];
   shuffle($randomArray);
   $_SESSION['randomArray'] = $randomArray;
} else{
   $randomArray = $_SESSION['randomArray'];
   $count = $_SESSION['count'];
   echo $randomArray[$count];
   $_SESSION['count']++;
}
?>

I have a SQL database that contains a field. I can get all of the elements in that field by:

      $result=mysql_query($sql);
      while($row=mysql_fetch_array($result)){
         $ftype  =$row['ftype']; //name of field is 'ftype'
         print $ftype}  //do something  

I want a random element to be printed out each time the page is opened/refreshed.

Knowing that the result is an array containing the information i want, I want to randomly choose an element of the array. Also, if there are N elements in the array, I want to choose all N elements exactly once before I show any element again for second time. The process repeats itself. I know how to code something like this in java or python, but I don't think that's the way I should go.

I think I need to use javascript, but I'm just not sure what technology to use.

UPDATE:

Patrick's idea seems to be exactly what i was looking for. I thought I would share what I have now and maybe you can suggest optimization. I hope the intent in the code is obvious.

<?
session_start();
if (!isset($_SESSION['count']) || !isset($_SESSION['randomArray'])) {
      $count = 0;
      $randomArray = array();
      $sql="SELECT youtubeurl FROM Foodlist";
      $result=mysql_query($sql);
      while($row=mysql_fetch_array($result)){
           array_push($randomArray,$row['youtubeurl']);
      }
      shuffle($randomArray);
      $_SESSION['randomArray'] = $randomArray;
      $_SESSION['count'] = $count; 
} elseif ($_SESSION['count'] >= sizeof($_SESSION['randomArray'])){
   $_SESSION['count'] = 0;
   $randomArray = $_SESSION['randomArray'];
   shuffle($randomArray);
   $_SESSION['randomArray'] = $randomArray;
} else{
   $randomArray = $_SESSION['randomArray'];
   $count = $_SESSION['count'];
   echo $randomArray[$count];
   $_SESSION['count']++;
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烧了回忆取暖 2024-11-24 06:19:49

您可以在没有 JavaScript 的情况下执行此操作,但是您需要打开/维护一个会话。

伪代码:

data = data_from_mysql()

choice = random.choice(data, exclude = SESSION['choices'])
SESSION['choices'].append(choice)

print choice

if len(SESSION['choices']) == len(data):
    SESSION['choices'] = []

You can do this without Javascript, however you'll need to open/maintain a session.

Pseudocode:

data = data_from_mysql()

choice = random.choice(data, exclude = SESSION['choices'])
SESSION['choices'].append(choice)

print choice

if len(SESSION['choices']) == len(data):
    SESSION['choices'] = []
高速公鹿 2024-11-24 06:19:49

如果你想要一个随机结果,让 dbms 来处理它。将其添加到 sql 查询的底部。

ORDER BY RAND()
LIMIT 1

If you want a random result let the dbms take care of it. Add this to the bottom of your sql query.

ORDER BY RAND()
LIMIT 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文