EchoFool Posted November 23, 2008 Share Posted November 23, 2008 Hey, Looking for some ideas on what is best way to approach an idea i have. I have for 4 variables calculated from values in fields related to record id's there are 4 record id's in total. So while it while loops these 4 rows it generates the four values like this: Record 1 = 1000 Record 2 = 2000 Record 3 = 500 Record 4 = 100 Now this is worked out by grabbing field values then using php to calculate total value. What I am trying to do whilst it is while looping though is select the lowest value record. In the above case it would be record 4 but I am not sure what kinda php feature to use to do this ? Any ideas? Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted November 23, 2008 Share Posted November 23, 2008 You could push each value into an array during your loop, then (once you've finished the loop) sort the array and then get the first value from that array. Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 23, 2008 Share Posted November 23, 2008 min (prefferably put your records into array) Quote Link to comment Share on other sites More sharing options...
EchoFool Posted November 23, 2008 Author Share Posted November 23, 2008 Hey i have put that in like this this but it isn't working: <?php print_r($State); Echo '<br>'; echo min($State); ?> Which displays: Array ( [1] => 178061500 [2] => 0 [3] => 0 [4] => 0 ) 0 Was hoping it would show value [2] because thats the lowest value and then to sort it out from a tie between 3 and 4 its the lowest position in the array but it just returns 0 =/ Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted November 23, 2008 Share Posted November 23, 2008 It's doing exactly what it should. 0 is the lowest value in your array, so it's returning 0. If you after the key for the lowest value, use sort(). Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 23, 2008 Share Posted November 23, 2008 Oh. So you actually need index of the lowest value. If so, then asort should help (not sort, as it will break key->value associations) Quote Link to comment Share on other sites More sharing options...
EchoFool Posted November 23, 2008 Author Share Posted November 23, 2008 Argh thank you Quote Link to comment 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.