Forum Moderators: coopster

Message Too Old, No Replies

Getting db values into array for php median calculation

How do I get mysql values into array for php median

         

syd1975

9:36 pm on Apr 30, 2003 (gmt 0)

10+ Year Member



Hi, this is my first time posting to this forum. I am just learning php/mysql; actually, put my first survey up about a month ago and worked beautifully. Am now doing a survey of tuitions for different schools and am trying to find the median. The php formula for the median seems to work fine (I actually found it on this forum), but I don't know how to get my tuition values into an array from the mysql database.

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.

senior mcinvale

10:47 pm on Apr 30, 2003 (gmt 0)

10+ Year Member



check out the array functions on PHP.net (http://php.net/manual/en/function.array.php)

array_push should help you out.

aspr1n

10:49 pm on Apr 30, 2003 (gmt 0)

10+ Year Member



$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

aspr1n

10:56 pm on Apr 30, 2003 (gmt 0)

10+ Year Member



actually sorry re-read after posting and should be:

[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

daisho

11:05 pm on Apr 30, 2003 (gmt 0)

10+ Year Member



Even though it may work (which I didn't know it did) you should not be using the return value of mysql_query() directly . It is defined by PHP as a "resource".

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.

syd1975

11:37 pm on Apr 30, 2003 (gmt 0)

10+ Year Member



Thanks to you all for your responses. I tried them and here's what happened:

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.

daisho

12:19 am on May 1, 2003 (gmt 0)

10+ Year Member



Try:

<?
/* 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";
?>

syd1975

2:23 am on May 1, 2003 (gmt 0)

10+ Year Member



Daisho, you are great...it worked perfectly! Thank you so much for working this out for me.

Thanks to everyone else who responded, too. I'm still new at this and have learned something new from each of your responses.

daisho

4:07 am on May 1, 2003 (gmt 0)

10+ Year Member



Glad to help. Now your commited to give answers when you read a question that you can answer ;)

daisho.

syd1975

9:53 am on May 1, 2003 (gmt 0)

10+ Year Member



I'll try my best to do so. Like probably most people here, I've learned an enormous amount over the years from these kinds of boards. I don't think I've ever been at a point where I was as knowledgeable as most of the others who post, but if I have something to offer, I will do so.

senior mcinvale

3:47 pm on May 1, 2003 (gmt 0)

10+ Year Member



"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)?"

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.

syd1975

12:50 pm on May 2, 2003 (gmt 0)

10+ Year Member



senior_mcinvale: "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."

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.