query for towns
query for towns
Hi,
I have more than 100 towns in my game,when player click on town in next page he should have some infromation about that town,which he should get from my database named towns. Now i don't understand how to make query in order to find specified town,that player clicked on.
When i query for players i do this
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query ($playerinfo) or die ("could not get player stats!");
$playerinfo3=mysql_fetch_array ($playerinfo2);
But how could i got information for towns , how could my query know on which city player clicked?
p.s. I know this is stupid question,but this is beginner section after all.
I have more than 100 towns in my game,when player click on town in next page he should have some infromation about that town,which he should get from my database named towns. Now i don't understand how to make query in order to find specified town,that player clicked on.
When i query for players i do this
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query ($playerinfo) or die ("could not get player stats!");
$playerinfo3=mysql_fetch_array ($playerinfo2);
But how could i got information for towns , how could my query know on which city player clicked?
p.s. I know this is stupid question,but this is beginner section after all.
Re: query for towns
Okay so assume you have a link which says:
If you still don't get what I'm talking about check out this link, I wrote it but it is about a different context but you should be able to understand it.
http://www.makewebgames.com/showthread. ... post305369
Okay so first, execute a query which selects all the town, and display them all (meaning in the while() loop). There after create a link like in the below code.Travel to "Town Name Here"
Code: Select all
$selectTowns = $db->query("Your Query Here!");
// While loop for fetching selectResults
while($fetchTowns = $db_fetch($selectTowns)) {
echo '<a href="somePHPFile?travel=true&townName="'{$fetchTowns['town_name_field_here']}'">Travel to '.$fetchTowns['town_name_field_here'].'</a>';
}
// Checks if link is pressed because by default the URL of the specified page is http://yourSiteName.com/travel.php after you click the link it will become something like http://yourSiteName.com/travel.php?travel=true&townName=The_Town_Name_Will_Be_Here.
if(isset($_GET['travel'])) {
// Sanitization will take place here thereafter
// Which is set once the link is clicked
$townName = htmlspecialchars(trim($_GET['townName']));
// After that you just run a simple query selecting the towns from that name provided.
$selectSpecificTown = $db->query("SELECT FROM TOWNS WHERE THE TOWN NAME = $townName");
// Then run another while loop like the above to display information
// You can do other check and stuff like seeing if that town actually exists and give error message if not. Or use prepared statements.
// $db is where you put mysql_query() I assumed you might be using a DB class.
// Also don't use MySQL choose MySQLi or PDO
}
http://www.makewebgames.com/showthread. ... post305369
Re: query for towns
First i must say that you really put effort in trying to explain me this,thanks for that!
Unfrotunately it looks like i can't understand this,I will read all of this again later (since now i'm in hurry) and try to understand this.
If you could make this more clear for me it would be nice. You had to know that i met with php first time 7 days ago.
Anyway i made a lot of things on my game until now,but of course there is plenty of things that i don't understand.
I'm making all this just in order to learn something.
Unfrotunately it looks like i can't understand this,I will read all of this again later (since now i'm in hurry) and try to understand this.
If you could make this more clear for me it would be nice. You had to know that i met with php first time 7 days ago.
Anyway i made a lot of things on my game until now,but of course there is plenty of things that i don't understand.
I'm making all this just in order to learn something.
- vitinho444
- Posts: 2819
- Joined: Mon Mar 21, 2011 4:54 pm
Re: query for towns
Ok, first of all, even if this was Super Advanced Help and Support, there are no "stupid" questions
About the town query, in order to get a town info, according to your table `towns` it should be like this:
Now, you must be asking how to get the "$TOWN_NAME" variable. You want the player to click on that town right? So why not a GET on the link of each town?
Wherever you have your town links (that the player will click to choose a town) you should have something like this:
First, the "page.php" is the page where you want to display the town info to the player.
The "?town=id" is to tell "page.php" that you are sending a town id to look up. Remember to change the id for the variable containing the id of the town ($towninfo3[0] might work).
Now on the "page.php" with that link you need to have this:
I hope you have id's on your Towns' database table, if you don't, you should because it's easier to identify by ID's in the backend
Good luck
About the town query, in order to get a town info, according to your table `towns` it should be like this:
Code: Select all
$townInfo="SELECT * from `towns` where name='$TOWN_NAME'";
$townInfo2=mysql_query ($townInfo) or die ("could not get town info!");
$townInfo3=mysql_fetch_array ($townInfo2);
Wherever you have your town links (that the player will click to choose a town) you should have something like this:
Code: Select all
<a href='page.php?town=id'>Town Name #1</a>
The "?town=id" is to tell "page.php" that you are sending a town id to look up. Remember to change the id for the variable containing the id of the town ($towninfo3[0] might work).
Now on the "page.php" with that link you need to have this:
Code: Select all
if(isset($_GET["town"]))
{
$id = $_GET["town"]; //this is our id fetched from the link
$townInfo="SELECT * from `towns` where id='$id' ";
$townInfo2=mysql_query ($townInfo) or die ("could not get town info!");
$townInfo3=mysql_fetch_array ($townInfo2);
//And now you have the town information :)
}
Good luck
Re: query for towns
Pretty much what I meant but this is simpler. xDvitinho444 wrote:Ok, first of all, even if this was Super Advanced Help and Support, there are no "stupid" questions
About the town query, in order to get a town info, according to your table `towns` it should be like this:Now, you must be asking how to get the "$TOWN_NAME" variable. You want the player to click on that town right? So why not a GET on the link of each town?Code: Select all
$townInfo="SELECT * from `towns` where name='$TOWN_NAME'"; $townInfo2=mysql_query ($townInfo) or die ("could not get town info!"); $townInfo3=mysql_fetch_array ($townInfo2);
Wherever you have your town links (that the player will click to choose a town) you should have something like this:First, the "page.php" is the page where you want to display the town info to the player.Code: Select all
<a href='page.php?town=id'>Town Name #1</a>
The "?town=id" is to tell "page.php" that you are sending a town id to look up. Remember to change the id for the variable containing the id of the town ($towninfo3[0] might work).
Now on the "page.php" with that link you need to have this:I hope you have id's on your Towns' database table, if you don't, you should because it's easier to identify by ID's in the backendCode: Select all
if(isset($_GET["town"])) { $id = $_GET["town"]; //this is our id fetched from the link $townInfo="SELECT * from `towns` where id='$id' "; $townInfo2=mysql_query ($townInfo) or die ("could not get town info!"); $townInfo3=mysql_fetch_array ($townInfo2); //And now you have the town information :) }
Good luck
- vitinho444
- Posts: 2819
- Joined: Mon Mar 21, 2011 4:54 pm
Re: query for towns
Yeah I just did a more "halls tutorials" approach so that people that are following can continue following
But your explanation was as good as mine
But your explanation was as good as mine
Re: query for towns
Everything works fine!
Thank you very much vitinho444
I understand everything very easily.
Thanks Script47 too,as i said you really tried hard to explain me what to do but i didn't understand it :S . That's my fault.
Anyway guys,it's really amazing how everybody want to help on this forum,it's really great for young people like me to learn something about php,java or MMORPG creating.
Can i ask you one mroe thing.
I would like that in my game exist mines,where players will send their citizens to work and get resources for their empire. How could i make that they get certain amount of resources every hour (or if production is 1000 resources per hour,then to get 1 resource every 3 seconds). Of course i don't expect complete coding,just to tell me how could i do that,and what should i use.
Thank you very much vitinho444
I understand everything very easily.
Thanks Script47 too,as i said you really tried hard to explain me what to do but i didn't understand it :S . That's my fault.
Anyway guys,it's really amazing how everybody want to help on this forum,it's really great for young people like me to learn something about php,java or MMORPG creating.
Can i ask you one mroe thing.
I would like that in my game exist mines,where players will send their citizens to work and get resources for their empire. How could i make that they get certain amount of resources every hour (or if production is 1000 resources per hour,then to get 1 resource every 3 seconds). Of course i don't expect complete coding,just to tell me how could i do that,and what should i use.
- vitinho444
- Posts: 2819
- Joined: Mon Mar 21, 2011 4:54 pm
Re: query for towns
I'm glad you did it
About the mines, it's very easy in fact. When I was into browser based games, I used this method:
Create a database table called... mines or wtv you want with the following structure:
`playerid` INT
`time` INT
`mine` INT
`forest` INT
...You can add more if you want
Then what you need to do to send people to work on the mines is simply update the value of `mine` WHERE `playerid`='$player_id'.
About the giving the resources is fairly simple too. Notice the `time` field in the database? There is where you will put " time() ", that will give you the seconds passed since Jan 1st 1970, and with that you can control how much time it passed since the last resource time.
like this:
Note that RESOURCE_TIME is the number of seconds you want per resource. If you want to give resources every hour, put 3600 as RESOURCE_TIME.
I recommend putting this script somewhere in a page that the player visits to check his resources (put this in the top).
Hope you understood it all
About the mines, it's very easy in fact. When I was into browser based games, I used this method:
Create a database table called... mines or wtv you want with the following structure:
`playerid` INT
`time` INT
`mine` INT
`forest` INT
...You can add more if you want
Then what you need to do to send people to work on the mines is simply update the value of `mine` WHERE `playerid`='$player_id'.
About the giving the resources is fairly simple too. Notice the `time` field in the database? There is where you will put " time() ", that will give you the seconds passed since Jan 1st 1970, and with that you can control how much time it passed since the last resource time.
like this:
Code: Select all
$rTime = $mineInfo3[1]; //Change this to met the value of `time` in the table ;)
$mineWorkers = $mineInfo3[2];
$forestWorkers = $mineInfo3[2];
if($rTime < 1)
mysql_query(" UPDATE `mines` SET `time`='time()' WHERE `playerid`='$player_id' ");
else
{
$tPassed = time() - $rTime;
if($tPassed > RESOURCE_TIME)
{
$intervals = $tPassed / RESOURCE_TIME;
$intervals = round($intervals); //Round the number to down
//In order to receive resources this page needs to be called, lets say it doesnt in some time, we can't just give 1 time resources since the player is owed more than that.
$resource = BASE_RESOURCE_AMOUNT * $mineWorkers * $intervals;
$wood = BASE_RESOURCE_AMOUNT * $forestWorkers * $intervals;
mysql_query(" UPDATE `player` SET `resource`=`resource`+'$resource' WHERE `id`='$player_id' ")
//Reset the timer
mysql_query(" UPDATE `mines` SET `time`='time()' WHERE `playerid`='$player_id' ");
}
}
I recommend putting this script somewhere in a page that the player visits to check his resources (put this in the top).
Hope you understood it all
Re: query for towns
I think i could understand all of this.Hope you understood it all
Should i create field "time" in database,or i shoudl already have it somwhere in my database by default? Because i don't see it thereNotice the `time` field in the database?
Re: query for towns
You know how you get field types like INT, VAR CHAR? Well find one called time.kibo95 wrote:I think i could understand all of this.Hope you understood it all
Should i create field "time" in database,or i shoudl already have it somwhere in my database by default? Because i don't see it thereNotice the `time` field in the database?