ok. I am in the process of creating a web based game. I have a page that randomly selects a monster from a database that is the users level. but when you hit attack it randomly selects a new monster. please help me so it will randomly select a monster once instead of every time you submit it.
here is the code for the page.
<?php
include("/../../wamp/www/test/includes/db_connect.php");
include("/../../wamp/www/test/includes/userauth.php");
$sql = "SELECT * FROM monsters WHERE mon_level = $userlevel ORDER BY RAND() LIMIT 1";
$query = mysql_query($sql) or die(msql_error());
$mon = mysql_fetch_array($query);
if(isset($_POST['fight'])) {
if($userhp >> 0) {
echo "You encounter a ".$mon['mon_name'].".";
echo "<br>What would you like to do?";
echo '<br><form action="'.$_SERVER['PHP_SELF'].'" method="post">
<input type="submit" name="attack" value="Attack">
<input type="submit" name="run" value="Run">
</form>';
exit;
}// if userhp > 0
else {
echo "You are dead. You can not attack anything.";
exit;
}// else
}// if post is fight
if(isset($_POST['attack'])) {
$monhp = $mon['mon_hp'];
$monname = $mon['mon_name'];
$monattack = $mon['mon_dam'];
$dammin = $monattack - 2;
$dammax = $monattack + 2;
$mondam = rand($dammin,$dammax);
echo 'The '.$mon['mon_name'].' has '.$monhp.' health.
<br>You have '.$userhp.' health.
<br><br>The monster hits you for '.$mondam.'.';
$userhp = $userhp - $mondam;
if($userhp <= 0) {
$userhp = 0;
}
$sql = "UPDATE users set user_hp = '".$userhp."' WHERE user_name = '".$username."'";
mysql_query($sql) or die(mysql_error());
echo '<br>You have '.$userhp.' health left.';
if($userhp <= 0){
echo '<br><br>You have died and must go back to town to heal.';
exit;
}
echo '<br><form action="'.$_SERVER['PHP_SELF'].'" method="post">
<input type="submit" name="attack" value="Attack">
<input type="submit" name="run" value="Run">
</form>';
exit;
}
if($userhp >> 0) {
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">
<input type="submit" name="fight" value="Find something to fight">';
exit;
}// if hp is not 0
else {
echo "You are dead. You can not attack anything.";
exit;
}// else
?>
all the user variables are set on the user auth page.
i have tried to use a $_session[] but i did not have luck with this. i may have not done it right.
any help would be appreciated.