Results 1 to 3 of 3

Thread: How to fetch the column name?

  1. #1
    Join Date
    Feb 2007
    Location
    Earth
    Posts
    133
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default How to fetch the column name?

    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.

    PHP Code:
    ///// 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>"
    Last edited by Demonicman; 01-06-2015 at 05:39 AM.
    http://www.insanecombat.com << bored? check out ma game

  2. #2
    Join Date
    Feb 2007
    Location
    Earth
    Posts
    133
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default

    This site used to have a lot of help. Now nobody every answers your questions......
    http://www.insanecombat.com << bored? check out ma game

  3. #3
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    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.
    Last edited by DyDr; 01-28-2015 at 10:19 PM.

Similar Threads

  1. Php help manual fetch?
    By mikster in forum PHP
    Replies: 11
    Last Post: 08-22-2011, 08:12 PM
  2. Mysql fetch problems...
    By Rockonmetal in forum MySQL and other databases
    Replies: 3
    Last Post: 09-09-2008, 04:57 AM
  3. Adding a column to 3-column fixed layout
    By slobjones in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 10-21-2007, 03:51 AM
  4. FETCH! or Spyware Detector?
    By techno_race in forum Computer hardware and software
    Replies: 3
    Last Post: 07-08-2007, 03:26 PM
  5. Removing 1 column from 3 column layout
    By finkdawg5 in forum HTML
    Replies: 1
    Last Post: 12-29-2006, 11:59 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •