Quostin Posted June 17, 2013 Share Posted June 17, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/279244-printing-column-name-based-on-its-row-value/ Share on other sites More sharing options...
mac_gyver Posted June 17, 2013 Share Posted June 17, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/279244-printing-column-name-based-on-its-row-value/#findComment-1436300 Share on other sites More sharing options...
Quostin Posted June 17, 2013 Author Share Posted June 17, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279244-printing-column-name-based-on-its-row-value/#findComment-1436304 Share on other sites More sharing options...
Zane Posted June 17, 2013 Share Posted June 17, 2013 (edited) while( $row = mysql_fetch_array($result) ) { //Looking for 2 echo each($row) == 2 ? key($row) : null; } Edited June 17, 2013 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/279244-printing-column-name-based-on-its-row-value/#findComment-1436319 Share on other sites More sharing options...
mac_gyver Posted June 17, 2013 Share Posted June 17, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279244-printing-column-name-based-on-its-row-value/#findComment-1436397 Share on other sites More sharing options...
DavidAM Posted June 21, 2013 Share Posted June 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279244-printing-column-name-based-on-its-row-value/#findComment-1437273 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.