Solved

PHP; Calculate Standard Deviation and Mean from Dreamweaver Recordset

Posted on 2013-05-26
3
494 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 110

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 110

Expert Comment

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

752 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