tags:

views:

80

answers:

1

I have an array of fields (not rows!) I want to loop through, in a table, in a predefined order, backwards (alternatively, I could reverse the order of the array) and return the field name of the first field that returns a predefined value.

For example, the fields are like this:

field_a => 1
field_b => 0
field_c => 1
field_d => 0
field_e => 0

And say, field_c was the first with the value I was looking for, even though field_a may already possess the value. What is the best way to loop through e through a, and stopping at c when it's the first field with the value, and returning that field name?

I am working with PHP and MySQL, so I would prefer the returned field name to be a string, not an array - I don't need the value of the row();, only the field name itself.

Thank you! :)

+2  A: 

Would this work?

SELECT *
FROM yourtable
WHERE criteria-for-property
ORDER BY whatever-ordering-you-want
LIMIT 1

This would retrieve 1 row and then stop.

Lasse V. Karlsen
Which, if you read the question carefully, is what he asks for. He says "That value" and "return the row name and value of the first row that returns a predefined value.".
Lasse V. Karlsen
Added for clarity; I want the returned field name.
dmanexe