Solved

PHP; Calculate Standard Deviation and Mean from Dreamweaver Recordset

Posted on 2013-05-26
3
497 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

630 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