How to iterate multi dimensional array and calculate sum for each row in php? -


i have array retrieved data database , put in array :

$query = "select * caseunder";  $result=mysql_query($query) or die('error, insert query failed');  $array[] = array(); $numrows=mysql_num_rows($result);  while ($i < $numrows){      $allergies =mysql_result($result, $i, "allergies");       $vege = mysql_result($result,$i, "vege");       $age = mysql_result($result, $i, "age");       $bmi =mysql_result($result, $i, "bmi");      $solution = mysql_result($result,$i, "solution");      $bmi2 = $_post['weight'] / (($_post['height']/100)*($_post['height']/100));     if($_post['age']>18 && $_post['age']<35)         $age2 = 'young ';     if($_post['age']>40 && $_post['age']<50)         $age2 = 'middle age ';     if($_post['age']>60)         $age2 = 'old men ';     $array[] = array('age'=>$age2,'allergies'=>$allergies,'vege'=>$vege,'bmi'=>$bmi2,'solution'=>$solution);      i++ } 

then, want compare each element in array input entered , calculate sum each row of array :

foreach($array $cases) {      if($cases['allergies'] == $_post['allergies']){         $count = 1;     }      if($cases['vege'] == $_post['vege']){         $count1 = 1;     }      if($cases['bmi'] == $bmi2)         $count2 = 1;      if($cases['age'] == $age2)         $count3 = 1;      $sum = $count + $count1 + $count2 + $count3;     echo $sum; } 

lets have entered age,bmi, allergies , vege same first row of database, the total sum should output 4 because 4 same comparison of data. in case try, every row of database should have different total sum because not same.but did not output want, example of wrong output:

0 4 4 4 4 4 4 4 4 

(assuming have 8 rows of database in phpmyadmin) first row of database after compared manually sum 4 seems when continue looping next row take same amount prev row.

when loop:

foreach($array $cases) {      if($cases['allergies'] == $_post['allergies']){         $count = 1;     }     if($cases['vege'] == $_post['vege']){         $count1 = 1;     }     if($cases['bmi'] == $bmi2)         $count2 = 1;     if($cases['age'] == $age2)         $count3 = 1;     $sum = $count + $count1 + $count2 + $count3;     echo $sum; } 

you not resetting $count, $count1, etc. variables between iterations. why

when continue looping next row take same amount prev row.

i don't need these separate variables unless using them else isn't included in question. can initialize sum 0 each repetition, increment directly if conditions match.

foreach($array $cases) {     $sum = 0;        if($cases['allergies'] == $_post['allergies']){         $sum++;     }     if($cases['vege'] == $_post['vege']){         $sum++;     }     if($cases['bmi'] == $bmi2) {         $sum++;     }     if($cases['age'] == $age2) {         $sum++;     }     echo $sum; } 

incidentally, looks way setting bmi , age in first loop make values match. i'm not sure if that's intended, seems kind of unlikely.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -