Jump to content

$data2 = $data2 + $data; Does not work


popunonkok

Recommended Posts

Hi

 

Im plotting a graph using JPGraph.

 

The first plotted line uses $data as its source. It works like a charm, I collect the data from a MySql database.

 

However, now I want to plot a new line. I want that line to use the same data but I want to add each value to the value before and so on.

 

This is my code that dosent work.

while ($row = mysql_fetch_array($result)) {
$data[] = $row['EF0000000DBBDD1D']*6/1000;
$data1[] = strtotime($row['DATETIME']);
$data2[] = $data2 + $data;
}

echo "-----------------------------";
print_r ($data);
print_r ($data2);

 

The result Im looking for is this:

 

$data =   2, 2, 2, 2, 2, 2, 2, 2, 2.....
$data2 = 2, 4, 6, 8, 10, 12, 14.......

 

I thought that this should be easy but I just cant seem to find the answer.

 

Any help is appreciated.

 

Link to comment
Share on other sites

That should be $data2[$index] + $data[$index-1], which is difficult because your index is not explicit.  I would change it to this:

 

$index =0 ;
while ($row = mysql_fetch_array($result)) {
$data[$index] = $row['EF0000000DBBDD1D']*6/1000;
$data1[$index] = strtotime($row['DATETIME']);
$data2[$index] = ($index == 0 ? $data[$index] : $data2[$index-1] + $data[$index]);
$index++;
}

 

The conditional says "If index is 0 (ie it's the first data point), just put it straight in.  Otherwise, add it to the previous data point from $data2 array"

Link to comment
Share on other sites

EDIT: btherl beat me to it, but I will post this anyway since my solution is much more efficient (no offense btherl)

 

Um, yeah. If you notice the code you put in the post title is NOT the same as the code you posted in the description. There is a huge difference - mainly that $data2 is an array!

$data2[] = $data2 + $data;

 

How can you create a new value by adding an array to a numeric value? You need to think about what you are really trying to accomplish in simple terms and then translate it into code. Here is how I would state the problem: "I want to generate a value on each iteration which is the sum of the current iteration value and all previous iteration values".

 

There is a very easy solution to your problem. Since $data contains the current iteration value and all previous iteration values, you just need the sum of all the values in that array!:

 

while ($row = mysql_fetch_array($result))
{
    $data[]  = $row['EF0000000DBBDD1D']*6/1000;
    $data1[] = strtotime($row['DATETIME']);
    $data2[] = array_sum($data);
}

Link to comment
Share on other sites

I'll agree that's that's a simpler solution mjdamato, but it's O(n^2) rather than O(n).  So it depends what you mean by "efficient" :)  Unless there's 1000s of data points it probably doesn't matter, and I would go with your simpler solution.

Link to comment
Share on other sites

I'll agree that's that's a simpler solution mjdamato, but it's O(n^2) rather than O(n).  So it depends what you mean by "efficient" :)  Unless there's 1000s of data points it probably doesn't matter, and I would go with your simpler solution.

 

I see what you are saying. However, if there are a large number of records, then I wouldn't want to run a ternary operator on each iteration (they are less efficient than normal IF statements). Instead, I would probably use a tracking variable. E.g.

$total = 0;
while ($row = mysql_fetch_array($result))
{
    $value = $row['EF0000000DBBDD1D']*6/1000;
    $total =+ $value;
    $data[]  = $value;
    $data1[] = strtotime($row['DATETIME']);
    $data2[] = $total;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.