cloudll Posted August 17, 2011 Share Posted August 17, 2011 Im using this code at the moment, and Im trying to make it include my battleon.php only if both conditions are met, but at the moment it includes the page when the first condition (randbattle) is met. if (($randbattle == "1") || ($enemy_hp == "0")) { include ('battleon.php'); } Can anyone tell me why it isnt working ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/245051-include-file-only-if-two-conditions-are-met/ Share on other sites More sharing options...
Maq Posted August 17, 2011 Share Posted August 17, 2011 Your logic is wrong, you want to use && (and). Quote Link to comment https://forums.phpfreaks.com/topic/245051-include-file-only-if-two-conditions-are-met/#findComment-1258659 Share on other sites More sharing options...
cloudll Posted August 17, 2011 Author Share Posted August 17, 2011 ah so simple, thanks I have another issue with the code I have come across. if (($rand_battle == "1") || ($enemy_hp == "0")) { include ('battleon.php'); } $rand_battle is decided using rand (1, 5) and when the include file called battleon.php is included it spawns a enemy to fight, thats why I wanted it to include only if the enemys health is on 0. So it doesnt try and respawn before its even been killed. And the first line of battleon.php resets the characters health to 9999 so you can battle it. but my issue is, once the characters health has been reset to full, the if statement is no longer valid, so as soon as the page refreshes (which it has to do quite a lot) the battleon file dissapears aswell as the enemy to fight. Does anyone have any ideas as to how i can include my battleon.php file when $enemy_hp == "0" but for the include to stay? I was thinking maybe making it so when the enemys health drops to 0, it updates a row in the database, and maybe that could control if the include stays or not, but i cant figure that out in my head lol, sorry if this has been very confusing Quote Link to comment https://forums.phpfreaks.com/topic/245051-include-file-only-if-two-conditions-are-met/#findComment-1258706 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 I'm not sure what you mean when you say you want the include to stay. Quote Link to comment https://forums.phpfreaks.com/topic/245051-include-file-only-if-two-conditions-are-met/#findComment-1258708 Share on other sites More sharing options...
cloudll Posted August 17, 2011 Author Share Posted August 17, 2011 the if statement only includes the battleon.php page if the characters health is at 0 but my battleon.php page updates the characters health back to full, so it can be faught. but then as soon as my main page is refreshed, because the characters not at 0 health, it no longer includes the file i need to fight the enemy. originally, i was using this code to include my battle, but i had to click a button which would update my database to change the value of $battleon. <?php if ($battleon == "1") { include 'battle.php'; } } ?> So i thought with the rand function, and only calling the battle page if the number '1' was generated by the function i could make the battle encounter random rather than having to start it manually. Quote Link to comment https://forums.phpfreaks.com/topic/245051-include-file-only-if-two-conditions-are-met/#findComment-1258718 Share on other sites More sharing options...
cloudll Posted August 17, 2011 Author Share Posted August 17, 2011 basically im trying to make it so a battle encounter is randomly generated, but a new one cannot be generated untill the current one has ended. I just cant figure out a way in my head, let alown on paper :-\ Quote Link to comment https://forums.phpfreaks.com/topic/245051-include-file-only-if-two-conditions-are-met/#findComment-1258724 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 Store the enemies heath in a session variable. Don't call the code to start a new battle until it's less than 1 Quote Link to comment https://forums.phpfreaks.com/topic/245051-include-file-only-if-two-conditions-are-met/#findComment-1258750 Share on other sites More sharing options...
jcbones Posted August 17, 2011 Share Posted August 17, 2011 I would set a session variable, 0 for fight ended, 1 for fight started. So then you could call it: if($_SESSION['fight'] == 0) { //there is no fight currently, lets start one> $_SESSION['fight'] = 1; } if($_SESSION['fight'] == 1) { //Someone started a fight on the last page> //Do fight stuff. if($fight_is_finished) { //Let everyone know that fight is over. $_SESSION['fight'] = 0; } } If that even makes sense to you. Quote Link to comment https://forums.phpfreaks.com/topic/245051-include-file-only-if-two-conditions-are-met/#findComment-1258766 Share on other sites More sharing options...
cloudll Posted August 17, 2011 Author Share Posted August 17, 2011 thanks guys ive never done anything with session variables before and im feeling pretty stupid at the moment, I understand a tiny bit of what you posted, i think Do you know how i would incorporate it with my original code <?php if (($randbattle == "1") && ($battleon == "2")) { include ('battleon.php'); } ?> or is that no longer applicable for what i want it to do? sorry to be so dumb Quote Link to comment https://forums.phpfreaks.com/topic/245051-include-file-only-if-two-conditions-are-met/#findComment-1258768 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 It's getting to the point where you're asking us to code for you. This is the point that I stop helping. There is way too much involved with your script to simply give you something that you can copy and paste and make work. We can offer theories and snippets that you can refer to, but implementing those snippets in to your script is for you to do. Quote Link to comment https://forums.phpfreaks.com/topic/245051-include-file-only-if-two-conditions-are-met/#findComment-1258771 Share on other sites More sharing options...
cloudll Posted August 17, 2011 Author Share Posted August 17, 2011 Ok, I will read up on some session tutorials and see if I understand your replys a little better then Quote Link to comment https://forums.phpfreaks.com/topic/245051-include-file-only-if-two-conditions-are-met/#findComment-1258773 Share on other sites More sharing options...
jcbones Posted August 17, 2011 Share Posted August 17, 2011 Oh, we will help you understand things better. Sessions store a file on the server that contains all of the data you wish to store for a short period of time. You can access that data in an array. In order for the server to know what data to make available, it will set a cookie in the clients browser that contains a value that matches the file name the data is inside. There is only 2 criteria to using sessions. 1. Start the session: <?php //this line MUST be called before ANY output to the page: session_start(); 2. You must save data to the session: <?php //this line MUST be called before ANY output to the page: session_start(); $_SESSION['started'] = 'Yes, you have started a session.'; Now to use that session, is just as simple. <?php //this line MUST be called before ANY output to the page: session_start(); //call the session: if(!empty($_SESSION['started'])) { echo $_SESSION['started']; //You should get an echo, on your first page refresh. } else { $_SESSION['started'] = 'Yes, you have started a session.'; echo 'Session is not showing up currently, please refresh the page.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245051-include-file-only-if-two-conditions-are-met/#findComment-1258779 Share on other sites More sharing options...
cloudll Posted August 17, 2011 Author Share Posted August 17, 2011 thank you so much, i have it working pretty much how i want now. thanks again Quote Link to comment https://forums.phpfreaks.com/topic/245051-include-file-only-if-two-conditions-are-met/#findComment-1258806 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.