Home › Forums › Back End › PHP Daily Quote Script › Re: PHP Daily Quote Script
January 8, 2011 at 12:18 pm
#67072
Member
I’d say, that you should use a sql database for it – either SQLite or MySQL. MySQL is a bit overkill, but it is easier to work with for newcomers (at least, that is my experience).
The text file you are using is not a relational database, and while you could accomplish the same thing with text files, it would be a waste of time and resources.
I’ll give you a little example. This uses mysql and php.
Bear with me if it fails. I have not tested it – I just wrote it off the top of my head. :-)
The mysql table:
CREATE TABLE `quotes` (
`id` int(3) not null auto_increment,
`quote` text,
`person` text,
`lastused` date default '2010-10-10',
PRIMARY KEY (`id`)
)
The PHP code:
// Connect to Mysql Database
// Checking if theres already selected a quote for today
$quoteQuery = mysql_query('SELECT quote,person FROM quotes WHERE lastused = CURDATE()');
// If not:
if (mysql_num_rows($quoteQuery) != 1) {
// Get all quotes from db which havent been used in the last 7 days
$idQuery = mysql_query('SELECT id FROM quotes WHERE DATEDIFF(CURDATE(),lastused) > 7');
// Getting a numerated array only
$ids = mysql_fetch_array($idQuery, MYSQL_NUM);
// Count the number of elements in that array
$idCount = count($ids);
// Getting a random number - $ids[$id] will now contain our random number.
$id = rand(0,$idCount);
// Update that row
mysql_query('UPDATE quotes SET lastused = CURDATE() WHERE id = '.$ids[$id]);
// Getting the newly selected daily quote
$quoteQuery = mysql_query('SELECT quote,person FROM quotes WHERE lastused = CURDATE()');
}
$row = mysql_fetch_row($quoteQuery);
?>
=$row[0]?>
=$row[1]?>