PHP and Math

Posts 14 of 4 · Page 1 of 1
PHP and Math
PHP requires you to know math. but not too much. An example of where PHP needs math is this.

I am making a poll system. So far I can show the options and the amount of views. Like so

Question
Option 1: 2
Option 2: 5
Option 3: 0
Option 4: 1
But what I want is a poll with an image, and to show the percentage.

Now however im finding the amount of votes for each option, its giving the value. So for each value i will give it a variable.

$optionvalue1 = (mysql query to get the value of votes in option 1);
$optionvalue2 = (mysql query to get the value of votes in option 2);
$optionvalue3 = (mysql query to get the value of votes in option 3);
$optionvalue4 = (mysql query to get the value of votes in option 4);

Ok so we want to turn them into percentage. to do this we need the total of all of them, it will be something like
$total = $optionvalue1 + $optionvalue2 + $optionvalue3 + $optionvalue4;
so now from the example above $total will be 8.

The next procedure is division by the option values. so like
2/8 will be for option 1.

$value1 = $optionvalue1 / $total;
$value2 = $optionvalue2 / $total;
$value3 = $optionvalue3 / $total;
$value4 = $optionvalue4 / $total;

the above will make $value1 which is 2/8 equal ".25".

Our next step is to make it a whole number, and since .25 is in the hundredth position, we will multiply it by 100, and it will make "25".
So now we do this.

$opt1value = $value1 * 100;
$opt2value = $value2 * 100;
$opt3value = $value3 * 100;
$opt4value = $value4 * 100;

This will make this:
$opt1value = 25
$opt2value = 62.5
$opt3value = 0
$opt4value = 12.5

Now if we add those values together we get 100.
This is what we are aiming for, because now we have transformed the value of 15 into 100%, where if we where using a bar graph, the bars image width will be the $optXvalue.


hope you got it
Great I suck at maths... Always time to learn though... Thanks your tuts are great!

Very detailed and explain every aspect of it...
optionvalue1;
optionvalue2;
optionvalue3;
optionvalue4;
Make a small function or something.

[php]
<?php

function percent($total,$value){

return number_format(($value / $total) * 100,2).'%';
}

$sql = 'SELECT SUM(value) AS total,value,option_id FROM values';
if(!$res = mysql_query($sql))
{
trigger_error(mysql_error().'<br />In query: '.$sql);
}
elseif(mysql_num_rows($res) == 0)
{
echo 'No results found';
}
else
{
while($row = mysql_fetch_assoc($res))
{
echo $row['option_id'].':'.percent($row['total'],$row['value']);
}
}
?>
[/php]
Quote Originally Posted by yearupie View Post
Make a small function or something.

[php]
<?php

function percent($total,$value){

return number_format(($value / $total) * 100,2).'%';
}

$sql = 'SELECT SUM(value) AS total,value,option_id FROM values';
if(!$res = mysql_query($sql))
{
trigger_error(mysql_error().'<br />In query: '.$sql);
}
elseif(mysql_num_rows($res) == 0)
{
echo 'No results found';
}
else
{
while($row = mysql_fetch_assoc($res))
{
echo $row['option_id'].':'.percent($row['total'],$row['value']);
}
}
?>
[/php]
makes sense, very nice.
Posts 14 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?