Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP; Calculate Standard Deviation and Mean from Dreamweaver Recordset

Posted on 2013-05-26
3
Medium Priority
?
510 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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…
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…
Suggested Courses

886 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