0

我有一个数组 year_values 包含一些值,例如

$year_values = array();
foreach($year_strings as $str) {
  $year_values[] = explode(",", $str);
}

然后我应用查询从数据库中提取一些值

$sql = "SELECT inventory.holdingcost as holdingcost, 
inventory.ordercost as ordercost, inventory.unitprice as unitprice, 
inventory.lead_time as lead_time, items.id as iid 
FROM inventory, items 
WHERE inventory.item_name = items.item_name";
        mysql_error();

        $result = mysql_query($sql);
    foreach($year_values as $vals) {

      while ($row = mysql_fetch_row($result)) {



      $w = $row[2];
      $e = $row[0];
      $r = $row[1];
      $tt = $row[3];
      $eo = sqrt(2 *  $vals[5] * $r / ($e * $w));
      $eoq = round($eo, 1);
      $ro = $vals[5] / 360;
      $rop = round($ro * $tt);
      $op = round($vals[5] / $eoq, 1);
      $cc = round(89 / $op);
      $h = round((($eoq * $e * $w) / 2), 2);
      $o = round((($r * $vals[5]) / $eoq), 2);
      $z = round($h + $o, 2);


       }
    }

当我在上面的 while 循环中使用 foreach 时,它只需要$year_values$vals[5] 的第一个值,我想在 while 中对 array 的每个值进行计算$year_values

如何纠正它?

重复发生在 value1 即 $val[5] 当前:

Value1      Value 2    Value 3
199           202           0.25
199           205           0.25
199           210           0.25
199           230           0.25
1698          202           0.25
1698          205           0.25
1698          210           0.25
1698          230           0.25

相反,我希望值显示为

Value1      Value 2    Value 3
199          202           0.25
1698         205           0.25
15           210           0.25
971          230           0.25
4

3 回答 3

3

我想你只需要存储所有数据,这样你就可以重复使用它。

$result = mysql_query($sql);
$rows = array() ;
while ($row = mysql_fetch_row($result)){
  $rows[] = $row ;
}

foreach($year_values as $vals) {
  foreach($rows as $row){
   $w = $row[2];
   $e = $row[0];
   $r = $row[1];
   $tt = $row[3];
   $eo = sqrt(2 *  $vals[5] * $r / ($e * $w));
   $eoq = round($eo, 1);
   $ro = $vals[5] / 360;
   $rop = round($ro * $tt);
   $op = round($vals[5] / $eoq, 1);
   $cc = round(89 / $op);
   $h = round((($eoq * $e * $w) / 2), 2);
   $o = round((($r * $vals[5]) / $eoq), 2);
   $z = round($h + $o, 2);
  }
}
于 2013-06-30T08:08:37.707 回答
2

简单地颠倒陈述:

while ($row = mysql_fetch_row($result)) {
foreach($year_values as $vals) {


      $w = $row[2];
      $e = $row[0];
      $r = $row[1];
      $tt = $row[3];
      $eo = sqrt(2 *  $vals[5] * $r / ($e * $w));
      $eoq = round($eo, 1);
      $ro = $vals[5] / 360;
      $rop = round($ro * $tt);
      $op = round($vals[5] / $eoq, 1);
      $cc = round(89 / $op);
      $h = round((($eoq * $e * $w) / 2), 2);
      $o = round((($r * $vals[5]) / $eoq), 2);
      $z = round($h + $o, 2);


       }
    }
于 2013-06-30T08:10:15.743 回答
1

获取所有行后,mysql_fetch_row 将返回 false。要重用它,您应该将它们存储在一个变量中(如 Jari 所做的响应),或者您可以切换两个周期(参见 imulsion 的答案)

于 2013-06-30T08:13:45.317 回答