Show different database records every 24 hours

I am creating a simple application with HTML5 and PHP and I need to show different quotes every day. So I have a database table with all 365 citations and authors in it, but I can't find a way to show another database record every 24 hours. I am new to programming and have searched all over the internet for an answer but found nothing like this.

So I have html and php code to connect to the database and display the latest record. How can I show the user the following database row every 24 hours?

This is what my code looks like:

<?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!

+3


source to share


2 answers


Add a column to your table named eg. id

... Go 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');

+3


source


You can refresh the page every 86400 seconds (seconds per day) when the page automatically reloads using PHP to get the current date and uses the date to display the thought of the day.

The following example refreshes the page every 3 seconds, gets the current date from PHP and displays 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 your 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 another, just take care of changing it on the next line, it is configured to run on MY localhost, you also change the host for yours:

<meta http-equiv="refresh" content="3;URL=http://localhost:8099/refresh_every_24h.php" />

      

Don't forget to change your computer's date while the page refreshes to see how the thought of the day changes! (remember that in this example you can only use the date today, tomorrow and the day after tomorrow).

+2


source







All Articles