Forum Moderators: coopster
function func_select_min_id($type_id_array){
foreach ($type_id_array as $type_id)
$item_query = 'SELECT * FROM prefs'." WHERE id='".mysql_real_escape_string($type_id)."'";
$item_result = mysql_query($item_query) or die ("Error doing query for this item ");
$item_row = mysql_fetch_array($item_result);
$rewards[] = ($item_row['paid']*$item_row['paid2']);
}
}
what this function does isnt that hard, I pass it an array of possible id's, which are basically mysql field id nr's and it looks up the concerning row, fetches 2 values and multiplies the 2, then the result is stuffed in a new array
My problem is I need to fetch the minimal value of the items in the array and fetch the key (so I need to re-establish which key actually corresponds to the product of the 2 values)
I know how min() works, but it wont return me a key, which is what I need :)
I know I can do multiplications inside a query, but can I do something like SELECT MIN * (paid * paid2) AS price FROM table WHERE col=2,6,7
(I know the last part wont work)
how do I select multiple rows and determine the id with the lowest product of two fields?
function func_select_min_id($type_id_array){
foreach ($type_id_array as $type_id){
$item_query = 'SELECT * FROM prefs'." WHERE rp_id='".mysql_real_escape_string($type_id)."'";
$item_result = mysql_query($item_query) or die ("Error doing query for settings for this item ");
$item_row = mysql_fetch_array($item_result);
$ids[] = $type_id;
$values[] = (int)($item_row['nr_members']*$item_row['paid']);
}
$iMinValue = min($values);
$arFlip = array_flip($values);
$iMinPosition = $arFlip[$iMinValue];// this is the one ;)
return $ids[$iMinPosition];
}?>