Thread: PHP and Math

Results 1 to 4 of 4
  1. #1
    Ugleh's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Location
    House, Where you live?
    Posts
    276
    Reputation
    17
    Thanks
    221
    My Mood
    Blah

    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

  2. The Following User Says Thank You to Ugleh For This Useful Post:

    Spookerzz (02-23-2010)

  3. #2
    Spookerzz's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Posts
    4,647
    Reputation
    26
    Thanks
    572
    Great I suck at maths... Always time to learn though... Thanks your tuts are great!

    Very detailed and explain every aspect of it...
    I'm back.

  4. #3
    yearupie's Avatar
    Join Date
    Apr 2008
    Gender
    male
    Posts
    54
    Reputation
    11
    Thanks
    2
    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]

  5. #4
    Ugleh's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Location
    House, Where you live?
    Posts
    276
    Reputation
    17
    Thanks
    221
    My Mood
    Blah
    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.

Similar Threads

  1. PHP and Databases #1
    By yearupie in forum PHP Programming
    Replies: 3
    Last Post: 07-05-2010, 09:11 PM
  2. Fuck Math and Physics.
    By Zeinland in forum Flaming & Rage
    Replies: 31
    Last Post: 03-18-2009, 05:45 PM
  3. Plz I Want Maple Global Hacks And Where Do I Get Game Engine 2 Make The Hacks Work???
    By mattinthehat in forum MapleStory Hacks, Cheats & Trainers
    Replies: 3
    Last Post: 01-15-2006, 06:12 PM
  4. Lineag2 and Ragnarok
    By suppaman in forum General Gaming
    Replies: 12
    Last Post: 01-01-2006, 04:07 PM
  5. i need short icq number pls and hack to wr..
    By BoneXDBreaker in forum WarRock - International Hacks
    Replies: 1
    Last Post: 12-26-2005, 05:08 PM