This is what I am trying to accomplish -
Display a "Quote of the Day" feature. I do not want it to display a random quote from the database everytime the page is displayed - I want one quote displayed there all day long. So I am trying to construct a script that will be run via crontab one a month on the first.
I have everything working now, but I am expriencing a problem I was not expecting- the random values are repeating themselves. When the script runs, I get duplicate values.
I searched PHP.net, but the only function that looks like it would work is array_unique(). Problem is, I am not sure how on earth to implement it correctly - does anyone have any suggestios? this is my code -
--> NOTE: DOM = Day of Month
<?php
//includes are here.
//Connect to Database
$dbConection = new DbConnect;
//Reset the Quote of the Day Order
$resetQuery = "UPDATE quotes SET dom = '0'";
mysql_query($resetQuery);
//Randomize it
$getQuotesQuery = "SELECT id FROM quotes";
$getQuotesResults = mysql_query($getQuotesQuery);
WHILE($row = mysql_fetch_array($getQuotesResults))
{
$dbArray[] = $row['id'];
}
FOR($i = 1; $i < count($dbArray); $i++)
{
$randArray = array_rand($dbArray, 21);
$num = ($i - 1);
$randNum = $randArray[$num];
$updateQuery = "UPDATE quotes SET dom = '$randNum' WHERE id = '$i'";
IF(mysql_query($updateQuery))
{
PRINT "Success<br>Query:<br>$updateQuery<br><br>";
}
ELSE
{
PRINT "Failure<br>";
echo mysql_error();
PRINT "<br> Query is:<br>$updateQuery";
}
}//End For Loop
PRINT "<br>Database Array Containing ID's:<br>";
print_r($dbArray);
PRINT "<br>Random Array Containing Random DOMS:<br>";
print_r($randArray);
?>