-1

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.

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?

Cyndi
  • 95
  • 1
  • 11
  • Are you trying to get the page it's self to update as well without the user refreshing even when they're viewin git? – Ohgodwhy Jul 10 '14 at 02:00
  • I want it to do two things, I want it to stay the same for everyone until it is time for the 24hr change over, and it will change without a user refreshing. We have users on mobile phones who stay logged in for 12 hours at a time, so if they do not refresh, I would like it to update on it's own. – Cyndi Jul 10 '14 at 02:04
  • Why not store the generated descriptions in a database/text file, then regenerate them every 24 hours? – JW Lim Jul 10 '14 at 02:20
  • JW - This is originally where I was going with it. I was trying to use the "rantex" method but stopped when I realized it would refresh the text displayed every time someone refreshed the page. So I do have a text file with all 20 lines of my weather descriptions in it. I am intermediate at code by this point, I ma not sure how to tell the sever / website not to regenerate unless 24 hours as passed. I also need to designate that it is only looking at the server clock and not the users. – Cyndi Jul 10 '14 at 02:26

2 Answers2

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 = new Date();
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(DAY FROM CURDATE()) 
        * EXTRACT(MONTH FROM CURDATE()) 
        * EXTRACT(YEAR FROM CURDATE())),
    (select MAX(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.

Community
  • 1
  • 1
S McCrohan
  • 6,663
  • 1
  • 30
  • 39
  • I will warn that I have intermediate experience with code a lot of this is new territory for me, but I am a quick learner and eager to fix this problem. This is the problem ( different users time zones ) I had with other solutions I was trying to implement. Using your example, say I have 20 weather descriptions that I'd like to cycle through, how would I list them so that it filters at the appropriate time? Should they be contained in divs? should I put them into a SQL database and display them that way? – Cyndi Jul 10 '14 at 02:07
  • What tech is the rest of your site built on? I think it would be easy to use the approach above in any server-side framework (PHP, node.js, etc). That still leaves the matter of refreshing at midnight, but that IS properly a browser-side javascript issue. – S McCrohan Jul 10 '14 at 02:10
  • I've been primarily building this in PHP. – Cyndi Jul 10 '14 at 02:11
  • The absolute simplest way to list them would be to hardcode them into an array in your PHP. That's not the /best/ way, but it would get the job done. Making a SQL table full of them and selecting one row from it would be better, if you're already using SQL for other things. If you are, what flavor? MySQL? – S McCrohan Jul 10 '14 at 02:24
  • Yes I am using my SQL and PHPmyadmin to manage my login/registration stuff – Cyndi Jul 10 '14 at 02:27
  • If I can go the SQL route I would prefer it as I am fairly familiar with it and php / html. javascript, ajax, and jquery would be a whole new adventure for me, which I don't really mind, but if SQL is better, then I would much rather go that way. – Cyndi Jul 10 '14 at 02:34
  • The ajax solution is totally valid. However, updated my answer with a SQL example (untested, but should be right) if you want to try that. – S McCrohan Jul 10 '14 at 02:35
  • If I am understanding the example correctly, I need to create a new table on my database, with just the one column. Make sure each description has sequential IDs with no duplicates. It leaves me with- how would the SQL know that I wanted a 24 hour timer on it before it performed a new query? I also assume using your example it will display the same SQL result for everyone viewing the page? – Cyndi Jul 10 '14 at 02:43
  • That query can be performed as many times as you want in a given day, and will always return the same text, until the server ticks over to the next day. You'll need a different, client-side solution to the problem of refreshing the page. See here for that: http://stackoverflow.com/questions/21512551/how-to-update-your-homepage-at-a-certain-time – S McCrohan Jul 10 '14 at 02:49
  • Ah I think see now: where ID = 1 + MOD( (EXTRACT(DAY FROM CURDATE()) * EXTRACT(MONTH FROM CURDATE()) * EXTRACT(YEAR FROM CURDATE())), This section is what is telling it not to update until the server hits the following day, yes? The second link you posted is to push the new query result to the website, yes? – Cyndi Jul 10 '14 at 02:56
  • I think I have this working! I don't have a way to test it because it isn't going to update again ( supposedly ) until 24 hours from now. I want to change the code temporarily so I can see if it updates properly now. What would you suggest? SELECT description FROM weather WHERE id = 1 + MOD( (EXTRACT(DAY FROM CURDATE()) * EXTRACT(MONTH FROM CURDATE()) * EXTRACT(YEAR FROM CURDATE())), (select MAX(id) from weather)) – Cyndi Jul 10 '14 at 04:34
  • If you want it to change more frequently, you can change the extracts. For instance, adding in `* EXTRACT(HOUR_MINUTE FROM NOW())` will have it changing every minute. – S McCrohan Jul 10 '14 at 14:01
1

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>
    function getDataFromServer() {
        var options = {
            type: "GET", //make a GET http request
            url: "/myServerSidePHPPage", // to the server at this location
            dataType: "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)

Kyle Gobel
  • 5,530
  • 9
  • 45
  • 68
  • It appears to me there are several ways to use the jquery would you suggest downloading it and uploading it, or just use the google method? – Cyndi Jul 10 '14 at 02:17
  • I would use a CDN like google – Kyle Gobel Jul 10 '14 at 02:20
  • I see your update, YES this is originally where I was going with it. I got stuck when I realized the way I was doing it would refresh it every time the page was reloaded by a user. Not good. I have about 20 lines of "weather descriptions" in this text file, do you have a small example of what I would do to display the random "weather" EDIT: I was following the rantex php method. – Cyndi Jul 10 '14 at 02:21
  • This text file would be on your server, it would be the server's responsibility to deliver **1** weather description to the clients. I'm an ASP guy and don't know php syntax so I can't help you with that part. But once you send the JSON weather description to the clients you would display it like shown in the ``ajax.then`` function – Kyle Gobel Jul 10 '14 at 02:26
  • Thank for for the suggestions Kyle, I really wish I understood ajax better - I've never considered using it before trying to make this feature work. I am going to play with your example and try to get a better grip on the suggestion you gave me. I understand the basic idea of what you've got up there it's plugging it into a real world solution that I need to digest. – Cyndi Jul 10 '14 at 02:53