Jump to content

Printing Column name based on its Row value


Quostin

Recommended Posts

I been stuck on this for a while, itching my head as I try to think about it. I know columns can print the value in the row under it, but I want to scan the whole row of a name, then look at the column and print the name.

 

For example

 

Name | Apple | Banana | Grape | Orange

Eric          1            2            3          4

Bob          2            3            4          1

George    3            4            1          2

 

I got

$res = "select * from fruit where name='Eric'";
$res = $db->query($res);
$res = $res->fetch(PDO::FETCH_ASSOC); 

I want it to look at everything in row where Eric is the name. Then I want to input 2 as a variable and scan through the whole row to match 2 and then display Banana.

 

I know I can do $res[banana] to grab the 2, but I want to do the opposite of that. I wish I could explain it better.

 

I also know that you can do:

if ($res[banana] == 2)
{
Code here
} 
else
{
More code here
} 

The problem with that is I will be having several columns and will take a long time to check every column

        

Link to comment
Share on other sites

the reason you are having a hard time doing this is because you are trying to use the database table like it is a spread-sheet and that is not efficient from a coding standpoint. you need to store each separate piece of data as one row in your database table. then you can find any or all of any type of data directly in the query.

 

name | key | value

Eric | 1   | apple

Eric | 2   | banana

Eric | 3   | grape

Eric | 4   | orange

 

SELECT value FROM your_table WHERE name='Eric' AND key=2

Link to comment
Share on other sites

I thought of that too, but not really want I want. I'll try again. There are going to be several columns and several rows alone, and can't have the name being posted so many times. I just want to grab the column name of the value it finds, and then save that column name as a variable. I'm not even sure if it is possible. Each time I had a new column, I would have to add one more row for each time in the database and that seems like it would be a lot more work.

 

This may be better in the php area. You can close this and I'll post there with a better example and more coding. Thank you or the reply.

Link to comment
Share on other sites

can't have the name being posted so many times

 

 

the example i posted contained only the same level of detail as what you provide about what you are actually doing. you would actually use a name_id in that table, with a separate table that stores the actual names and their associated id's. if you want better/specific/detailed replies, you need to post actual/better/detailed information about what you are doing.

 

something tells me that the 1,2,3,4,... values control some ordering of the output and your statement that you want to find the column for a specific value like 2 really means - you want to retrieve/find the values in the order given by the numbers. using the method i have suggested, this would be -

 

SELECT value FROM your_table WHERE name='Eric' ORDER BY key

 

you would then just loop over the rows the query return and output whatever your fruit names actually correspond to.

Link to comment
Share on other sites

While I absolutely agree with mac_gyver -- the data needs to be normalized -- the function you are looking for is array_search. Since you are using FETCH_ASSOC, this will return the column name for the value you have.

 

Again, the database should be normalized rather than trying to work around issues that should not exist.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.