CodeIgniter Forums

Full Version: Array and foreach lopp with decimals.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Been twisting my head on a foreach loop that decreses tokens. Attaching code that runs. What i try is to run trough the orders with same price (4 in total here, but after 3 are processed, i can not make it break. Also total tokens/coins add up to 4000 and should go up to 5000 leaving last order with xxxx coins left. Also the < > in the if statements are acting funny, probarly due it's a 8 digit decimal to play with.

The break should be the $user_amout initially and update the last order with remaining tokens to database.

PHP Code:
$user_price  '0.00002450';
$user_amount '5000.00000000';

 if ($check_price == $user_price) {
 // We found a match, buy the amount of tokens
 echo "<br />We found Price a match<br />";
 $equalorder $this->getEqual($type$user_price);
 foreach ($equalorder as $equal) {
   if ($user_amount $equal->amount){
     $to_reduce $equal->amount;
   $user_amount bcsub($user_amount,$to_reduce,8);
   $order_check += $user_amount;
   //$user_amount -= $to_reduce;
   echo "<br> Coins to process from this order : ";
   echo $user_amount ;
   echo "<br>";
   if ($order_check == '5000.00000000') {
  // End foreach loop
 echo "<br /><br /> Total coins processed : ";
 echo $order_check;

the result i get when running it with the var dump included.

  We found Price a match
array(4) { [0]=> object(stdClass)#29 (3) { ["id"]=> string(1) "1" ["price"]=> string(10) "0.00002450" ["amount"]=> string(13) "2500.00000000" } [1]=> object(stdClass)#28 (3) { ["id"]=> string(1) "2" ["price"]=> string(10) "0.00002450" ["amount"]=> string(13) "1000.00000000" } [2]=> object(stdClass)#27 (3) { ["id"]=> string(1) "4" ["price"]=> string(10) "0.00002450" ["amount"]=> string(13) "4000.00000000" } [3]=> object(stdClass)#26 (3) { ["id"]=> string(1) "9" ["price"]=> string(10) "0.00002450" ["amount"]=> string(14) "10000.00000000" } }
Coins to process from this order : 2500.00000000

Coins to process from this order : 1500.00000000

Coins to process from this order : 500.00000000

Coins to process from this order : -500.00000000

Total coins processed : 4000

Thanks for any help!
When is a decimal number a string?
Should they be processed the floatval($value) on each of the decimal numbers ?

If this is the case, things looks abit more easy to make work.