Jump to content

muskelmann098

New Members
  • Posts

    8
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

muskelmann098's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thanks for that. You were right, I neglected to declare the $conn variable within the function. I fixed it and it works now. Thanks a lot for your help.
  2. When I try to run the following piece of code, I get this error: Here is the area of the code where I think the problem is: /*--36--*/ $b = "SELECT * FROM users WHERE town = '$town' AND username = '$name'"; /*--37--*/ $b_res = mysql_query($b,$conn)or die(mysql_error()); /*--38--*/ $barray = mysql_fetch_array($b_res)or die(mysql_error()); /*--39--*/ $blev = $barray["$building"]; I'm not really sure what is wrong with the SQL query as I'm using almost exactly the same syntax a few lines above. I hope someone here might see where I messed up. Thanks in advance
  3. 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.
  4. 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.
  5. 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
  6. Thanks for all the tips, it's really appreciated. I would expect that I'm not always following any recommended coding standards because I'm only self-taught, and kind of a novice too. Anyway, what I'm trying to do is to make a game, kind of like "Travian" if anyone here has played it. 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 holds the cost of each resource depending on which field is being upgraded. My problem is how to get the values here subtracted from the total number of resources stored in the SQL table.*/ function cost($res){ switch($res){ case oil: $oil_cost = 70; $iron_cost = 100; $wood_cost = 100; $crop_cost = 40; case iron: $oil_cost = 110; $iron_cost = 60; $wood_cost = 100; $crop_cost = 40; case wood: $oil_cost = 100; $iron_cost = 110; $wood_cost = 60; $crop_cost = 40; case crop: $oil_cost = 90; $wood_cost = 85; $iron_cost = 85; $crop_cost = 20; } } /*This is where I want to level-up the field (and that works), but also subtract the costs of the upgrade (stored above) from the total number. The way it's set up now is just something I jotted down to tell myself what I want it to do in the end. It's probably not even close to working.*/ switch($_GET['action']) { case 'crop_1': resUpdt(crop1); $oil = totRes(Oil) - cost(oil); $iron = totRes(Iron) - cost(iron); $wood = totRes(Wood) - cost(wood); $crop = totRes(Crop) - 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. Thanks a lot for your help so far. This is definitely one of the best PHP help sites out there!
  7. Thanks, that solution didn't actually work, but it got me thinking of something that did $conn was necessary since it's what allows me to connect to the database, but thanks for the "trigger_error" tip. I have a feeling I'll be using it a lot. Again, thanks for your quick reply
  8. Hello, I have been making a kind of browser-based game, and I have written a function that will get the total number of resources from my database, and the echo it. Unfortunately, it is returning only the name of the variable I put in, and not the values from the table. It might be that I'm only tired after a whole day of coding, but I just can't see what's causing the error. Would someone here care to look over it? 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 'Oil' FROM `Villages` WHERE camp = '$cname'"); $totres_arr = mysql_fetch_array($totres_que); $totres = $totres_arr["Oil"]; echo $totres; } Thanks a lot folks!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.