## Calculate The Median using Function with an Array

Hello All!

I am trying to calculate the Median of an array. I was able to get the MIN, MAX, AVG, But I am having difficulty with the Median and the way the display sorts the information.

My Question is How do I get the Median, using a function individually for each status. (Status being A, C, E, L, P, W)

My Code is:
PHP Code:
```     <?php     echo "<p><b>A</b> = Active, <b>E</b> = Expired, <b>L</b> = Canceled, <b>P</b> = Pending, <b>W</b> = Withdrawn</p>\n";     // COUNT STATUS     include '_inc/include.php';     \$query = "SELECT status, COUNT(status) FROM median GROUP BY status";            \$result = mysql_query(\$query) or die(mysql_error());     // Print out result     while(\$row = mysql_fetch_array(\$result)){     echo "There are ". \$row['COUNT(status)'] ." ". \$row['status'] ." items.";     echo "<br />";     }     ?> ```
The display is:

A = Active, C = Closed, E = Expired, L = Canceled, P = Pending, W = Withdrawn
There are 6 A items.
There are 2 C items.
There are 9 E items.
There are 4 L items.
There are 2 P items.

My Code is:
PHP Code:
```     <?php     // COUNT STATUS AND RETURN MINIMUM LIST PRICE     \$query = "SELECT listprice, status, MIN(listprice), COUNT(status) FROM median GROUP BY status";            \$result = mysql_query(\$query) or die(mysql_error());     // Print out result     while(\$row = mysql_fetch_array(\$result)){     echo "The MIN list price for the  ". \$row['COUNT(status)'] ." ". \$row['status'] ." is \$" .    \$row['MIN(listprice)'];     echo "<br />";     }     ?> ```
The display is:

The MIN list price for the 6 A is \$139900.00
The MIN list price for the 2 C is \$149900.00
The MIN list price for the 9 E is \$138000.00
The MIN list price for the 4 L is \$229000.00
The MIN list price for the 2 P is \$179000.00

The MAX and AVG is similar to the above.

I tried to include a function to calculate the Median (MED)

PHP Code:
```     <?php     function median(\$arr)     {     sort(\$arr);     \$count = count(\$arr); //count the number of values in array     \$middleval = floor((\$count-1)/2); // find the middle value, or the lowest middle value     if (\$count % 2) { // odd number, middle is the median         \$median = \$arr[\$middleval];     } else { // even number, calculate avg of 2 medians         \$low = \$arr[\$middleval];         \$high = \$arr[\$middleval+1];         \$median = ((\$low+\$high)/2);     }     return \$median;     }           echo median(array(\$lp)) . "\n";  // 153500     echo "<p> </p>";     echo median(array(4.1, 7.2, 1.7, 9.3, 4.4, 3.2)) . "\n";       // 4.25          ?> ```
This works for a static array, but how do I calculate it to look like the above MIN, MAX?

PHP Code:
```     <?php     \$i=0;     while (\$i < \$num) {          \$ld=mysql_result(\$result,\$i,"listdate");     \$cd=mysql_result(\$result,\$i,"closedate");     \$contractd=mysql_result(\$result,\$i,"contractdate");     \$s=mysql_result(\$result,\$i,"status");     \$scd=mysql_result(\$result,\$i,"statuschangedate");     \$wd=mysql_result(\$result,\$i,"withdrawdate");     \$cand=mysql_result(\$result,\$i,"canceldate");     \$lp=mysql_result(\$result,\$i, "listprice");     \$sp=mysql_result(\$result,\$i,"soldprice");     \$gla=mysql_result(\$result,\$i,"gla");     \$b=mysql_result(\$result,\$i,"built");     \$o=mysql_result(\$result,\$i,"owner");     \$fs=mysql_result(\$result,\$i,"fore");     \$ss=mysql_result(\$result,\$i,"shortsale");     \$f=mysql_result(\$result,\$i,"features");     ?> ```
I think maybe the reason I having so much trouble with this is that the data in the table is uploaded via file upload (csv). But I didn't think that really had anything to do with it because the data is already there.