...

View Full Version : converting values to percentage%



jarv
08-15-2008, 04:38 PM
Can someone please help me, $values gives me numbers such as: 265,445,86,85

What I need to do is convert these numbers to % for Google Chart
Can someone please help?



//looping counties
$query1 = "SELECT rsCounty, COUNT(PubID) AS County_Count FROM pubs GROUP BY rsCounty";
$result1 = mysql_query($query1) or die(mysql_error().'<br>SQL: ' . $query1);
echo mysql_num_rows($result1);

$county_select_options = '<option value="">Search county</option>';
while ($row = mysql_fetch_assoc($result1))
{
$counties[$row['rsCounty']] = $row['County_Count'];
// $county_select_options .= '<option value="display.php?PubID='.$row['id'].'">'.$row['rsCounty'].' (x'.$row['County_Count'].')</option>'."\n";
}
echo "<pre>";
print_r($counties);
echo "</pre>";

$values = "";
$labels = "";
foreach($counties as $rsCounty=>$count)
{

$values .= $count.',';
$labels .= $rsCounty.'|';
}
$values = rtrim($values, ',');
echo "$values";
$labels = rtrim($labels, '|');
$url = 'http://chart.apis.google.com/chart?cht=p3&chd=t:'.$values.'&chs=480x200&chl='.$labels.'&chco=0000ff';
echo "$url";
?>
<img src="<?php echo $url ?>" />

hinch
08-15-2008, 04:56 PM
What are they a &#37; of?

if its simply a percent of another variable then its

$pervalues = ($values/$othervar)*100;

so if you have 1500 county's

$county=1500;
$pervalues = ($values/$county)*100;

would give you the perct of $county taken up by the $values.

jarv
08-15-2008, 07:30 PM
sorry, I don't understand hinch?!

hinch
08-15-2008, 10:10 PM
try put this outside of your foreach
$totnumb = mysql_num_rows($result1);

put this inside of your foreach
$pervalues = ($count/$totnumb)*100;

If i've got your code right anyway

basically to work out a percent you take your item count in your case the value you add to $values each time and you then divide that number by the total number of results returned (calculated by $totnumb = mysql_num_rows($result1); ) then multiply the answer of that by 100. its fairly simple maths its just a case of working out which if your variables needs to be divided by what.

jarv
08-16-2008, 12:46 PM
thanks, i tried that see below:



<?php
$values = "";
$labels = "";
$totnumb = mysql_num_rows($result1);
foreach($counties as $rsCounty=>$count)
{
$pervalues = ($count/$totnumb)*100;
$values .= $pervalues.',';
$labels .= $rsCounty.'|';
}
$values = rtrim($values, ',');
echo "$values";
echo "$pervalues";
$labels = rtrim($labels, '|');
$url = 'http://chart.apis.google.com/chart?cht=p3&chd=t:'.$values.'&chs=480x200&chl='.$labels.'&chco=0000ff';
echo "$url";
?>
<img src="<?php echo $url ?>" />
now i get 4 1/4

the numbers i get now are: 6625,11125,2150,2125

hinch
08-16-2008, 12:58 PM
hrmp seems i didn't understand your code correctly :)
basically divide the small number over the big number and then multiply the result by 100 to get a percent I assume your big number was the total count and the small number was the item count in your for each.

Guess I was wrong.

http://en.wikipedia.org/wiki/Percent will help you with the maths you'll have to get the right input vars though.

oesxyl
08-16-2008, 12:59 PM
try this:


<?php
//looping counties
$query1 = "SELECT rsCounty, COUNT(PubID) AS County_Count FROM pubs GROUP BY rsCounty";
$result1 = mysql_query($query1) or die(mysql_error().'<br>SQL: ' . $query1);
echo mysql_num_rows($result1);

$county_select_options = '<option value="">Search county</option>';
while ($row = mysql_fetch_assoc($result1))
{
$counties[$row['rsCounty']] = $row['County_Count'];
// $county_select_options .= '<option value="display.php?PubID='.$row['id'].'">'.$row['rsCounty'].' (x'.$row['County_Count'].')</option>'."\n";
}
echo "<pre>";
print_r($counties);
echo "</pre>";

// make this arrays not strings:
$values = array();
$labels = array();
// compute the sum:
$total = array_sum($counties);
foreach($counties as $rsCounty=>$count){
// compute percent and add to values list:
$values[] = ($count*100/$total);
$labels[] = $rsCounty;
}
// convert array $values to a string comma separated
$values = implode(',',$values);
echo "$values";
// same conversion for $labels but '|' separated
$labels = implode('|', $labels);
$url = 'http://chart.apis.google.com/chart?cht=p3&chd=t:'.$values.'&chs=480x200&chl='.$labels.'&chco=0000ff';
echo "$url";
?>
<img src="<?php echo $url ?>" />

read comments, post question. Test the code, I didn't test it, post problems. I will help you to make things clear, :)

use php tags, if you look to the colors you can see errors if there are
best regards

jarv
08-16-2008, 01:16 PM
that's brilliant! thanks! you lot are the best! x

oesxyl
08-16-2008, 01:35 PM
that's brilliant! thanks! you lot are the best! x
:)
did you test it? did you have any question about code?

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum