/    Sign up×
Community /Pin to ProfileBookmark

server changes something daily and keeps record

Is it possible to set something up so that on a website there is a spot for a quote of the day on every page and each day the server picks the next one out of a file? As it changes, I would like it to record the old quotes into another file and saves it for 15 days?

Example:

I have 2 files (they might not end up being .txt, but that’s what I’m going to use for the example): [U]quotelist.txt[/U] and [U]previous.txt[/U].

Text in the quotelist.txt file would be like:

[CODE]some quote -by person
another quote -by person
some more quote -by person
etc[/CODE]

and in the previous.txt file, there would be a list of the 15 most recent quotes. As the quote changes daily, the most recent quote would be added to the top of the list along with a date, and the now 16-day old quote would be deleted from the file.

I realize this may not be in the correct sub forum of server-side.

I have the [URL=http://ipower.com/webhosting_proplan.html]iPower Pro Plan[/URL] hosting package, so I believe it would be able to do it (if there is a script that can).

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@PineSolPirateJul 18.2006 — You can do that with php.

I've not worked with flat files much, but the basic idea would be to randomly pick a line in the quotes, and move it over to the "used" one. I would read the used one into an array, array_shift off the last quote and array_push on the new one, then write it to the file.

As for the "random" quote, I'm sure there is a better way, but again, you can just read the file into an array and pick a random index.

Hope my decidedly vaugue assistance helps you.
Copy linkTweet thisAlerts:
@Wiz_CreationsauthorJul 18.2006 — It doesn't have to pick a random quote; just one from the quotelist file. Whether it's the top one or any of the others doesn't matter.

I don't really know php... at all, so I can't write it. =/
Copy linkTweet thisAlerts:
@PineSolPirateJul 18.2006 — Are you wanting it to remove the quote from the quotes file once it uses it?
Copy linkTweet thisAlerts:
@PineSolPirateJul 18.2006 — This will read quotes (one per line) from a "quotes.txt" and pick a random one, then move it to the top of a file called "used.txt".[CODE]<?php
$quoteHandle = fopen("quotes.txt", "r");
$quoteString = file_get_contents($quoteHandle);
fclose($quoteHandle);
$quoteArray = explode("n",$quoteString);
$randomIndex = array_rand($quoteArray);
$usedHandle = fopen("used.txt", "r");
$usedString = file_get_contents($usedHandle);
fclose($usedHandle);
$usedArray = explode("n", $usedString);
array_pop($usedArray);
array_unshift($usedArray, $quoteArray[$randomIndex]);
$usedHandle = fopen("used.txt", "w");
foreach($usedArray as $quote)
{
fwrite($usedHandle, $quote."n");
}
fclose($usedHandle);
?>[/CODE]

I realize this code is awfully long, but I haven't done files stuff in forever. Maybe someone else out there can clean it up so there don't need to be three handles and all that stuff.
Copy linkTweet thisAlerts:
@MstrBobJul 18.2006 — PineSolPirate, your code will do this on [i]every page load[/i]. This should be rethought if you want it to happen once a day.

One solution would, I guess, be to store the day that a quote was moved to the used file. If it was more than a day ago since the last one, then move another over. Somehow, you need to keep track of the date.
Copy linkTweet thisAlerts:
@PineSolPirateJul 18.2006 — Well I didn't mean just stick that code on the main page and let the world at it. You would have that somewhere else, in an admin dir maybe, and just pull the top line off of used.txt for your main page. Set up a cron job, whatever.
Copy linkTweet thisAlerts:
@MstrBobJul 18.2006 — Well I didn't mean just stick that code on the main page and let the world at it. You would have that somewhere else, in an admin dir maybe, and just pull the top line off of used.txt for your main page. Set up a cron job, whatever.[/QUOTE]

A cron job would be the best way to go about this. Simplest solution.
Copy linkTweet thisAlerts:
@Wiz_CreationsauthorJul 19.2006 — I'm lost =(

Would the php that moves it from quotes.txt to used.txt also display it on the webpage? I don't know how to set up a cron job either. I guess I could just leave this out of the webpage, or make it easier by just having it show a random quote each time the page loads. [url=http://javascriptkit.com/script/script2/randomcontent.shtml]This script would work[/url]

It took a while for me to respond because somehow my subscription to the thread was deleted and I thought there weren't any responses.
Copy linkTweet thisAlerts:
@PineSolPirateJul 19.2006 — Well, you need shell access to the server. Plus it needs to be a unix/linux server. Then run "crontab -e" typing in "0 2 * * * /path/to/script.php" will run the script at "/path/to/script.php" at 2 A.M. every day. Assuming that get working you can use this code to display the latest quote.
[code=php]<?php
$usedHandle = fopen("used.txt", "r");
print fgets($usedHandle);
fclose($usedHandle);
?>[/code]

More cron info @ http://www.adminschoice.com/docs/crontab.htm
×

Success!

Help @Wiz_Creations spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...