Forum Moderators: coopster
My query is:
$query="SELECT tuition FROM tuitiontable";
$result=mysql_query($query);
$num=mysql_numrows($result);
The PHP code for the median (posted by andreasfreidrich, but I tried to adapt it):
[PHP]
sort ($result, SORT_NUMERIC);
if ($num % 2) {
$median = $result[floor($num/2)];
} else {
$median = ($result[$num/2] + $result[$num/2 - 1]) / 2;
}
echo "Number of Schools: $num<br>Median: $median";
[PHP]
This will produce the number of schools fine, but for the median I keep getting "0" as the answer; so, for some reason, it's not reading my array values properly. Would appreciate your suggestions. Thanks.
$query="SELECT tuition FROM tuitiontable";
$result=mysql_query($query);
$num=mysql_numrows($result);
sort ($result, SORT_NUMERIC);
if ($num % 2) {
$median = $result[floor($num/2)];
} else {
$median = ($result[$num/2] + $result[$num/2 - 1]) / 2;
}
echo "Number of Schools: $num<br>Median: $median";
what does the function floor return? Remember $result is not an array so you can't use the results of floor(); as an index into the array - cos it isn't one.
try something like:
$result = mysql_query( $query );
do {
$array = $row; // Need to be careful how you assign here
} while ( $row = mysql_fetch_array( $result ));
sort ($array, SORT_NUMERIC);
$median = $array[floor($num/2)]; asp
[pre]
$result = mysql_query( $query );
$array = array( array());
for( $i=0; $i< $num; $i++ ) {
$array[$i] = mysql_fetch_array( $result, TYPE );
}
sort ($array, SORT_NUMERIC);
$median = $array[floor($num/2)];[/pre] Still assuming of course floor(); gives you an index into the array.
asp
It is meant to be a parameter to functions like "mysql_fetch_array", "mysql_result" and a few others. Not sure if that is your problem or not but thought I'd mention it since it could cause you problems down the road since you are using undocumented behaviour.
daisho.
senior mcinvale: I checked out array_push. It looks like it's used to add individual items to an array (16660, 14300, etc.), but since the number of tuition amounts in my database will vary, depending on how many people have responded, how would I go about adding all the values in my $tuition field (about 50-60 values)?
aspr1n: I tried your php code, but then when I typed [PHP] echo "$median";[PHP] at the end to output the median, the only thing that was output was the word "Array." Was I supposed to change any of your code, or use as is (I cut and pasted it and used it as is)?
daisho: I think you could be right about mysql_query. When I was playing around with it earlier, the result came out to be the word "resource4" which made no sense to me at the time, but now it does. I'm just not sure what I should be using.
Anyhow, thanks to you all for your help. I will keep on trying to make it work.
<?
/* The order by saves you from doing the sort. Let the DB do it */
$query="SELECT tuition FROM tuitiontable ORDER BY tuition ASC";
$result=mysql_query($query);
/* Not needed just for safety to ensure that $thearray is an array incase the SQL does not return any results */
$thearray=array();
while ( $row=mysql_fetch_array($result,MYSQL_NUM) ) {
$thearray[]=$row[0]; // Similar to array push
}
/* This should be the same as $num=mysql_numrows($result); but since we are using $thearray for access lets make sure that count matches! */
$num=count($thearray);
if ($num % 2) {
$median = $thearray[floor($num/2)];
} else {
$median = ($thearray[$num/2] + $thearray[$num/2 - 1]) / 2;
}
echo "Number of Schools: $num<br>Median: $median";
?>
run a loop through your results. push each value into your array. BAM! you have however many values were pulled from the database into your array.
now you can run that array through your median function to get that value.
Yes, you are correct! In fact, the code that daisho provided did use array_push. Sorry, being so new to this, I didn't catch on immediately to what you were describing.