Jump to content

Percentages


Canman2005

Recommended Posts

Hi all

 

I have a simple query, it looks like

 

$sql = "SELECT * FROM `thisdata` WHERE `id` = 1";
$query = @mysql_query($sql,$connection) or die(mysql_error());
while ($row = mysql_fetch_array($query))
{
$datavalue1 = $row['dataone'];
$datavalue2 = $row['datatwo'];
$datavalue3 = $row['datathree'];
$datavalue4 = $row['datafour'];
$datavalue5 = $row['datafive'];
}

 

now each $datavalue (1-5) holds a number, so the above may give

 

$datavalue1 = 12

$datavalue2 = 342

$datavalue3 = 4532

$datavalue4 = 2

$datavalue5 = 767

 

there isnt always 5 values, sometimes there maybe just 2, 3 or 4 values, such as

 

$datavalue1 = 54

$datavalue2 = 2

$datavalue3 =

$datavalue4 =

$datavalue5 =

 

is it possible to gather all the values given and then provide a percentage out of 100 for each one, so if I had 3 values then there % would be as follows

 

$datavalue1 = 36 - would equal 18%

$datavalue2 = 64 - would equal 32%

$datavalue3 = 100 - would equal 50%

$datavalue4 =

$datavalue5 =

 

Does this make sense?

 

Can anyone help

 

thanks

 

ed

Link to comment
https://forums.phpfreaks.com/topic/80367-percentages/
Share on other sites

Instead of SELECT *, select just the values you want

 

SELECT dataone, datatwo, datathree, datafour, datafive FROM ...

 

Now $row contains just these values.

$total = array_sum ($row);
foreach ($row as $val) {
     if ($val) echo $val*100/$total;
}

Link to comment
https://forums.phpfreaks.com/topic/80367-percentages/#findComment-407364
Share on other sites

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.