Randomly Displayed Text
Solution 1:
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.
For instance:
var today = newDate();
var weatherIndex = (today.getDate() * today.getMonth() * today.getYear()) % weatherListSize;
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).
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(DAYFROM CURDATE())
*EXTRACT(MONTHFROM CURDATE())
*EXTRACT(YEARFROM CURDATE())),
(selectMAX(ID) from WeatherTexts));
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.
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.
Solution 2:
The way I would do this is using some ajax, and a server side language (php works).
You would use the ajax and javascript to hit your server and let the server tell the client what to draw. (having that logic server side would ensure that everyone would see the same thing).
Then to keep all the clients updated, I would put that ajax call on some time of timeout (via setTimeout
, that would basically re-ask the server every say 10 seconds if it should change what is shown.
the javascript would look something like this (using jquery here)
<script>functiongetDataFromServer() {
var options = {
type: "GET", //make a GET http requesturl: "/myServerSidePHPPage", // to the server at this locationdataType: "json"//make your php page return json
};
$.ajax(options).then(function (data) {
//data will be the json that your php page returns//use this data to update your page//pretending you have a <div id='weather'> somewhere on your page//and also assuming you returned json with a weatherDescription property//you could load the text or html into that div like::
$('#weather').html(data.weatherDescription);
});
}
//this is the jquery onDocumentReady function, it fires when the page loads
$(function() {
//tell the page to call the getDataFromServer function every //10 * 1000 milliseconds (10 seconds)window.setTimeout(getDataFromServer, 10 * 1000);
});
</script>
I dont' know php to help you with that part, but hopefully this can get you started and once you hit more specific problems you can ask questions about those.
As for outward appearing randomness you can do that in several ways. One easy way that comes to mind is to generate a random 'weather', and store it a text file with the time it was generated. Your php action could read this text file to give the clients the random weather, and also read the time it was generated to see if 24 hours has passed. If 24 hours pass generate a new random weather. (You could just store these values in memory on the server as well)
Post a Comment for "Randomly Displayed Text"