Page 1 of 1

Random Quote - on a timer

Posted: Sun Jan 12, 2014 11:29 am
by chris98
I have already prepared the database, and created the following table with 100 quotes in:

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 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.

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>
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.

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
)); 

?>
Please help, I really don't know what to do about a timer?

Re: Random Quote - on a timer

Posted: Sun Jan 12, 2014 12:02 pm
by Celauran
Have you considered a cron job?

Re: Random Quote - on a timer

Posted: Sun Jan 12, 2014 12:09 pm
by chris98
Not really, but then again I don't know that much about them. Plus, if it's possible (and not too hard) I would rather just do it with PHP somehow. Is there a easy way with PHP?

Re: Random Quote - on a timer

Posted: Sun Jan 12, 2014 12:23 pm
by Celauran
They're not mutually exclusive; cron is the scheduler, which can then call a PHP script. PHP runs and then dies, so it really isn't the right tool for the job here.

Re: Random Quote - on a timer

Posted: Sun Jan 12, 2014 12:38 pm
by chris98
Hmm... I obviously can't use javascript because it would then mean that it could A.) Be tampered with, B.) Difficult to co-ordinate with the PHP (in my opinion), and C.) I'm not even sure if it would work, as the user might have to stay on the page for that length of time

The same problem could occur also using PHP. Would there be any way of including a file into it without having to reset the timer every time the page changed?