dansk Posted January 2, 2007 Share Posted January 2, 2007 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 https://forums.phpfreaks.com/topic/32538-random-quote-every-day/ Share on other sites More sharing options...
fert Posted January 2, 2007 Share Posted January 2, 2007 you would need a cron Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-151293 Share on other sites More sharing options...
corbin Posted January 2, 2007 Share Posted January 2, 2007 You could make another file called quote2.txtIn 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 https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-151295 Share on other sites More sharing options...
dansk Posted January 2, 2007 Author Share Posted January 2, 2007 Thanks corbinI will try to code it myself and hopefully I will be able to get it. Otherwise, I will hunt you down with questions :)Thanks alot for the help Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-151306 Share on other sites More sharing options...
dansk Posted January 7, 2007 Author Share Posted January 7, 2007 Hi Corbin,I am a little bit lost here. Can you be more specific, I tried to think it over and I was a little bit confused. Do I need to use cookies work with the time?Thanks Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-154877 Share on other sites More sharing options...
corbin Posted January 7, 2007 Share Posted January 7, 2007 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 https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-154881 Share on other sites More sharing options...
PC Nerd Posted January 7, 2007 Share Posted January 7, 2007 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 formatif(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 useGOOD LUCK Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-154885 Share on other sites More sharing options...
DarkendSoul Posted January 7, 2007 Share Posted January 7, 2007 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 https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-154888 Share on other sites More sharing options...
corbin Posted January 7, 2007 Share Posted January 7, 2007 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 quoteif(!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 https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-154890 Share on other sites More sharing options...
dansk Posted January 7, 2007 Author Share Posted January 7, 2007 Thanks a lot for the help guys.here you go corbin, this is how my quote file looks likequote:hello world 1<br>commentry alkjdflajfauthor:author 1quote: hello world 2<br>commentry 355345435author: author 2quote: hello world 3<br>commentry *******author: author 3quote: hello world 4<br>commentry 3adddddddddauthor: author 4btw, 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 https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-154899 Share on other sites More sharing options...
PC Nerd Posted January 7, 2007 Share Posted January 7, 2007 the easiest way to do it, if thats the entire quotes file, is to just create an array, as i showed you above...... ;D Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-154901 Share on other sites More sharing options...
dansk Posted January 7, 2007 Author Share Posted January 7, 2007 okay, here's what i have when trying yoursbut 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 formatif($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 https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-154903 Share on other sites More sharing options...
corbin Posted January 7, 2007 Share Posted January 7, 2007 Oooohhhh my was wrong... I forgot to write the time to the time file lol... Hmmm doesn't look like you're using mine anyways though... Tell me if you want me to fix mine... Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-154911 Share on other sites More sharing options...
dansk Posted January 7, 2007 Author Share Posted January 7, 2007 sure corbin, I will let you know.You have always been a great help bro. Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-154914 Share on other sites More sharing options...
dansk Posted January 7, 2007 Author Share Posted January 7, 2007 Corbin, can you fix yours anyway. I will learn from it regardlessThanks Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-154916 Share on other sites More sharing options...
corbin Posted January 7, 2007 Share Posted January 7, 2007 Fixed in my previous post... Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-154921 Share on other sites More sharing options...
dansk Posted January 7, 2007 Author Share Posted January 7, 2007 it wrote to the time file one quote, but it seems to me that it still gets the stuff form the original quote files and changes them everytime the page reloads Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-154926 Share on other sites More sharing options...
dansk Posted January 8, 2007 Author Share Posted January 8, 2007 tried to use dark's and corbin's and neither one worked.,,help ???in dark's i need to do the time check and in corbin's nothing is happening Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-156048 Share on other sites More sharing options...
Clarkey_Boy Posted January 8, 2007 Share Posted January 8, 2007 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 https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-156120 Share on other sites More sharing options...
dansk Posted January 8, 2007 Author Share Posted January 8, 2007 Thank you, but the issue is basically how to get these random quotes show up only every 24 hours Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-156154 Share on other sites More sharing options...
Clarkey_Boy Posted January 8, 2007 Share Posted January 8, 2007 So put them in a database with a day of the month next to them (as in 1st, 2nd, 3rd etc, not Monday, Tuesday, Wednesday). Then select the quote with the day column the same as the current day.RC Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-156173 Share on other sites More sharing options...
dansk Posted January 8, 2007 Author Share Posted January 8, 2007 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 formatif(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 useGOOD LUCK[/quote]but I am unable to do the time check as he mentions. Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-156180 Share on other sites More sharing options...
Clarkey_Boy Posted January 8, 2007 Share Posted January 8, 2007 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 https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-156194 Share on other sites More sharing options...
dansk Posted January 9, 2007 Author Share Posted January 9, 2007 thanks Clarkey_Boy, this one works but i will save it when i can't get corbin to fix his :D:Dcorbin, i think you need to check the code because the if statement is kind of not working :D Link to comment https://forums.phpfreaks.com/topic/32538-random-quote-every-day/#findComment-156559 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.