query for towns

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
kibo95
Posts: 35
Joined: Wed Jul 16, 2014 6:59 am

query for towns

Post by kibo95 »

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.
User avatar
Script47
Posts: 147
Joined: Thu Nov 21, 2013 6:11 pm

Re: query for towns

Post by Script47 »

Okay so assume you have a link which says:
Travel to "Town Name Here"
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.

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 
}
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
User avatar
kibo95
Posts: 35
Joined: Wed Jul 16, 2014 6:59 am

Re: query for towns

Post by kibo95 »

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.
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

Re: query for towns

Post by vitinho444 »

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:

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);
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:

Code: Select all

<a href='page.php?town=id'>Town Name #1</a>
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:

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 :)
}
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
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Script47
Posts: 147
Joined: Thu Nov 21, 2013 6:11 pm

Re: query for towns

Post by Script47 »

vitinho444 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:

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);
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:

Code: Select all

<a href='page.php?town=id'>Town Name #1</a>
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:

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 :)
}
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
Pretty much what I meant but this is simpler. xD
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

Re: query for towns

Post by vitinho444 »

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 ;)
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
kibo95
Posts: 35
Joined: Wed Jul 16, 2014 6:59 am

Re: query for towns

Post by kibo95 »

Everything works fine!
Thank you very much vitinho444 :D
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.
:D
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

Re: query for towns

Post by vitinho444 »

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:

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' ");

   }
}

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 ;)
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
kibo95
Posts: 35
Joined: Wed Jul 16, 2014 6:59 am

Re: query for towns

Post by kibo95 »

Hope you understood it all ;)
I think i could understand all of this.
Notice the `time` field in the database?
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 there
User avatar
Script47
Posts: 147
Joined: Thu Nov 21, 2013 6:11 pm

Re: query for towns

Post by Script47 »

kibo95 wrote:
Hope you understood it all ;)
I think i could understand all of this.
Notice the `time` field in the database?
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 there
You know how you get field types like INT, VAR CHAR? Well find one called time.
Post Reply

Return to “Beginner Help and Support”