次のプログラムはまさにこれを行います!
<?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>';
?>
ただし、同じランダムデータベースアイテムを2回続けてエコーしないようにする方法がわかりません!
データベースの図は次のとおりです。
Table Name = dogs
id | name | lastused
------------------------
1 | Rover | 1362960167
2 | Chip | 1362960123
3 | Rex | 1362960178
答えは次のようなmysqlクエリにあると思います
SELECT * FROM dogs WHERE dateused<$datetensecondsago LIMIT 1
中古品の使用時間を更新しましたが、うまくいきません!
助けてくれてありがとう!