Jump to content

Populate array and extract MIN value


w424637

Recommended Posts

I think I'm almost there but not quite getting it.

 

I have a query - from this I run a loop to establish a running balance - daya to day cumulative total.

I then want to store this as an array. and finally extract to a variable the smallest value in the entire array.

 

Below is the rogue code!!

 

  $strQuery = "SELECT uspgi_reporting.race_date, date_conv.month2, sum(uspgi_reporting.handle) as handle, sum(uspgi_reporting.profit) as trading_profit FROM uspgi_reporting Inner Join date_conv ON uspgi_reporting.race_date = date_conv.actual_date  Where 1 = 1  group by uspgi_reporting.race_date, date_conv.month2";

$result = mysql_query($strQuery) or die(mysql_error());

$row_result = mysql_fetch_assoc($result);

 

$myArray = array();

 

while($row_result = mysql_fetch_assoc($result))

 

{

  $running_total+= $row_result['trading_profit'];

  $myArray[] = $running_total;

 

}

 

$minValue = min(array($myArray));

 

echo $minValue;

 

 

Any help really appreciated

 

 

Link to comment
https://forums.phpfreaks.com/topic/241710-populate-array-and-extract-min-value/
Share on other sites

Should anyone chance upon this I have resolved the problem.

 

Below is the code I have changed.

 

$myArray = array();

 

while($row_result = mysql_fetch_assoc($result))

 

{

  $running_total+= $row_result['trading_profit'];

  $myArray[] = $running_total;

 

}

 

echo min($myArray);

You found the error before I finished my post, but do you need all the individual values in the array or do you just need the total and minimum? If you only need the total and minimum you can create a query to get those values instead of querying all of the values and doing calculations in PHP.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.