Search code examples
phpradio-buttonmedian

Determine median using php


I have an HTML page that has radio buttons that you can choose from. One is Age Range, and the other one is Salary Range. These are handled by a php file, what i want to do is be able to print

  • The user’s age range
  • The user’s salary range
  • The median salary for the user’s age range
  • A statement about how the user compares to the median for his/her age
    So far i am only printing the user's age range, and salary range. I cannot figure out how to print the other ones.

    Income

    <table>
        <tr>
            <td>
                Age Range</td>
            <td colspan="2">
                Salary Range</td>
        </tr>
        <tr>
            <td>
                <input id="Radio1"  checked="checked" name="R1" type="radio" value="V1" />15 to 
                24 Years</td>
            <td>
                <input id="Radio7" checked="checked" name="R7" type="radio" value="V1" />$15,000 
                to $24,999</td>
            <td>
                <input id="Radio13"  name="R7" type="radio" value="V1" />$75,000 
                to $84,999</td>
        </tr>
        <tr>
            <td>
                <input id="Radio2"  name="R1" type="radio" value="V1" />25 to 
                34 Years</td>
            <td>
                <input id="Radio8"  name="R7" type="radio" value="V1" />$25,000 
                to $34,999</td>
            <td>
                <input id="Radio14"  name="R7" type="radio" value="V1" />$85,000 
                to $94,999</td>
        </tr>
        <tr>
            <td>
                <input id="Radio3"  name="R1" type="radio" value="V1" />35 to 
                44 Years</td>
            <td>
                <input id="Radio9"  name="R7" type="radio" value="V1" />$35,000 
                to $44,999</td>
            <td>
                <input id="Radio15"  name="R7" type="radio" value="V1" />$95,000 
                to $104,999</td>
        </tr>
        <tr>
            <td>
                <input id="Radio4" name="R1" type="radio" value="V1" />45 to 
                54 Years</td>
            <td>
                <input id="Radio10"  name="R7" type="radio" value="V1" />$45,000 
                to $54,999</td>
            <td>
                <input id="Radio16"  name="R7" type="radio" value="V1" />$105,000 
                to $114,999</td>
        </tr>
        <tr>
            <td>
                <input id="Radio5"  name="R1" type="radio" value="V1" />55 to 
                64 Years</td>
            <td>
                <input id="Radio11" name="R7" type="radio" value="V1" />$55,000 
                to $64,999</td>
            <td>
                <input id="Radio17"  name="R7" type="radio" value="V1" />$115,000 
                to $124,999</td>
        </tr>
        <tr>
            <td>
                <input id="Radio6"  name="R1" type="radio" value="V1" />65 
                Years and Older</td>
            <td>
                <input id="Radio12"  name="R7" type="radio" value="V1" />$65,000 
                to $74,999</td>
            <td>
                <input id="Radio18"  name="R7" type="radio" value="V1" />$125,000 
                and up</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </table>
    <input type="submit" name="submit" value="See Where You Stand">
    

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Income</title>
<style>

body { font-family: Helvetica, Arial, sans-serif; }
body { font-size: 85%; }


</style>



</head>
<body style="background-color:lightgray; ">

<form action="AgeGroupSalaryRange.php" method="post" name="agegroup">

<h1>Income</h1>

    <table>
        <tr>
            <td>
                Age Range</td>
            <td colspan="2">
                Salary Range</td>
        </tr>
        <tr>
            <td>
                <input id="Radio1"  checked="checked" name="R1" type="radio" value="V1" />15 to 
                24 Years</td>
            <td>
                <input id="Radio7" checked="checked" name="R7" type="radio" value="V1" />$15,000 
                to $24,999</td>
            <td>
                <input id="Radio13"  name="R7" type="radio" value="V1" />$75,000 
                to $84,999</td>
        </tr>
        <tr>
            <td>
                <input id="Radio2"  name="R1" type="radio" value="V1" />25 to 
                34 Years</td>
            <td>
                <input id="Radio8"  name="R7" type="radio" value="V1" />$25,000 
                to $34,999</td>
            <td>
                <input id="Radio14"  name="R7" type="radio" value="V1" />$85,000 
                to $94,999</td>
        </tr>
        <tr>
            <td>
                <input id="Radio3"  name="R1" type="radio" value="V1" />35 to 
                44 Years</td>
            <td>
                <input id="Radio9"  name="R7" type="radio" value="V1" />$35,000 
                to $44,999</td>
            <td>
                <input id="Radio15"  name="R7" type="radio" value="V1" />$95,000 
                to $104,999</td>
        </tr>
        <tr>
            <td>
                <input id="Radio4" name="R1" type="radio" value="V1" />45 to 
                54 Years</td>
            <td>
                <input id="Radio10"  name="R7" type="radio" value="V1" />$45,000 
                to $54,999</td>
            <td>
                <input id="Radio16"  name="R7" type="radio" value="V1" />$105,000 
                to $114,999</td>
        </tr>
        <tr>
            <td>
                <input id="Radio5"  name="R1" type="radio" value="V1" />55 to 
                64 Years</td>
            <td>
                <input id="Radio11" name="R7" type="radio" value="V1" />$55,000 
                to $64,999</td>
            <td>
                <input id="Radio17"  name="R7" type="radio" value="V1" />$115,000 
                to $124,999</td>
        </tr>
        <tr>
            <td>
                <input id="Radio6"  name="R1" type="radio" value="V1" />65 
                Years and Older</td>
            <td>
                <input id="Radio12"  name="R7" type="radio" value="V1" />$65,000 
                to $74,999</td>
            <td>
                <input id="Radio18"  name="R7" type="radio" value="V1" />$125,000 
                and up</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </table>
	<input type="submit" name="submit" value="See Where You Stand">
</form>
</body>
</html>

Here is the php code

<?php
$agerange = $_POST['R1'];
$incomerange = $_POST['R7'];
/*$mediansalary = */




?>

<?php 


AgeBrackets: ("15 to 24 Years", "25 to 34 Years", "35 to 44 Years", "45 to     54 Years", "55 to 64 Years", "65 Years and Older");




MedianIncome: (34311, 52702, 64973, 67141, 57538, 35611);




IncomeRangesStart: (15000, 25000, 35000, 45000, 55000, 65000, 75000, 85000, 95000, 105000, 115000, 125000);




?>

Solution

  • This function will return the median of an array of values.

    <?php
    function getMedian($arr) {
        sort($arr);
        $count = count($arr);
        $middleval = floor(($count-1)/2);
        if ($count % 2) {
            $median = $arr[$middleval];
        } else {
            $low = $arr[$middleval];
            $high = $arr[$middleval+1];
            $median = (($low+$high)/2);
        }
        return $median;
    }
    

    I'm struggling to understand your form/code and to see what this array might be, but hopefully this helps.