随机显示的文字 [英] Randomly Displayed Text

查看:176
本文介绍了随机显示的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为游戏网站开发天气生成器".我已经在互联网上搜索了几个小时,试图找到最好的方法来做我想做的事,但我似乎找不到能给我一切的东西.我研究过Jquery,AJAX,Javascript,PHP,html的混合物,显示随机div容器,SQL,我研究了很多.

I am working on a "weather generator" for a game website. Ive been searching the internet for hours now trying to come up with the best method to do what I want and I just can't seem to find something that gives me everything. I've looked at Jquery, AJAX, Javascripts, mixtures of PHP, html, displaying random div containers, SQL, I've researched so many.

我想做什么:我有一组基于文本的天气描述,希望每24小时随机更改一次.这里有个要注意的地方:它必须始终为每个人显示-same-.

What I want to do: I have a set of text based weather descriptions that I want to randomly change every 24 hours. Here is the catch: It has to display the -same- thing for -everyone- at all times.

有人能指出我正确的方向吗?

Can anyone point me in the right direction?

推荐答案

我的方法是使用一些稳定的数学方法将当前日期转换为索引,并将其转换为您的描述集.它不会真正是随机的-实际上,它将完全是确定性的-但对您以外的任何人显然都是随机的.

My approach would be to use some stable piece of math to convert the current date into an index into your set of descriptions. It won't truly be random - in fact, it will be entirely deterministic - but it will be APPARENTLY random to anyone but you.

例如:

var today = new Date();
var weatherIndex = (today.getDate() * today.getMonth() * today.getYear()) % weatherListSize;

javascript中的示例,但是您实际上想在服务器上执行此操作-否则,您可能会在任何给定时刻针对不同时区的用户获得不同的结果(此外,您确实不希望发送所有每次都记录下您的天气文本-只是今天).

Example in javascript, but you actually want to do this on the server - otherwise, you're potentially going to get different results at any given moment for users in different timezones (plus, you really don't want to send all of your weather text down every time - just today's).

您提到您已经在使用MySQL.一个SQL示例如下:

You mentioned you're using MySQL already. A SQL example would be along the lines of:

    select text from WeatherTexts
    where ID = 1 + MOD(
        (EXTRACT(DAY FROM CURDATE()) 
        * EXTRACT(MONTH FROM CURDATE()) 
        * EXTRACT(YEAR FROM CURDATE())),
    (select MAX(ID) from WeatherTexts));

假设顺序编号.由于CURDATE()整天保持不变,因此该查询将在给定的一天中(每次用户执行)返回完全相同的结果.第二天运行时,结果(几乎总是)会有所不同.

assuming sequential IDs. Because CURDATE() remains the same all day, this query will return exactly the same result on every execution (by every user) during a given day. When it is run on the next day, the result will (almost always) be different.

将该查询包含在您的PHP中,以便在每次加载页面时都运行该查询.然后将一些JavaScript添加到页面中,类似于如何是否要在特定时间更新您的主页?,以确保该页面在午夜之后重新加载以获取新内容.记住要在计时器中使用服务器时区!而且,如果您希望人们打开该页面的时间超过24小时,请记住在上一个计时器到期时启动一个新的(24小时)计时器.

Include the query in your PHP so it's run each time the page is loaded. Then add some javascript to the page similar to what's described in How to update your homepage at a certain time?, to ensure that the page reloads just after midnight to get the new content. Remember to use the server timezone in the timer! And if you expect people may have the page open for more than 24 hours, remember to start a new (24-hour) timer when the previous one expires.

这篇关于随机显示的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