...

View Full Version : Code to display DAILY random string?



Iconoclast
10-18-2009, 06:44 PM
Hi guys, I've got a script that will display a random quote each time the page is visited or refreshed, but I'd like to have something that will pick a random string from a list and display it for the entire day...is this possible? Thanks!

Phil Jackson
10-18-2009, 06:48 PM
it is possible indeed. How is your php knowledge?

Iconoclast
10-18-2009, 09:03 PM
I found this code, but I don't know how to use it?


function RandomQuoteByInterval($TimeBase, $QuotesArray){

// Make sure it is a integer
$TimeBase = intval($TimeBase);

// How many items are in the array?
$ItemCount = count($QuotesArray);

// By using the modulus operator we get a pseudo
// random index position that is between zero and the
// maximal value (ItemCount)
$RandomIndexPos = ($TimeBase % $ItemCount);

// Now return the random array element
return $QuotesArray[$RandomIndexPos];
}

Phil Jackson
10-18-2009, 09:57 PM
<?php

function RandomQuoteByInterval($TimeBase, $QuotesArray){

// Make sure it is a integer
$TimeBase = intval($TimeBase);

// How many items are in the array?
$ItemCount = count($QuotesArray);

// By using the modulus operator we get a pseudo
// random index position that is between zero and the
// maximal value (ItemCount)
$RandomIndexPos = ($TimeBase % $ItemCount);

// Now return the random array element
return $QuotesArray[$RandomIndexPos];
}

$QuotesArray = array("Have a great day!", "HAHAHAHHA looser", "You smell like a bed of roses");
echo RandomQuoteByInterval(1440, $QuotesArray);

?>


should do the trick

Iconoclast
10-18-2009, 09:58 PM
Works perfectly, thanks so much mate!

Phil Jackson
10-18-2009, 09:59 PM
Dont thank me, thank the bloke who wrote the code!

Iconoclast
10-20-2009, 03:14 AM
Hey, sorry, now I have another question.

It seemed to be working, but it doesn't seem to cycle through them every 24 hours.

It has something to do with this line here:

echo RandomQuoteByInterval(1440, $QuotesArray);

I've tried substituting various values in, but so far nothing really seems right. Either way, even when I use a really small number like 0.000000001, it doesn't seem to cycle through them (and if it is, then the timer is REALLY long before it changes to the next one).

I'm assuming 1440 is because of the # of minutes in a day, but what if that's not what it's being measured by? Any ideas?

kbluhm
10-20-2009, 04:05 AM
Here is that function and example:
http://www.jonasjohn.de/snippets/php/daily-random-quotes.htm

So wherever 1440 came from, it should simply be the day of the year... not very random.

You may need to employ a database or caching. Let's go back to square one: How are your quotes stored?

Iconoclast
10-20-2009, 04:55 AM
I just used an array, like so:


function RandomQuoteByInterval($TimeBase, $QuotesArray){

// Make sure it is a integer
$TimeBase = intval($TimeBase);

// How many items are in the array?
$ItemCount = count($QuotesArray);

// By using the modulus operator we get a pseudo
// random index position that is between zero and the
// maximal value (ItemCount)
$RandomIndexPos = ($TimeBase % $ItemCount);

// Now return the random array element
return $QuotesArray[$RandomIndexPos];
}

$QuotesArray = array("Phoenix",
"30 Seconds To Mars",
"The Beatles",
"Rise Against",
"The Spill Canvas",
"The Killers",
"Metric",
"Aerosmith");
echo RandomQuoteByInterval(1440, $QuotesArray);

It's actually for a Band/Artist of the day. I've got way more in that, I just shortened it to a few so you would get the idea. Does this help?

Phil Jackson
10-20-2009, 06:10 AM
just wrote you this....yawn 3 hours kip was nice...



<?php
//http://www.actwebdesigns.co.uk
//luke@actwebdesigns.co.uk

function RandomQuoteByInterval($howOften, $QuotesArray){

if(file_exists("randomQuote/data.txt"))
{
$handle = fopen("randomQuote/data.txt", 'r');
$lines = file("randomQuote/data.txt");
$hash = trim($lines[0]);
$current = trim($lines[1]);
fclose($handle);

$trueHash = md5($current.floor((time()/(($howOften)*60))));
if($hash!=$trueHash)
{
while(true)
{
$randomKey = rand(0, (count($QuotesArray)-1));
if($QuotesArray[$randomKey]!=$current)
{
if($fp = @fopen("randomQuote/data.txt", 'w'))
{
fwrite($fp, $trueHash."\n".$QuotesArray[$randomKey]);
fclose($fp);
return $QuotesArray[$randomKey];
}
else
{
die("Error: problems opening data.txt");
}
break;
}
}
}
else
{
return $current;
}
}
else
{
if(!file_exists("randomQuote/"))
{
mkdir("randomQuote/", 0700);
}

$randomKey = rand(0, (count($QuotesArray)-1));
$trueHash = md5($QuotesArray[$randomKey].floor((time()/(($howOften)*60))));
if($fp = @fopen("randomQuote/data.txt", 'w'))
{
fwrite($fp, $trueHash."\n".$QuotesArray[$randomKey]);
fclose($fp);
return $QuotesArray[$randomKey];
}
else
{
die("Error: problems opening data.txt");
}
}
}

$QuotesArray = array("Phoenix",
"30 Seconds To Mars",
"The Beatles",
"Rise Against",
"The Spill Canvas",
"The Killers",
"Metric",
"Aerosmith");

echo RandomQuoteByInterval(1440, $QuotesArray); // add howOften in minutes 1440 = 24*60

?>

Iconoclast
10-20-2009, 06:24 AM
Wow thanks mate...but everytime I refresh the page, it displays a new artist/band from the array? Even though the value is set to 1440?

