Ok here's the code I have so far, with a great big comment block where I need to work out the date issue.
Basically what this code does is generates three random numbers between 0 and 20 and assigns each number a picture. It then needs to check whether it's been six months since the user last used it by looking at the date in $last_major_draw which it pulls from their profile on the database. Then, if it has been six months, then it gives them three cards, adds them to their profile on the database, and sets $last_major_draw to now.
Problem is, I don't know if I'm even using the right format for the date in $now, and if I am, how I can get it to work out if they last used it over six months ago.
<?
include 'db.php';
//GetImg Function
function getImgForNumber($dir, $i) {
if (!is_numeric($i) || !is_integer($i)) {
return;
}
$tag = "<img src=\"" . $dir . "/" . $i . ".jpg\" >";
return $tag;
}
//Random Array Function
function RandomArray($min,$max,$num) {
$ret = array();
while (count($ret) <$num) {
do {
$a = rand($min,$max);
} while (in_array($a,$ret));
$ret[] = $a;
}
return($ret);
}
$character = $_POST['character'];
$pass = $_POST['pass'];
$character = htmlentities($character, ENT_QUOTES);
$password = htmlentities($password, ENT_QUOTES);
if((!$character) || (!$password))
{
PRINT "Please go back and fill in both fields.";
exit;
}
$result = mysql_query("SELECT * FROM characters WHERE name = '$character'");
$num_rows = mysql_num_rows($result);
if ($num_rows < 1)
{
PRINT "The character you requested does not exist on the database. <br> Please go back and try again or <a href='http://www.visionaryentertainment.com/~onlinegaming/php/add_character_form.php'>click here to create a character.</a>";
exit;
}
$row = mysql_fetch_row($result);
$name = $row['name'];
$db_password = $row['password'];
$major1 = $row['major1'];
$major2 = = $row['major2'];
$major3 = $row['major3'];
$last_major_draw = = $row['last_major_draw'];
if ($pass != db_password){
PRINT "The password you entered does not match the one on our database <br> Please go back and try again.";
exit;
}
//HERE'S WHERE I'M GOING TO NEED SOMETHING THAT LOOKS AT THE $LAST_MAJOR_DRAW VARIABLE, LOOKS AT TODAY'S DATE AND DECIDES WHETHER IT'S BEEN SIX MONTHS SINCE THE LAST MAJOR DRAW. I'D ALSO LIKE SOMETHING THAT CAN ADD SIX MONTHS TO $LAST_MAJOR_DRAW AND PUT IT IN THE VARIABLE $NEXT_MAJOR_DRAW
if ($last_major_draw < $six_months_ago)
{
PRINT "It has been less than six months since your last Major Arcana Draw <br> The next time you will be able to draw Major Arcana will be on $next_major_draw";
} else {
$roll_array = RandomArray(0, 20, 3);
$major1 = $roll_array[0] . ".jpg";
$major2 = $roll_array[1] . ".jpg";
$major3 = $roll_array[2] . ".jpg";
$date = getdate()
$now = $date['mon']."-".$date['mday']."-".$date['year'];
$update = mysql_query("UPDATE `characters` SET major1 = '$major1', major2 = '$major2', major3 = '$major3', last_major_draw = '$now' WHERE name = '$character'") or die (mysql_error());
if (!$update){
PRINT "There was a problem updating your character.<br>Please contact the Guides at NewLondonGuides@hotmail.com."
} else {
echo "<p align=center><table border=3><tr>";
foreach($roll_array as $rolled) {
echo "<td>" . getImgForNumber("http://www.visionaryentertainment.com/~onlinegaming/php/cards/major", $rolled) ."</td>";
}
echo "</tr></table>";
}
}
?>