Random Quote - on a timer
Posted: Sun Jan 12, 2014 11:29 am
I have already prepared the database, and created the following table with 100 quotes in:
I also have created a second table with the quote in that is the current one called current_quote. This database includes the id of the quote, plus the day. month, year, hour, minute and second it was added.
I have the SQL correct, and it will randomly select a row.
But how could I make it change the quote randomly every "x" hours? - I have created a test page which will change the quote every time it's reloaded, but I want it to do it automatically, and I want this page to only be a temporary measure until I get the timer working.
Please help, I really don't know what to do about a timer?
Code: Select all
CREATE TABLE `quotes` (
`id` int(10) NOT NULL auto_increment,
`quote` text NOT NULL,
`unit` varchar(50) NOT NULL,
`game` varchar(50) default NULL,
PRIMARY KEY (`id`)
);
I have the SQL correct, and it will randomly select a row.
Code: Select all
<div class="margin_bottom_20 horizontal_divider"></div>
<div class="header_01">Random Quote:</div>
<?php
// Retrieve it from the table
$getquote = "SELECT quotes.quote, quotes.unit, quotes.game
FROM quotes
INNER JOIN current_quote ON quotes.id = current_quote.id LIMIT 1";
$ps = $shn_conf->query($getquote);
foreach ($ps as $row)
{
?>
<div class="quote"><?php echo $row['quote']; ?></div>
<p class="unit"><?php echo $row['unit']; ?></p>
<p class="game"><?php echo $row['game']; ?></p>
<?php } ?>
<div class="margin_bottom_20 horizontal_divider"></div>
Code: Select all
<?php require_once('include/include.php');
// Delete everything from the database
$sql = "DELETE FROM `current_quote`";
$stmt = $shn_conf->prepare($sql);
$stmt->execute();
// Get a new quote
$ranquote = "SELECT `id` FROM `quotes` ORDER BY RAND() LIMIT 1";
$quotes = $shn_conf->query($ranquote);
foreach ($quotes as $row)
{
$id = $row['id'];
}
$date = date('Y-m-d H:i:s');
// Insert the new quote in the database
$insertquote = "INSERT INTO `current_quote` (id,loaded_on) VALUES (:id,:date)";
$ps = $shn_conf->prepare($insertquote);
$ps->execute(array(
':id'=>$id,
':date'=>$date
));
?>