jkkenzie Posted May 10, 2008 Share Posted May 10, 2008 Is it possible to automatically change a variable say $x so as to get results for example $x1="", $x2="" within a WHILE statement? e.g $count=0; while($count<$count2) { $row[getresults5]=$result['SUM(Value)']; } am just gazing Link to comment https://forums.phpfreaks.com/topic/105007-automatically-change-a-variable-within-while-statement/ Share on other sites More sharing options...
xenophobia Posted May 10, 2008 Share Posted May 10, 2008 Not too sure what you are meaning... Are you try to get the reference id on an array? $row['getresults' . $count]; ?? Am i right? Link to comment https://forums.phpfreaks.com/topic/105007-automatically-change-a-variable-within-while-statement/#findComment-537509 Share on other sites More sharing options...
jkkenzie Posted May 10, 2008 Author Share Posted May 10, 2008 This is my code: $count41=0; while($count41<$count4) { $getresults5 = mysql_query("SELECT strCountry, SUM(Par_Value) FROM tblcountries WHERE Par_Id='$parid' AND projectid='$proid' AND strCountry='$countryname[$count41]' ORDER BY strCountry"); while($sum5 = mysql_fetch_array($getresults5)) { $sum51[] = $sum5['SUM(Par_Value)']; $countrysum[]=$sum5['strCountry']; } $count41++; } $count51=0; while($count51<$count41) { $sums10[]=$sum51[$count51]; $countrysum10[]=$countrysum[$count51]; $count51++; } $highestval= max($sums10[0],$sums10[1],$sums10[2],$sums10[3],$sums10[4],$sums10[5],$sums10[6],$sums10[7],$sums10[8],$sums10[9]); $highestvalcountry = mysql_query("SELECT strCountry FROM tblcountries WHERE Par_Id='$parid' AND projectid='$proid' AND SUM(Par_Value)='$highestval'"); $highestcounty= mysql_fetch_array($highestvalcountry) or mysql_error(); echo $highestcounty['strCountry']; I wanted to get the country with highest SUM(Par_Value) or just highest Par_Value within a group of countries. from the code above that is where am stuck. Thanks, Joe Link to comment https://forums.phpfreaks.com/topic/105007-automatically-change-a-variable-within-while-statement/#findComment-537516 Share on other sites More sharing options...
xenophobia Posted May 10, 2008 Share Posted May 10, 2008 I try to give me solutions: $sql = "SELECT strCountry, SUM(Par_Value) AS PV FROM tblcountries WHERE Par_Id='$parid' AND projectid='$proid' AND strCountry='$countryname[$count41]' GROUP BY strCountry"; $qry = mysql_query($sql) or die(mysql_error()); $highest=0; for($i=0; $i<mysql_num_rows($qry; $i++){ if(mysql_result($qry, $i, 'PV') > $highest){ $highest = mysql_result($qry, $i, 'PV'); } } echo $highest; Is this what you want? Link to comment https://forums.phpfreaks.com/topic/105007-automatically-change-a-variable-within-while-statement/#findComment-537540 Share on other sites More sharing options...
jkkenzie Posted May 10, 2008 Author Share Posted May 10, 2008 Not really but you are VERY close....THOUGH you did not get the NAME of the country with the highest SUM(Par_Value) From you code you get the highest value. but a problem on this code on line with: for($i=0; $i<mysql_num_rows($qry; $i++){ BUT i need the NAME of the country bearing the highest SUM(Par_Value). Your way of using "for" interest me, i need to know more... Thanks again.... Joe Link to comment https://forums.phpfreaks.com/topic/105007-automatically-change-a-variable-within-while-statement/#findComment-537553 Share on other sites More sharing options...
xenophobia Posted May 10, 2008 Share Posted May 10, 2008 Then this might help: $sql = "SELECT strCountry, SUM(Par_Value) AS PV FROM tblcountries WHERE Par_Id='$parid' AND projectid='$proid' AND strCountry='$countryname[$count41]' GROUP BY strCountry"; $qry = mysql_query($sql) or die(mysql_error()); $highest=0; $highest_country=""; for($i=0; $i<mysql_num_rows($qry); $i++){ if(mysql_result($qry, $i, 'PV') > $highest){ $highest = mysql_result($qry, $i, 'PV'); $highest_country = mysql_result($qry, $i, 'strCountry'); } } echo $highest_country . " has highest value: " . $highest; Link to comment https://forums.phpfreaks.com/topic/105007-automatically-change-a-variable-within-while-statement/#findComment-537565 Share on other sites More sharing options...
jkkenzie Posted May 10, 2008 Author Share Posted May 10, 2008 YOU ARE THE GENIUS..................... Thanks men..... Regards, Jose Link to comment https://forums.phpfreaks.com/topic/105007-automatically-change-a-variable-within-while-statement/#findComment-537569 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.