0

Looking for breadcrumbs/hints on a project I've found in my lap. I typically work in pure HTML/CSS with sprinkles of basic PHP/Javascript. Usually if someone sends me in the right direction I can piece things together.

My client wants a ticker that updates every SERVER DAY. For Example:
1. Value X is set to 100
2. check if 24 hours (SERVER time, not client time) has passed
3. Run number generator between 5-10. Store in value Y
4. Update value X to X+Y. Re-store as value X
5. Return to step 2

I have searched this problem a couple different places but haven't quite found the direction I'm looking for. This question was close: javascript, increase number day by day

and led me to believe PHP might be my language of choice...but I'm finding more information geared towards this question written in javascript and stored client-side in cookies, which wasn't really what we were going for.

Does anyone have any ideas to get me started in the right direction? Doesn't seem to hard logically, just need to figure out the semantics. Any help is super appreciated....

UPDATE:

I've decided to use wordpress cron and found the handy wordpress crontrol plugin. This fixes the TIME issue, and thanks to 'Misunderstood's help I think I've got that file opening/random number thing down. Now what I am curious about is how to make a line of text on the site itself (could be div, h1, whatever) display the contents of the txt file 24/7. I used to know how to do this but it's escaping me. Do I need to use AJAX or javascript?

Community
  • 1
  • 1

2 Answers2

1

Run a cron job that calls a php script every 24 hours at what ever time you want that to be.

$fp = fopen('x.txt','r+');
$x = intval(fread($fp));
$y = mt_rand (1,5);
fwrite($fp,$y + $x);
fclose($fp);
Misunderstood
  • 5,534
  • 1
  • 18
  • 25
  • Curious - just did some research into cron, which I admit I know nothing about. I'm not sure at this point if my client's server allows the use of cron but I did read that wordpress has a psuedo-cron system. The client's site is built with wordpress- does that add any options to my challenge? – Caitlin Alexander Jan 19 '15 at 01:44
  • If the server has cPanel, there is an icon in the "Advanced" group labeled "corn jobs". The command would be something like: php /home/use/public_html/script.php, where script.php is the name of the script to run. – Misunderstood Jan 19 '15 at 01:58
  • Thanks so much I really appreciate both your thoughtful responses. I did some research and found a plugin for wordpress that allows me to set up cron jobs easily. The only piece I'm missing is calling the contents of the .txt file into a section of the website....do I need AJAX or...? – Caitlin Alexander Jan 19 '15 at 15:19
  • If your page is generated by PHP, the easiest way to bring in a local txt via PHP is `readfile(filename);` where filename is something like /home/user/textfiles/file.txt. From any web site `echo file_get_contents('http://www.exasmple.com/file.txt');` If it's an HTML page then AJAX if it is dynamic. – Misunderstood Jan 19 '15 at 19:56
1

If you don't have access to do a cronjob, you can do the following: When the script, that has to display this value is called, open a file (or read from a DB) the last time it was updated. For example, if the script was called today (2015.01.18), and the last call was on 2015.01.13 then you have to update X 5 times. So add 5 random values to X then store it with the new (today's) date.

Fenistil
  • 3,734
  • 1
  • 27
  • 31