下面的程序就是这样做的!
<?php
/************************************
AIM: To retrieve and echo a random item from a database, and continue to echo the same item whenever and whereever the page is reloaded, for five seconds. After the five seconds is up, the program must echo another random item, and contine ad infinitum.
CONDITIONS: The program cannot echo two results in a row. (In effect the same result for ten seconds).
*************************************/
// Get ready to seed a random value from future 'rand()' function. Seed for 5 seconds.
srand(floor(time() / (5)));
// Retreieve all rows from table 'dogs'.
$result1 = $mysqli->query = new mysqli("SELECT * FROM dogs", MYSQLI_USE_RESULT);
// See how many rows are in 'dogs'.
mysqli_result::$num_rows;
// Generate a random value between 1 and the number of rows in 'dogs'.
echo $random = rand(1,$num_rows) . '<br>';
// Find the current date as a unix timestamp.
echo $currentdate = time() . '<br>';
// Find the time ten seconds ago.
echo $datetensecondsago = $currentdate - 10;
echo '<br>';
?>
但是,我不知道如何让它不连续两次回显相同的随机数据库项目!
数据库原理图如下:
Table Name = dogs
id | name | lastused
------------------------
1 | Rover | 1362960167
2 | Chip | 1362960123
3 | Rex | 1362960178
我认为答案在于 mysql 查询,例如
SELECT * FROM dogs WHERE dateused<$datetensecondsago LIMIT 1
然后更新使用过的物品的使用时间,但我无法解决!
感谢帮助!