Stalingrad Posted July 17, 2010 Share Posted July 17, 2010 Hi! I'm having a problem with setting a random variable after I select a random row from a table. I have no problem with selecting the random row. I just can't get the variable to stay as it was selected. Each time I re-visit the page, it changes to the newly randomly selected variable. I want the variable to stay the same until I press the submit button. Here is my code: <?php require("global.php"); loggedin(); echo "<font size=6>Quests</font><br><br>"; $action = $_GET[act]; $reward = rand(100, 500); $gettheitem = mysql_query("SELECT id FROM items ORDER BY RAND() LIMIT 1"); $qnquery = "SELECT * FROM quests WHERE userid='$id'"; $qnresult = mysql_query($qnquery); while($qnrow = mysql_fetch_array($qnresult)) { $checkstatus1 = $qnrow['qon']; } $squery = "SELECT * FROM user WHERE id='$id'"; $sresult = mysql_query($squery); while($srow = mysql_fetch_array($sresult)) { $cquests = $srow['questscore']; $fquests = $srow['questsfailed']; } $randitemq = mysql_query("SELECT * FROM items WHERE id!='0'"); $crandit = mysql_num_rows($randitemq); $randit = rand(1, $crandit); $getitem = mysql_fetch_row(mysql_query("SELECT * FROM items WHERE id='$randit '")); $randomitem1 = "SELECT * FROM items ORDER BY rand() LIMIT 1"; $resultt = mysql_query($randomitem1); while($iroww = mysql_fetch_array($resultt)) { $itemimagee = $iroww['image']; $itemname1 = $iroww['name']; $grabitem1 = $iroww['id']; } $grabq = "SELECT * FROM items WHERE id!=0"; $grabresult = mysql_query($grabq); while($grow = mysql_fetch_array($grabresult)){ $itemimage = $grow['image']; $itemname = $grow['name']; $grabitem = $grow['id']; } if($checkstatus1 == "No") { echo "Hey There! Welcome to the Quest page! Here you can complete jobs for the workers of SadHazel. You have unlimited time per-quest. The reward will be in coins, and it's a random amount. You never know how much you'll get until you complete the quest!<br><br>"; ?> <html><form action="<?php echo "$PHP_SELF"; ?>" method="get"> <input type="submit" name="act" value="start"><br><br></html> <?php echo "You have currently completed <b>$cquests</a> Quests!<br>However... you have failed <b>$fquests</b> Quests"; if($action == "start") { mysql_query("UPDATE quests SET itemid = '$grabitem1', `qon` = 'Yes' where userid = '$id' "); echo "Hey there again! I'd like you to bring me this item, please: <br><br><img src=/items/$itemimagee><br><b>$itemname1</b><br><br>"; } } if($checkstatus1 == "Yes") { echo "Hey there yet again! I'll remind you what item I need: <br><br><img src=items/$itemimagee><br><b>$itemname1</b><br><br>"; ?> <html><form action="<?php echo "$PHP_SELF"; ?>" method="get"> <input type="submit" name="act" value="have"><br><br></form></html> <html><form action="<?php echo "$PHP_SELF"; ?>" method="get"> <input type="submit" name="act" value="quit"><br><br></form></html> <? if($action == "quit") { mysql_query("UPDATE quests SET itemid = '0', `qon` = 'No' where userid = '$id' "); mysql_query("UPDATE user SET questsfailed=$fquests +1 WHERE id='$id'"); echo "Oh no! It looks like you've given up!"; } if($action == "have") { $hiquery = "SELECT * FROM user_items WHERE owner='$YourName' AND itemid='$grabitem1'"; $haveit = mysql_query($hiquery) or die(mysql_error()); $haveitornot = mysql_num_rows($haveit); $theitem = mysql_query("SELECT itemid FROM user_items WHERE owner='$YourName'"); if($haveitornot != "0") { mysql_query("UPDATE user SET questscore=$cquests +1 WHERE id= '$id'"); mysql_query ("UPDATE user SET point=$points +$reward WHERE id='$id'"); mysql_query("UPDATE quests SET itemid = '0', `qon` = 'No' where userid = '$id' "); mysql_query("DELETE FROM user_items WHERE itemid='$grabitem1' AND owner='$YourName' LIMIT 1"); echo "<font color=green>Success! You have completed this quest.</font><br><br>To thank you, I'll give you <b>$reward</b> Coins!<br><br>"; } if($haveitornot == "0") { echo "<font color=red>Error! You don't have the item!</font><br><br>"; } } } ?> Please Note: I have some variables there taht aren't being used. They are simply there just in case I mess up... I don't want to lose variables that previously worked. Any help is appreciated... thank you in advance! =] Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 17, 2010 Share Posted July 17, 2010 Store it in a SESSION var, then check for its presence before allowing the query to run and select another. As a side note, google "why mysql ORDER BY RAND() is bad" and have a look through the results. Quote Link to comment Share on other sites More sharing options...
Stalingrad Posted July 17, 2010 Author Share Posted July 17, 2010 How do I set a session variable? Also... is there any other way to do it if the rand() code is bad? Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.