Reputation: 539
I am building a simple app with HTML5 and PHP and I need to show a different quote every day. So I have a database table with all the 365 quotes and authors in it, but I can't seem to find a way to show a different database record every 24 hours. I am fairly new to programming and searched the whole internet for an answer but couldn't find anything similar to this.
So I have my html and php code to connect to the database and show the latest record. How can I show the user another database row every 24 hours?
This is what my code looks like now:
<?php
$con=mysql_connect('localhost','xxxx','xxxx');
if(!$con)
{
die ("Failed to connect: " . mysql_error() ) ;
}
mysql_select_db("xxxx",$con);
$sql = "SELECT * FROM quotes LIMIT 1" ;
$myData=mysql_query($sql,$con) ;
while($record = mysql_fetch_array($myData)) {
echo "<h1>" . $record['quote'] . "</h1>";
echo "<br><p> - " . $record['author'] . " - </p>";
}
mysql_close($con);
?>
Thanks in advance guys!
Upvotes: 2
Views: 944
Reputation: 10381
You can refresh the page every 86400 seconds (seconds in a day), when the page automatically reloads use PHP to get current date and use the date to display thought of the day.
Next example refreshes page every 3 seconds, get current date with PHP and display the thought of the day (I only added 3 thoughts for today 5/29, tomorrow 5/30 and after tomorrow 5/31). Create a text file in your www directory, name it "refresh_every_24h.php" and run it from you browser :
<?php
$database = array( "5/29" => "Jon Skeet : «I'm the king!»" ,
"5/30" => "Jester : «I know more assembly than you»" ,
"5/31" => "Tómax : «I'm here to make points, baby»" );
?>
<!--
NEXT "META" REFRESHES PAGE EVERY 3 SECONDS, YOU CHANGE IT BY 86400 (SECONDS PER DAY).
-->
<meta http-equiv="refresh" content="3;URL=http://localhost:8099/refresh_every_24h.php" />
<html>
<head>
<title></title>
</head>
<body>
Thought of the day:
<br/>
<?php
$arr = getdate( time() ); // GET CURRENT SYSTEM TIME.
echo $database[ strval( $arr[ "mon" ] ) . // USE MONTH AND
"/" . // DAY TO FIND
strval( $arr[ "mday" ] ) // THOUGHT OF THE DAY.
];
?>
</body>
</html>
To change the filename for any other just take care of changing it in the next line, it's configured to run on MY localhost, you change the host too for your own:
<meta http-equiv="refresh" content="3;URL=http://localhost:8099/refresh_every_24h.php" />
Don't forget to change the date of your computer while the page is refreshing, to see how the thought of the day changes! (remember, with this example you can only use date of today, tomorrow and after tomorrow).
Upvotes: 2
Reputation: 163
Add a column to your table named e.g. id
. Iterate through all the rows in the table and add values from 1 to 365. Then you can do:
$sql = "SELECT * FROM quotes WHERE id = " . date('d');
Upvotes: 4