php - Get away from MySQL query inside foreach loop. foreach causes issues with while loop? -
i have following code:
<?php //the company_array: $company_array = array( "aaa" => "aaa", "bbb" => "bbb", "ccc" => "ccc", "ddd" => "ddd" ); $platform_data = 'pc'; //just keep short :) foreach ($company_array $company) { if ($stmt = $mysqli->prepare("select price, time $company platform = ? order time asc")) { $stmt->bind_param("s", $platform_data); $stmt->execute(); $stmt->bind_result($price[$company], $time[$company]); $i=0; while ($stmt->fetch()) { $company_info[$company][$i] = array('price' => $price[$company], 'time' => $time[$company]); $i++; } $stmt->close(); } ?>
now had issues this, last iteration of loop seems break, if print $company_info last company displays fine, last 1 seem repeat last value rows:
array ( [aaa] => array ( [0] => array ( [price] => 626.8600 [time] => 2013-09-27 14:30:06 ) [1] => array ( [price] => 615.5900 [time] => 2013-09-27 15:45:05 ) [2] => array ( [price] => 604.7400 [time] => 2013-09-27 17:45:05 ) ) [bbb] => array ( [0] => array ( [price] => 246.7200 [time] => 2013-09-27 14:30:06 ) [1] => array ( [price] => 245.4700 [time] => 2013-09-27 15:45:05 ) [2] => array ( [price] => 244.8300 [time] => 2013-09-27 17:45:05 ) ) [ccc] => array ( [0] => array ( [price] => 189.0900 [time] => 2013-09-27 14:30:06 ) [1] => array ( [price] => 188.9800 [time] => 2013-09-27 15:45:05 ) [2] => array ( [price] => 188.8900 [time] => 2013-09-27 17:45:05 ) ) [ddd] => array ( [0] => array ( [price] => 134.3100 [time] => 2013-10-06 13:30:06 ) [1] => array ( [price] => 134.3100 [time] => 2013-10-06 13:30:06 ) [2] => array ( [price] => 134.3100 [time] => 2013-10-06 13:30:06 ) ) )
as can see company aaa, bbb , ccc have different prices , times in each of arrays, company ddd has same value repeated 3 times (the last value in database) when values should different in same way other companies.
now have read doing wrong, instead of using foreach should use implode() on array , use that, confusing me since can not find example used on field (seems used on where)
my question be, how can away using foreach loop since seems causing issues, , use proper implode() method on field?
i guessing need modify while loop output remains in same format.
Comments
Post a Comment