I had to manually create the directory called randomQuote, as well as data.txt and chmod permissions to 777.

One question though I'm kind of confused, what exactly is the data.txt file for? What is being written to that file? I checked it and it's got a long string of random characters, then on the next line down is the artist that comes up as the one of the day on my page. What's that for?

Thanks again mate! :) +thanks

Iconoclast
10-21-2009, 12:42 AM
^^Any ideas?

mlseim
10-21-2009, 01:22 AM
Iconoclast ...

What do you consider "a day"?
Is that based on YOUR timezone?
Or your server timezone?
Or your visitor's timezone?

For me, the next day (Wed) starts in about 5 hours.
For my friend in Australia, it's already Wed, 10AM ...
So, which quote will he see?

You need to either pick one timezone to use, or use cookies and IP
address to attempt to pick the user's timezone. The internet makes
the world very small, but time is very abstract.

twisucshir
10-21-2009, 02:54 AM
Hey, sorry, now I have another question.

It seemed to be working, but it doesn't seem to cycle through them every 24 hours.

It has something to do with this line here:

echo RandomQuoteByInterval(1440, $QuotesArray);

I've tried substituting various values in, but so far nothing really seems right. Either way, even when I use a really small number like 0.000000001, it doesn't seem to cycle through them (and if it is, then the timer is REALLY long before it changes to the next one).

I'm assuming 1440 is because of the # of minutes in a day, but what if that's not what it's being measured by? Any ideas?

This is an interesting discussion. thank you for sharing
However I think you should vary more examples to your writing much more interesting !
_________________________
Faire simulation pret personnel en ligne | (http://pretpersonnelenligne.org) Faire calcul taux pret personnel en ligne | (http://pretpersonnelenligne.org/pret-personnel-en-ligne/) Demander un financement simulation credit personnel (http://pretpersonnelenligne.org/simulation-credit-personnel/)

Iconoclast
10-23-2009, 06:16 AM
Iconoclast ...

What do you consider "a day"?
Is that based on YOUR timezone?
Or your server timezone?
Or your visitor's timezone?

For me, the next day (Wed) starts in about 5 hours.
For my friend in Australia, it's already Wed, 10AM ...
So, which quote will he see?

You need to either pick one timezone to use, or use cookies and IP
address to attempt to pick the user's timezone. The internet makes
the world very small, but time is very abstract.

Well, my timezone is GMT - 5 hours. So I would be happy using that. But I'd settle for whichever method is easiest, provided that it changes every 24 hours instead of each time the page is refreshed.

Just as a side note, would I not be able to somehow use the server's time? Thanks.

mlseim
10-23-2009, 12:33 PM
Sure, you would most likely use your server's time.

But the quote will change for you at midnight (your server time).
For others, the quote will change at 2pm in the afternoon, or 11am
in the morning ... etc.

You could change according to user cookies, but you won't know which
quote any particular user is seeing (not knowing their timezone).

Iconoclast
10-23-2009, 01:40 PM
That's okay. I'm not too concerned if it's a few hours off...so I guess server time would be the best option? How would that look? Thanks again?

mlseim
10-23-2009, 04:21 PM
OK, how about this thought ...

It only switches once per day.

By having it random, there is the possibility that the same quote could be repeated two days in a row.

What if you had a text file with 365 quotes. You load that text file into an array,
and then use PHP to display the quote (line in array) associated with the day of
the year. There would never be any repeats until the next year ... or until the "past"
quotes were replaced.

So your text file might be called "quotes.db" ... and there are 365 lines in that file (quotes).



<?php

// Open the quote text file ...
$quote = file("quotes.db");

// Get current day of the year.
$day = date("z");

echo $quote[$day];

?>

Iconoclast
10-23-2009, 04:27 PM
Well, I actually thought about that. And I came up with a JavaScript code that would do exactly that: change the message based on the day and find the corresponding item in the array.

But I've had another idea. Could I set this up via a Cron Job? My hosting has cPanel, and I know Cron Jobs are included.

After some Googling, I've found this to be the common syntax for adding a Cronjob:

php -q /home/myaccountname/public_html/randomband.php

At the moment, randomband.php looks like so:


<?php

function RandomQuoteByInterval($TimeBase, $QuotesArray){

// Make sure it is a integer
$TimeBase = intval($TimeBase);

// How many items are in the array?
$ItemCount = count($QuotesArray);

// By using the modulus operator we get a pseudo
// random index position that is between zero and the
// maximal value (ItemCount)
$RandomIndexPos = ($TimeBase % $ItemCount);

// Now return the random array element
return $QuotesArray[$RandomIndexPos];
}

$QuotesArray = array("Phoenix",
"Aha",
"Carolina Liar",
"The Fray",
"Howie Day",
"Finch",
"Brand New",
"The Hush Sound",
"Aerosmith");


echo RandomQuoteByInterval(1440, $QuotesArray);

?>

There's a lot more items in the array, but I shortened it to a few in order to get the point across. Also, I know I won't need the QuoteByInterval stuff if it's running via Cronjob, but I wasn't sure how exactly to remove it in order to have the code still work.

Now, the question I have is, I have a PHP block on my webpage that I use to echo the string that it determines. Can I still do that from a Cronjob?

mlseim
10-23-2009, 04:41 PM
What would the purpose be for the Cronjob?

The only thing that a CRON would be able to do is pick the quote and
save it into another file, like an RSS feed or something. What else would
a cron script do with the quotes? And why would that be necessary?

It's way more easy to use PHP to render that quote every time, than to have
a CRON job write to a file every day - and you still would need to use a PHP "include".

EDIT:
What exactly does the quotes file look like?
Maybe I'm not getting what you're doing?


.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum