Jump to content

random quote every day


dansk

Recommended Posts

Hello my friends at phpfreaks,

I am using this random quote generator

[code]<?php
//-----------------------------------------------------------------------------
/*
author: Thomaz Ebihara
http://www.php-freebies.com
date: November 2005
script version: 1.0
*/
//-----------------------------------------------------------------------------
?>
<?php
$file = file('quotes.txt');
$quotes = array();
$authors = array();
$count = 0;
foreach($file as $k => $v)
{
if(preg_match("#^\r?\n$#", $v) == 0)
{
$count++;

if($count % 2 == 1)
{
preg_match("#quote\:(.+)\r?\n?#i", $v, $matches);
$quotes[] = trim($matches[1]);
}
else
{
preg_match("#author\:(.+)\r?\n?#i", $v, $matches);
$authors[] = trim($matches[1]);
}
}
}
$rnd = array_rand($quotes);
$rnd_quote = $quotes[$rnd];
$rnd_author = $authors[$rnd];
echo "<p class=\"quote\">$rnd_quote</p>";
echo "<p class=\"author\">$rnd_author</p>";
?>[/code]

I like it, but I would like it to display the quote every 24 hours or everyday?

Is this possible?

Thank you
Link to comment
Share on other sites

You could make another file called quote2.txt
In quote2 you could have
[code]
1111111111|I'm a moron|George W. Bush
[code]
Then read quote2.txt explode the |'s if the timestamp is >= time() + (3600 * 24) then pick another random quote... Run a while loop to make sure the quotes dont match, that way if they do the while loop keeps going and picks another quote.

Then you open quote2 with w+ so it over writes it then write time()|quote|author to it.

The downside would be, it would have to do the time check every time the page was loaded, but currently you're running an entire file through anyways, so it would actually be faster... I can code it if you want, but I would rather you do it lol[/code][/code]
Link to comment
Share on other sites

No, cookies are a bad idea when working with time.  I personally use FireFox and have it set to clear my cookies, saved form data, history and all that fun stuff when ever I close it because I'm paranoid like that... Also, if the time was set in cookies, people would all have different quotes at different times, which could be good or bad...

Anyways to help you with your logic, I'll write a little script, but it'll take me a few minutes...

edit: what does the quotes file you use look like?  I looked through your script, but I'm horrible with regexp...
Link to comment
Share on other sites

well i think its simple.

if you have a database of quotes, or simply even an array, then youd do something like this:


[code]
$quote_array['1'] = "Quote 1";
$quote_array['2'] = "Quote 2";
$quote_array['3'] = "Quote 3";
$quote_array['4'] = "Quote 4";



$Q_Number = rand(1, 4);
echo $quote_array[$Q_Number];

[/code]
mind you there might be a better way of doing it.  somewhere in there you would have to do some if's to check the time,  and then you would do something like

[code]

$latest_date = 1/1/07;

$today_date = date(d,m,y); # i think thats the right format

if(today_date != latest_date) {

#do the quote generator above

}
else {

echo $current quote;

}

[/code]

i decided that you should store the current quote decided from the array.... in a $current_quote variable, therefore its easier to read and use

GOOD LUCK
Link to comment
Share on other sites

Well heres what you need to do... if you have a MySQL database...

[code]
<?php
// Connect to database
// This is up to you to fill in

// Todays date, for other things.
$dateToday = date("Y.m.d");

// Request todays quote.
$getQuote = mysql_query("SELECT * FROM quotes WHERE date = '$dateToday'");

// Check if there is a quote set today.
if ($quoteToday = mysql_fetch_array($getQuote)) {

  // There already is a quote today, lets just repeat.
  echo "\"{$quoteToday['quote']}\" - {$quoteToday['author']}";

}
// If there is no quote lets add one.
else {

  // No quote today? Well lets find one.
  /* How ever you fetch your quotes */

  // Lets add this
  mysql_query("INSERT INTO quotes (quote, author, date) VALUES ($quote, $author, $dateToday)");

}
php?>
[/code]

This code is untested and should only be used as a guide.
Link to comment
Share on other sites

He pulls the quotes from a file....

Havent tested it, but theres no reason this shouldnt work:

[code=php:0]
<?php
$quotes_file = "quotes/quotes.txt";
$quotes_time = "quotes/time.txt";
$new_quote_time = 24*60*60; //how often to get a new quote

if(!file_exists($quotes_time)) {
$han = @fopen($quotes_time, "w+");
if(!file_exists($quotes_time)) {
die("The time file does not exist, and could not be created");
}
else {
fwrite($han, "0");
}
@fclose($han);
}

$qt_content = file($quotes_time);
$qt_content = explode("|", $qt_content);
$filetime = $qt_content[0];
$quote = $qt_content[1];
$author = $qt_content[2];

if((time() - $filetime) > $new_quote_time) {
$file = file($quotes_file);
$quotes = array();
$authors = array();
$count = 0;
foreach($file as $k => $v)
{
if(preg_match("#^\r?\n$#", $v) == 0)
{
$count++;

if($count % 2 == 1)
{
preg_match("#quote\:(.+)\r?\n?#i", $v, $matches);
$quotes[] = trim($matches[1]);
}
else
{
preg_match("#author\:(.+)\r?\n?#i", $v, $matches);
$authors[] = trim($matches[1]);
}
}
}
$rnd = array_rand($quotes);
$quote = $quotes[$rnd];
$author = $authors[$rnd];

$han = fopen($quotes_time, "w+");
fwrite($han, time() . "|" . $quote . "|" . $author);
fclose($han);

}
echo "{$author} said <i>{$quote}</i>";
?>
[/code]
Link to comment
Share on other sites

Thanks a lot for the help guys.

here you go corbin, this is how my quote file looks like

quote:hello world 1<br>commentry alkjdflajf
author:author 1
quote: hello world 2<br>commentry 355345435
author: author 2
quote: hello world 3<br>commentry *******
author: author 3
quote: hello world 4<br>commentry 3addddddddd
author: author 4

btw, i created a time file, do I have have to put something in there?

----
I am really confused between all the solutions I was given, all of them seem to be cool, I never thought of using Mysql with quotes, but because of you DarkendSoul, i am reconsidering my plans.

Link to comment
Share on other sites

okay, here's what i have when trying yours

but this is not the file, the acutall file will contain 100's of them



<?php
$file = file('quotes.txt');
$quotes = array();
$authors = array();
$count = 0;


$latest_date = 1/1/07;

$today_date = date("Y.m.d"); # i think thats the right format

if($today_date != $latest_date) {

foreach($file as $k => $v)
{
if(preg_match("#^\r?\n$#", $v) == 0)
{
$count++;

if($count % 2 == 1)
{
preg_match("#quote\:(.+)\r?\n?#i", $v, $matches);
$quotes[] = trim($matches[1]);
}
else
{
preg_match("#author\:(.+)\r?\n?#i", $v, $matches);
$authors[] = trim($matches[1]);
}
}
}
$rnd = array_rand($quotes);
$rnd_quote = $quotes[$rnd];
$rnd_author = $authors[$rnd];

}
else {


echo "<p class=\"quote\">$rnd_quote</p>";
echo "<p class=\"author\">$rnd_author</p>";

}

?>

and of course, it's not working b/c of my dumb cut and paste
Link to comment
Share on other sites

I have only read the first 5 or so posts so forgive me if this has already been mentioned.

I would personally use a database to store the quotes, then have a random variable. I would check that the count in the quotes table where id = the random variable = 1. If the count is 0, I would "remake" the random number and do the same again and again until I got a number where count = 1. I would then draw the quote out of the table where id = random number and display that quote.

RC
Link to comment
Share on other sites

Wouldn't this make certian quotes only show on certian days, which will not make it truly random.

The one that is closest to what I need is in [quote author=PC Nerd link=topic=120655.msg498833#msg498833 date=1168156650]
well i think its simple.

if you have a database of quotes, or simply even an array, then youd do something like this:


[code]
$quote_array['1'] = "Quote 1";
$quote_array['2'] = "Quote 2";
$quote_array['3'] = "Quote 3";
$quote_array['4'] = "Quote 4";



$Q_Number = rand(1, 4);
echo $quote_array[$Q_Number];

[/code]
mind you there might be a better way of doing it.  somewhere in there you would have to do some if's to check the time,  and then you would do something like

[code]

$latest_date = 1/1/07;

$today_date = date(d,m,y); # i think thats the right format

if(today_date != latest_date) {

#do the quote generator above

}
else {

echo $current quote;

}

[/code]

i decided that you should store the current quote decided from the array.... in a $current_quote variable, therefore its easier to read and use

GOOD LUCK
[/quote]

but I am unable to do the time check as he mentions.
Link to comment
Share on other sites

Try the following:

[code]
function randomquote()
{
    $quote_array = array(1 => "Quote 1", "Quote 2", "Quote 3", "Quote 4");

    // OR:

    $quote_array = array(1 => "Quote 1", 2 => "Quote 2", 3 => "Quote 3", 4 => "Quote 4");

    // (Both of the above result in the same thing, just one is less code, the other is easier to understand to those that have not written the code. Remember to repeat the array up to number 31 (so carry on from 5 up to 31)).
    $quote_number = date("d");
    echo $quote_array[$quote_number];
}

// Now call the function wherever you want the quote to be placed, as follows:

randomquote();
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.