Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP; Calculate Standard Deviation and Mean from Dreamweaver Recordset

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

722 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