Ich habe ganze jetzt soweit implementiert. name der ID spalte hab ich nun auch variabel gemacht.
PHP-Code:
function GetIDOfTheDay ($IDName, $tablename) {
if (TableExists ($tablename) ) {
//check if there is already a entry for $tablename
$sql = "SELECT COUNT(*) FROM RandomRows WHERE tablename='$tablename' LIMIT 1";
$result = mysql_query ($sql);
$row = mysql_fetch_row ($result);
if ($row[0] == 1 ) {
//check if the current random value is valid for today
$sql = "SELECT RandomID
FROM RandomRows
WHERE tablename='$tablename'
AND date=CURDATE()
LIMIT 1";
$result = mysql_query ($sql);
if (mysql_num_rows ($result) == 1) {
$row = mysql_fetch_array ($result);
return $row['RandomID'];
}
else {
//random value is out of date - update with a new random value
//generate new random value of the table
$sql = "SELECT $IDName
FROM $tablename
ORDER BY RAND()
LIMIT 1";
$result = mysql_query ($sql);
$row = mysql_fetch_array ($result);
$sql = "UPDATE RandomRows SET
RandomID='$row[$IDName]',
date=CURDATE()
WHERE tablename='$tablename'
LIMIT 1";
mysql_query ($sql);
}
}
else {
//no random value in RandomRow table
//generate a random value for the first time
$sql = "SELECT $IDName
FROM $tablename
ORDER BY RAND()
LIMIT 1";
$result = mysql_query ($sql);
$row = mysql_fetch_array ($result);
$sql = "INSERT INTO RandomRows
SET
RandomID='$row[$IDName]',
tablename = '$tablename',
date = CURDATE()";
mysql_query ($sql);
}
//select the new random value
$sql = "SELECT RandomID
FROM RandomRows
WHERE tablename='$tablename'
AND date=CURDATE()
LIMIT 1";
$result = mysql_query ($sql);
$row = mysql_fetch_array ($result);
return $row['RandomID']; }
else {
//table doesn't exist
return false;
}
}
Kommentar