Kryllster Posted December 31, 2008 Share Posted December 31, 2008 I have an entry point to a dungeon room (Im making a game) and Its supposed to take a diamond out of the inventory. Now it was working with a link to the php sctript then using the header location funtion it would take the diamond the send him to the page. I am wanting to just use this over and over agiain as a function. I have like 7 or 8 scripts in different rooms to do this. Here is what my function looks like?? <?php function takeDiamond($arg){ switch ($arg){ case "main1": $takedia = $sql="UPDATE $tbl_name SET diamond = diamond - 1 WHERE username='$username'"; break; } return $takedia; } ?> now in the index.php it just shows the content but I am at a loss how to do it or I mean get it to work??? Stumped again. Thanks. Kryllster Link to comment https://forums.phpfreaks.com/topic/139044-solved-not-sure-how-to-solve-this/ Share on other sites More sharing options...
Zhadus Posted December 31, 2008 Share Posted December 31, 2008 I don't exactly understand what you're doing with that switch statement. Just run the function when someone tries to enter a room that requires a diamond and have it first check for diamonds, and return pass/fail. function takeDiamond() { $diamond = (Run a query to get amount of diamonds in inventory); if ($diamond > 0) { $diamond--; (Run new query to update the database with new diamond amount); return true; } else { return false; } Then just check that function for true or false when the person enters the room. Link to comment https://forums.phpfreaks.com/topic/139044-solved-not-sure-how-to-solve-this/#findComment-727231 Share on other sites More sharing options...
Kryllster Posted December 31, 2008 Author Share Posted December 31, 2008 Ok well that makes more sense but where do I run the function at in the content page or the controller index, and how do I make it run for lack of better wording. ??? Link to comment https://forums.phpfreaks.com/topic/139044-solved-not-sure-how-to-solve-this/#findComment-727246 Share on other sites More sharing options...
chronister Posted January 1, 2009 Share Posted January 1, 2009 Basically, you would run something like this wherever you want the diamond check / reduction to take place. If that is on your index page, then run it there. If it is on the content page.... your script and the logic determines where you want to run this. <?php if(takeDiamond()) { // let the user proceed, they have 1 less diamond now } else { // the user does not have enough diamonds to proceed. } ?> Nate Link to comment https://forums.phpfreaks.com/topic/139044-solved-not-sure-how-to-solve-this/#findComment-727292 Share on other sites More sharing options...
Kryllster Posted January 1, 2009 Author Share Posted January 1, 2009 Thanks for all your help I have it now. Learned quite a bit tonight. Kryllster Link to comment https://forums.phpfreaks.com/topic/139044-solved-not-sure-how-to-solve-this/#findComment-727416 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.