Solved

PHP; Calculate Standard Deviation and Mean from Dreamweaver Recordset

Posted on 2013-05-26
3
484 Views
Last Modified: 2013-05-26
Would like to calculate the mean and standard deviation from the 'id' field of a simple Dreamweaver Recordset which is:

mysql_select_db($database_conn_latestmysql, $conn_latestmysql);
$query_rst_price1 = "SELECT id FROM latest_ctry";
$rst_price1 = mysql_query($query_rst_price1, $conn_latestmysql) or  die(mysql_error());
$row_rst_price1 = mysql_fetch_assoc($rst_price1);
$totalRows_rst_price1 = mysql_num_rows($rst_price1);

Two difficulties:
1. correctly creating the array from the recordset
2. using the function sd($array).

Could anyone please help?
0
Comment
Question by:tweddlewalker
  • 2
3 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39197970
Do you have the function sd() code written yet?  If not, check this page.  It may or may not be part of your PHP installation.
http://php.net/manual/en/function.stats-standard-deviation.php

This should help get things started.

error_reporting(E_ALL);
mysql_select_db($database_conn_latestmysql, $conn_latestmysql);
$query_rst_price1 = "SELECT id FROM latest_ctry";
$rst_price1 = mysql_query($query_rst_price1) or  die(mysql_error());

// NUMBER OF ROWS
$num = mysql_num_rows($rst_price1);

// COLLECT THE ID FROM EACH ROW
$ids = array();
while ($row = mysql_fetch_object($rst_price1))
{
    $ids[] = $row->id;
}

// COMPUTATIONS HERE
$sum = array_sum($ids);
$avg = $sum / $num;

Open in new window

0
 

Author Closing Comment

by:tweddlewalker
ID: 39198077
Thank you for your help. It has unlocked the problem. Due to inexperience had omitted the line:
$ids[] = $row->id;

Now we can write:
  //calculate the variance
$the_variance = 0.0;
  for ($i = 0; $i < $num; $i++)
  {
    //sum the array
    $the_variance = $the_variance + ($ids[$i] - $idsvg) * ($ids[$i] - $idsvg);
  }
  $the_variance =$the_variance /$num-1;
  //now get the standard deviation
  $sd=sqrt($the_variance)

to complete the sequence and it works.
First rate
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39198110
Excellent!  Thanks for the points, ~Ray
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question