Jump to content

Automatically change a variable within WHILE statement?


jkkenzie

Recommended Posts

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

 

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?

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

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.