Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    Question converting values to percentage%

    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?

    Code:
    //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 ?>" />

  • #2
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    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.
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #3
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    sorry, I don't understand hinch?!

  • #4
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    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.
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #5
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    thanks, i tried that see below:

    Code:
    <?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

  • #6
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    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.
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • Users who have thanked hinch for this post:

    jarv (08-16-2008)

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try this:

    PHP Code:
    <?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,

    Edit: use php tags, if you look to the colors you can see errors if there are

    best regards
    Last edited by oesxyl; 08-16-2008 at 12:03 PM.

  • Users who have thanked oesxyl for this post:

    jarv (08-16-2008)

  • #8
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    that's brilliant! thanks! you lot are the best! x

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by jarv View Post
    that's brilliant! thanks! you lot are the best! x

    did you test it? did you have any question about code?

    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •