abrew Posted June 27, 2012 Share Posted June 27, 2012 hi again ,,, i need help again (as beginers) ,,, plizz i'm going to run percentile function that i found by googling it's work well with the example ,,, here's the script ; require_once("../lib/Stat.class.php"); $stat = new Stat(); $data = array(12,34, 21, 14, 23, 56); echo "data = ".$data."<br />"; echo "25th Percentile = ".$stat->percentile($data,25)."<br />"; echo "Median (50th percentile) = ".$stat->median($data)."<br />"; echo "95th Percentile = ".$stat->percentile($data,95)."<br />"; echo "quartile(25th, 50th, 75th percentile) = ".implode(", ", $stat->quartiles($data))."<br />"; but i need to use this function to calculate my data on database so i do like this require_once("../lib/Stat.class.php"); $stat = new Stat(); $T10p = "SELECT MaxTemp FROM monthly_syn"; $T10pqres = mysql_query($T10p); $data = mysql_fetch_array($T10pqres); // $data = array(12,34, 21, 14, 23, 56); echo "data = ".$data."<br />"; echo "25th Percentile = ".$stat->percentile($data,25)."<br />"; echo "Median (50th percentile) = ".$stat->median($data)."<br />"; echo "95th Percentile = ".$stat->percentile($data,95)."<br />"; echo "quartile(25th, 50th, 75th percentile) = ".implode(", ", $stat->quartiles($data))."<br />"; and the result is wrong ,,, is that because of MYSQL_FETCH_ARRAY ,,, ??? i think mysql_fetch_array will get the same result as array <this make me confuse> please any idea ??? here's the stat.class.php ; <?php class Stat{ function median($data){ $median = $this->percentile($data,50); return $median; } function percentile($data,$percentile){ if( 0 < $percentile && $percentile < 1 ) { $p = $percentile; }else if( 1 < $percentile && $percentile <= 100 ) { $p = $percentile * .01; }else { return ""; } $count = count($data); $allindex = ($count-1)*$p; $intvalindex = intval($allindex); $floatval = $allindex - $intvalindex; sort($data); if(!is_float($floatval)){ $result = $data[$intvalindex]; }else { if($count > $intvalindex+1) $result = $floatval*($data[$intvalindex+1] - $data[$intvalindex]) + $data[$intvalindex]; else $result = $data[$intvalindex]; } return $result; } function quartiles($data) { $q1 = $this->percentile($data,25); $q2 = $this->percentile($data, 50); $q3 = $this->percentile($data, 75); $quartile = array ( '25' => $q1, '50' => $q2, '75' => $q3); return $quartile; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/264854-does-array-same-with-mysql_fetch_array/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.