0

I have an array year_values containing some values like

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

then I apply a query for extracting some values from database

$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);


       }
    }

When I use foreach above while loop it just takes first value of $year_values as $vals[5], I want to make computations inside while for every value of array $year_values.

How to correct it?

repetition is occurring in value1 i.e $val[5] currently:

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

instead i want the values to be displayed like

Value1      Value 2    Value 3
199          202           0.25
1698         205           0.25
15           210           0.25
971          230           0.25
2
  • you need to give more detail about what you are trying to achieve. And what is the point of that code at all? It does not use the results of that calculation at all oO. Commented Jun 30, 2013 at 8:39
  • the array year_values contains values like: Array ( [0] => 2013 [1] => 1 [2] => 205 [3] => [4] => 266 [5] => 199 ) Array ( [0] => 2013 [1] => 1 [2] => 1748 [3] => [4] => 299 [5] => 1698 ) Array ( [0] => 2013 [1] => 1 [2] => 15 [3] => [4] => 170 [5] => 15 ) Array ( [0] => 2013 [1] => 1 [2] => 1000 [3] => [4] => 31 [5] => 971 ) Array ( [0] => 2013 [1] => 1 [2] => 975 [3] => [4] => 117 [5] => 947 ) i want to use the all 5th index values for formula computation like:$eo = sqrt(2 *$vals[5]*$r /($e*$w)); and finally display results Commented Jun 30, 2013 at 9:03

3 Answers 3

3

I guess you just have to store all your data, so you could reuse it.

$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);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

every value in $vals[5] is repeating now
2

Simply reverse the statements:

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


       }
    }

1 Comment

i tried it but in this case the query results repeat for each value in array $year_values
1

mysql_fetch_row will return false once the rows have been fetched all. To reuse it you should store them in a variable (like the response made by Jari) or you could switch the two cycles (see imulsion's answer)

3 Comments

ah ops sorry, confused you with author... i saw only now your question, I will edit.
i tried the proposed solutions but still the results are not accurate, repetition occurs,
Finally my problm is fixed solution is: foreach($year_values as $key => $vals) { $w = $rows[$key][2]; $e = $rows[$key][0]; $r = $rows[$key][1]; $tt = $rows[$key][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); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.