Log in

View Full Version : How to fetch the column name?



Demonicman
01-06-2015, 05:11 AM
Ok so I have a table like so:

Name---------Bank----------Store----------Library

Town1---------1,6---------15,2---------12,16

Town2---------3,2---------2,8---------16,16


So my question is how do I get the name of the column for use in a variable?

This is for a map. So when the map builds on the screen for the player, I will put the current user coordinates into a string to find a match on the table and fetch the column that it is in. So if the user is in Town1 and they are on the coordinates 15,2 then they should be able to see a picture of a Store. If I use the town Name in the query, I would have to check each column in a loop to match the correct coordinates to the correct column name to determine what should be on that location, and I fear that would be hard on the server to constantly do that every single time someone moves locations on the map.

In the most simplest sense, I want to be able to fetch the column name based on the if the coordinates are in that column. That way I can just put the column name into a variable string to be used in both a link and a different picture than the rest of the map.


///// First check current user location with a query to the Users table and put into a fetch array variable called $User.
$User=mysqli_fetch_array(mysqli_query("select * from Users where ID='1'"));
echo "You are at $User[Town] at the coordinates $User[Coords]";
///// Find column name in town table where $User[Town] matches the row and then find what column the matching $User[Coords] is in to put in a variable called $Name
echo "Here is the store: <a href='$Name.php'><img src='$Name.jpg'></a>";

Demonicman
01-08-2015, 07:06 AM
This site used to have a lot of help. Now nobody every answers your questions......

DyDr
01-28-2015, 09:56 PM
I fear that would be hard on the server to constantly do that every single time someone moves locations on the map.

Yes, it would. Your database design is not 'normalized'. It's more like a spreadsheet, that lays information out for a human to access, that requires extra manipulation for a computer to find or update. To normalize the data, basically, each piece of data must be stored in it's own row. This lets you find or manipulate each piece of data, or any set of data that single piece is part of, directly in a query.

Just off of the top of my head, you need a town table, that holds or defines the town_id, town name, and any other town related info. A location table, that holds or defines the location_id, location name (Store, Bank, Library), and any other location related info. The table you have posted, call it 'places' would have columns for place_id, town_id, location_id, x coord, y coord, and any other specific information about that location in that town, such as a store name... Your Users table should actually just hold the place_id from this last table as that defines everything about the place the user is at.

To get and display the current user's town/location, you would use one query that join's the related tables.