Reputation: 13535
i have a table that have too many text record . ( For example 1000
record )
my table structure look like below :
ID | Text
---------------------------------------
1 | My Test Text 1
2 | My Test Text 2
i want show a record per day, have any idea about this ?
thanks
Upvotes: 0
Views: 240
Reputation:
RAND() function works in a defined way so you can use a seed to generate a random selection on the table. For the purpose you can use the Current date as seed and the query will be look like SELECT Text From Table_Name ORDER BY RAND(CURDATE()) LIMIT 1
Upvotes: 2
Reputation: 21979
because you don't have date in it, my suggestion is to use text file to store what ID is shown yesterday (some UNTESTED quick code to get you started):
<?php
$tempfile = '/path/to/file/yesterday_id.txt';
list($yesterday_id, $yesterday_date) = explode("\t", file_get_contents( $tempfile ));
$today = date('Y-m-d');
if( $yesterday_date <> $today)
{
$yesterday_id++;
}
$sql = "SELECT * FROM sometable WHERE id = '$yesterday_id'";
$rs = mysql_query( $sql );
if( $r = mysql_fetch_assoc($rs))
{
echo $r['text'];
file_put_contents($tempfile, "{$yesterday_id}\t{$today}");
}
else
{
echo "text not found.";
}
?>
Upvotes: 0
Reputation: 1908
order by rand with a seed of todays date ORDER BY RAND(CURDATE())
or $date = date('Y-m-d') ;
and then use ...ORDER BY RAND($date)
Upvotes: 3
Reputation: 3165
You could write (or find) a function that would generate a pseudo-random integer (some type of very high-collision hash) based on the date. Use that to SELECT Text WHERE ID = ?
. That way you don't have to store any additional data. Of course, you would have to make the integer bound in the range of n to m, where n is your lowest ID and m is your highest.
Upvotes: 0
Reputation: 43219
Your only option is to sort randomly and limit your results to 1, as you have no date column.
SELECT text FROM yourTable ORDER BY RAND() LIMIT 1
(assuming MySQL)
Upvotes: 0
Reputation: 77956
SELECT * FROM my_table ORDER BY RAND() LIMIT 1
That will fetch a random record. Cache it with PHP with a 24 hour lifespan. Every time this page is loaded, PHP should check for the cache file and the expire time. If the cache is expired, make the new query, re-cache. Rinse, repeat.
Upvotes: 2
Reputation: 2561
for that you need to add column in table to store date on which data has been inserted.
and then you can always write query to get data as per date..
Upvotes: 0