Forum Moderators: coopster

Message Too Old, No Replies

little help here, damn arrays :p

         

dmmh

9:06 pm on Jan 3, 2006 (gmt 0)

10+ Year Member




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 :)

dmmh

9:09 pm on Jan 3, 2006 (gmt 0)

10+ Year Member



mmm, just occured to me I might be better of doing this in the select statement.....

dmmh

9:23 pm on Jan 3, 2006 (gmt 0)

10+ Year Member



and I dont know how to this either lol

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?

dmmh

10:40 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



fixed like this:


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];
}?>