muskelmann098 Posted December 15, 2009 Share Posted December 15, 2009 Hello, I'm trying to make a sort of browser-based multiplayer game. It might be way over my head, but my plan is to learn from it as I go along. Right now, I'm trying to get a system down for the "payment" of resources whenever a field is upgraded. Here is what I have at the moment: <?php include("database.php"); include("createcamp.php"); include("login.php"); include("resformula.php"); $name = $_SESSION['username']; /*This function levels up the field when the user asks for it. So far it's working perfectly! */ function resUpdt($resnum){ global $conn, $name; // I know you told me, but I haven't got to fixing this one yet. Will do soon though. $result = mysql_query("SELECT camp FROM `Villages` WHERE username='$name'"); $cnamearr = mysql_fetch_array($result); $cname = $cnamearr['camp']; $level_result = mysql_query("SELECT * FROM Villages WHERE camp = '$cname'"); $levelarr = mysql_fetch_array($level_result); $level = $levelarr["$resnum"]; $newlevel = ($level + 1); $result = mysql_query("UPDATE Villages SET $resnum = '$newlevel' WHERE camp = '$cname'"); } /*This function uses another one of my functions (included at the bottom to subtract the resource cost from the total amount of resources. Unfortunately, it's not working. I'm not very surprised because I had no clue how to do it from the start, so this is just some trial and error attempt. I hope someone here can tell me how to do it properly.*/ function cost($res){ switch($res){ case oil: $oil = totRes(Oil) - $oil_org = 70; $iron = totRes(Iron) - $iron_org = 100; $wood = totRes(Wood) - $wood_org = 100; $crop = totRes(Crop) - $crop_org = 40; case iron: $oil_org = 110; $iron_org = 60; $wood_org = 100; $crop_org = 40; case wood: $oil_org = 100; $iron_org = 110; $wood_org = 60; $crop_org = 40; case crop: $oil = totRes(oil) - $oil_org = 90; $wood = totRes(iron) - $wood_org = 85; $iron = totRes(iron) - $iron_org = 85; $crop = totRes(iron) - $crop_org = 20; echo "$oil, $iron, $wood, $crop"; } } switch($_GET['action']) { case 'crop_1': resUpdt(crop1); cost (Crop); break; /* This switch statement continues, but it's very large and it does the same for ever case so I'm not going to post all of it. So there you have it! That's what I want to do, and I have to admit, it seems like a huge undertaking, but hopefully with a push in the right direction, I will find a way. Here is the "totRes" function. It takes the current number of resources in store in the player's village. function totRes($resource){ global $conn, $name; $result = mysql_query("SELECT camp FROM `Villages` WHERE username = '$name'"); $cnamearr = mysql_fetch_array($result); $cname = $cnamearr['camp']; $totres_que = mysql_query("SELECT * FROM `Villages` WHERE camp = '$cname'"); $totres_arr = mysql_fetch_array($totres_que); $totres = $totres_arr["$resource"]; echo $totres; I hope someone here is able to understand what I'm trying to do and see where I'm going wrong. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/185208-using-functions-within-another-function/ Share on other sites More sharing options...
trq Posted December 15, 2009 Share Posted December 15, 2009 You failed to actually describe your problem. Quote Link to comment https://forums.phpfreaks.com/topic/185208-using-functions-within-another-function/#findComment-977726 Share on other sites More sharing options...
muskelmann098 Posted December 15, 2009 Author Share Posted December 15, 2009 Well... that really helps... So what would you say my problem is? I said in my previous post I'm not even close to sure about how to do this. Quote Link to comment https://forums.phpfreaks.com/topic/185208-using-functions-within-another-function/#findComment-977728 Share on other sites More sharing options...
dgoosens Posted December 15, 2009 Share Posted December 15, 2009 Well... that really helps... So what would you say my problem is? I said in my previous post I'm not even close to sure about how to do this. I guess you could start by trying to explain clearly what you are trying to achieve and what the actual problems are you encounter... Quote Link to comment https://forums.phpfreaks.com/topic/185208-using-functions-within-another-function/#findComment-977731 Share on other sites More sharing options...
muskelmann098 Posted December 15, 2009 Author Share Posted December 15, 2009 I'm sorry if I wasn't being clear enough. I'll give it another shot. I'm trying to make a game where a player has a village with a certain amount of resources. The total number of resources for each village is stored in a MySQL database. When a player wants to upgrade his resource field, this should cost him a certain amount of resources as described here: <?php function cost($res){ switch($res){ case oil: $oil = totRes(Oil) - $oil_org = 70; $iron = totRes(Iron) - $iron_org = 100; $wood = totRes(Wood) - $wood_org = 100; $crop = totRes(Crop) - $crop_org = 40; } } ?> The "_org" variables are the costs, and the $oil, $iron, $wood, and $crop variables are the total amount of resources left. The totRes() function, described below, gets the total number of resources in that village, so that if this was working, it would take the total number of resources in a village, and subtract the costs. Then I would go on with updating the database. The "totRes()" function gets the total number of resources for each village. These numbers are stored in a database. <?php include("login.php"); include("database.php"); include("createcamp.php"); include("resupdt.php"); $name = $_SESSION['username']; function totRes($resource){ global $conn, $name; $result = mysql_query("SELECT camp FROM `Villages` WHERE username = '$name'"); $cnamearr = mysql_fetch_array($result); $cname = $cnamearr['camp']; $totres_que = mysql_query("SELECT * FROM `Villages` WHERE camp = '$cname'"); $totres_arr = mysql_fetch_array($totres_que); $totres = $totres_arr["$resource"]; echo $totres; } ?> The problem is basically that it doesn't work... No matter how "newbish" that sounds, it's a fact, and since I'm not sure if I am allowed to do what I'm trying to do, I'm not really sure how to go by troubleshooting either. I think this is the first time in this project where I've simply not had a clue. I hope that makes it clearer. If you need any more information, or anything is unclear, please ask and I will do my best to answer. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/185208-using-functions-within-another-function/#findComment-977737 Share on other sites More sharing options...
sasa Posted December 15, 2009 Share Posted December 15, 2009 change echo $totres; to return $totres; Quote Link to comment https://forums.phpfreaks.com/topic/185208-using-functions-within-another-function/#findComment-977878 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.