Cetanu Posted August 25, 2009 Share Posted August 25, 2009 Well, I am working on an RPG script right now and I wanted this: Member searches for enemy - Finds enemy- Different enemies have different health, attack, etc - If a member damages an enemy I want to update the enemy's stats so that it reflects the damage, but I do not want to use a database or anything. I am currently just echoing the enemies stats like <strong>Enemy:</strong> <strong>Attack:</strong> Etcetera. But how could I reload the page with the random enemy (I'm using mt_rand() ) still selected and change its health to reflect damage. I don't know if this makes sense. :'( Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/ Share on other sites More sharing options...
Bendude14 Posted August 25, 2009 Share Posted August 25, 2009 can you pass the random enemy's ID via $_GET and maybe the damage as well... something like update.php?enemy=321&damage=65 Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-905516 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 I could, but could people modify that? Also, I am reeeeeally bad at $_GET. Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-905526 Share on other sites More sharing options...
Batosi Posted August 25, 2009 Share Posted August 25, 2009 Umm yea dont use get or post for that, umm if you dont want to do database your best bet is use $_SESSION Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-905529 Share on other sites More sharing options...
Bendude14 Posted August 25, 2009 Share Posted August 25, 2009 yes they could modify it. If you dont want them to access it use a form and POST the data <input type="hidden" name="damage" value="65" /> Then access it like this $_POST['damage']; Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-905530 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 Umm yea dont use get or post for that, umm if you dont want to do database your best bet is use $_SESSION How would I use SESSION to reflect damage and health decrements? yes they could modify it. If you dont want them to access it use a form and POST the data <input type="hidden" name="damage" value="65" /> Then access it like this $_POST['damage']; I would have to have a Submit button for this, right? I think it would look awkward having a little "Confirm Damage" button or something. Could I utilize JS for this at all? Maybe....when they click the ok button on an alert popup then something happens? I don't know. This is making me go insane. Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-905540 Share on other sites More sharing options...
Bendude14 Posted August 25, 2009 Share Posted August 25, 2009 yer by all means use javascript to subimt the form its something like form[0].submit but dont take my word for it. a quick google should show u the answer Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-905817 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 Great now I've got to learn JS too. Anyone have other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-905997 Share on other sites More sharing options...
roopurt18 Posted August 25, 2009 Share Posted August 25, 2009 Also, I am reeeeeally bad at $_GET. If you're bad at $_GET (and I'm not even sure what that means), then you're in a world of hurt. You add values to the URL after a ? in the form of param=value and separate them with an ampersand. Then you access them in your page with $_GET. What's so hard about that? yes they could modify it. If you dont want them to access it use a form and POST the data <input type="hidden" name="damage" value="65" /> That can be modified just as easily as GET. Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906055 Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 As roopurt said, if you are afraid of the user modifying the information, then both POST and GET would probably be a bad idea as the user could easily modify both if they have the know how. I would suggest using javascript to make it much more dynamic, so the user doesn't have to refresh the page everytime he does one attack. However, judging from the fact that you don't know javascript, and you have trouble with get variables, this may be a daunting task. I would suggest trying a smaller project to get the fundamentals of PHP down before you try tackling an RPG, but if you must make an RPG, I would strongly suggest some sort of Javascript/Ajax attack system. but I guess you could always use sessions too... just create session vars for the health and damage or whatever else you need, and whenever you preform an attack, decrement the value of the session variable Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906058 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 I understand the URL format for $_GET but what I don't understand how to use $_GET. I don't know why but I can't wrap my mind around it. Someone once gave me this code: <?php if(!array_key_exists , 'page'){ //A WHOLE BUNCH OF $_GET STUFF } ?> (Or something along those lines) And I had no idea how to make $_GET to work...I just used the code and it worked. I do not know why I can't understand it. I could do it with forms and understand it, but not with other things. @mikesta Well I get other things about PHP, just not $_GET and I never needed to teach myself JS so I didn't. If I need it now I will learn it. I will also try and learn more about $_GET before moving on, but I probably will have issues with it like I did when I started PHP. So, with sessions, I could start a session when they find an enemy, and then what? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906060 Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 create a few session vars, for whatever data you need to keep and update. like #after you get the monster stuff $_SESSION['mon_health'] = $monster_health; $_SESSION['mon_name'] = $monster_name; #etc. as far as get goes, its mad ease. say I have a URL that goes www.mysite.com/food.php?id=tacobell id is the get variable, and tacobell is the value of said get variable. To access the variable, on food.php you write something like $fastfood = $_GET['id']; echo "You are eating at " . $fastfood; Usually to pass get variables to a page you have to create a hyperlink, and write it as the URL i wrote above, so for example, If I made a link that looked like <a href="food.php?id=tacobell">Taco Bell</a> the food.php page would look like: You are eating at tacobell. if I changed the URL to say food.php?id=Mcdonalds the page would then say You are eating at Mcdonalds Make sense? Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906071 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 Yes, that makes a lot of sense to me. I think what's confusing me is where you are getting the ['id'] from.... It would be defined on the page, right, but how? Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906108 Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 Nope, its defined in the URL itself. So when you go to the page: www.mysite.com/food.php?id=tacobell a get variable with the key 'id' is created. similarly if you went to www.mysite.com/food.php?socks=tacobell a get variable named socks would be created, which could be accessed the way I posted above. You can think of the $_GET global as an array, much like the following $arr = new Array(); $arr['my'] = "array"; $arr['is'] = "awesome"; And when you go to a URL that is like mysite.com/food.php?id=tacobell its as if you wrote the PHP $_GET['id'] = "tacobell"; Make more sense? As you see, since you define the variables in a URL, any user can theoretically create or alter your get variables, so its best not to use them for passing sensitive data to the script, and you should always sanitize get variables before you do anything with them. Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906118 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOH. *has a sudden realization* I $_GET IT! THANKS. Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906121 Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 haha nice pun. Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906124 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 Now I understand $_GET more, anyone have some ideas? I am into JS, just a beginner now. Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906126 Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 well, if you are just beginning I would hold off until you understand the fundamentals of the HTML DOM, and some Ajax functionality (That doesn't mean don't learn JavaScript. JavaScript is awesome. So dam awesome). However, as for your problem, I would suggest using sessions for now. You can always change things at a later date. Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906131 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 $_SESSIONs right. I'll try it. Ajax Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906138 Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 Ajax stands for Asynchronized JavaScript and XML btw. Its basically just Javascript on steroids though. Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906151 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 Oooh. Okay. I am using $_SESSION, but I get the white screen of doom. If I already have a session running for usernames, etc. do I need to cancel it before starting a new one? Also, can I just add in the new $_SESSION variables to the existing session? Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906163 Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 yeah you just need to create a new session. No need to destroy any other sessions. Make sure you have session_start() at the beginning of your page though. creating a new session is pretty straight forward, just like $_SESSION['newSess'] = "New Value"; Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906167 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 Yeah, that's what I did. My code must have an error, then. <?php //there is other stuff before this so the $row thing IS right. =) while($row=mysql_fetch_array($result)){ echo "<strong>".$row['name']."</strong><br/> <strong>".$row['species']."</strong><br/> <strong> Health: ".$row['health']."</strong><br/> <strong> Attack: ".$row['attack']."</strong><br/> <strong> Defense: ".$row['defense']."</strong><br/> <strong>"; if($row['species']=="Alien"){ echo "Stealth: ".$row['stealth']; } if($row['species']=="Marine"){ echo "Melee: ".$row['melee']; } echo "</strong> <strong>Money: ".$row['money']."</strong>"; echo "</td><td>"; if(!$_POST['choose']){ echo "Choose an Enemy to Fight <form action='fcenter.php?page=planet1' method='post'> <select size='4' name='fight'> <option value='Aliens'>Aliens</option> <option value='Marines'>Marines</option> <option value='Native Animals'>Native Species</option> <option value='Totally Random'>Randomize</option> </select> <input type='submit' name='choose' value='Search'/> </form>"; } if($_POST['choose']){ if($_POST['fight']=="Aliens"){ $randA = mt_rand(1,20); if($randA=="1" || $randA=="7" || $randA=="2" || $randA=="20"){ echo "You have not found an enemy to fight. Please try again."; } if($randA=="3" || $randA=="6" || $randA=="11" || $randA=="8" || $randA=="16"){ // HERE ARE MY NEW SESSIONS $_SESSION['attackDrone']=25; $_SESSION['defenseDrone']=20; $_SESSION['healthDrone']=20; echo "<strong>Enemy:</strong> Drone Alien<br/> <strong>Attack:</strong> ".$_SESSION['attackDrone']."<br/>" <strong>Defense:</strong> ".$_SESSION['defenseDrone']."<br/>" <strong>Health:</strong> ".$_SESSION['healthDrone']."<br/>" <strong>Money for Kill:</strong> 100<br/> <strong>Experience:</strong> 10<br/>"; if(!$_POST['confirm']){ echo "<form action='fcenter.php?page=planet1' method='post'> <input type='radio' name='attack' value='Attack'/>Attack<br/> <input type='radio' name='attack' value='Flee'/> Flee <br/> <input type='submit' name='confirm' value='Choose'/> </form> "; } if($_POST['confirm']){ if($_POST['attack']=="Attack"){ $i = $row['attack']; if(intval($i) > 20){ $dam = $row['attack']-$_SESSION['defenseDrone']; $_SESSION['healthDrone']=$_SESSION['healthDrone']-$dam; echo "Testing HellO! "; } if($i<20){ echo "Hi"; } } if($_POST['attack']=="Flee"){ echo "Hi"; } } } } } } echo "</td></tr></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906168 Share on other sites More sharing options...
Cetanu Posted August 26, 2009 Author Share Posted August 26, 2009 I fixed my fatal error!!!! Now, I have a new problem: <?php if($_POST['confirm']){ if($_POST['attack']=="Attack"){ $i = $row['attack']; if(intval($i) > 20){ $dam = $row['attack']-$_SESSION['defenseDrone']; $_SESSION['healthDrone']=$_SESSION['healthDrone']-$dam; echo "Testing HellO! "; } if($i<20){ echo "Hi"; } } ?> When this part executes it does NOTHING. It just redirects me to the page I'm on.... Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906385 Share on other sites More sharing options...
Cetanu Posted August 26, 2009 Author Share Posted August 26, 2009 Would an improper form do that? <?php if(!$_POST['confirm']){ echo "<form action='fcenter.php?page=planet1' method='post'> <input type='radio' name='attack' value='Attack'/>Attack<br/> <input type='radio' name='attack' value='Flee'/> Flee <br/> <input type='submit' name='confirm' value='Choose'/> </form> "; } if(isset($_POST['confirm'])){ if($_POST['attack']=="Attack"){ $i = $row['attack']; if(intval($i) > 20){ $dam = $row['attack']-$_SESSION['defenseDrone']; $_SESSION['healthDrone']=$_SESSION['healthDrone']-$dam; echo "Testing HellO! ".$_SESSION['healthDrone']; } if(intval($i) < 20){ echo "Hi"; } } else if($_POST['attack']=="Flee"){ echo "Hi"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/171729-confusing-question/#findComment-906409 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.